Go: ///
// File:  0558.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:58:45 2018
//
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type pos struct {
	x, y int
}

var H, W, N int
var graph [][]string

func wfs(p pos, index int) (int, pos) {
	var q []pos
	dists := make([][]int, H)
	for i := range dists {
		dists[i] = make([]int, W)
	}

	q = append(q, p)
	for len(q) > 0 {
		p := q[0]
		dist := dists[p.x][p.y]
		q = q[1:]

		for _, d := range []pos{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} {
			x := p.x + d.x
			y := p.y + d.y
			if 0 <= x && x < H && 0 <= y && y < W && graph[x][y] != "X" && dists[x][y] == 0 {
				dists[x][y] = dist + 1
				q = append(q, pos{x, y})
			}
		}
	}

	for i := range graph {
		for j := range graph[i] {
			if graph[i][j] == strconv.Itoa(index) {
				return dists[i][j], pos{i, j}
			}
		}
	}

	panic("not found")
}

func main() {
	fmt.Scan(&H, &W, &N)

	var start pos

	graph = make([][]string, H)
	for i := range graph {
		var s string
		fmt.Scan(&s)
		graph[i] = strings.Split(s, "")
		for j := range graph[i] {
			if graph[i][j] == "S" {
				start.x = i
				start.y = j
			}
		}
	}

	count := 0
	for i := 1; i <= N; i++ {
		c, s := wfs(start, i)
		start = s
		count += c
	}

	fmt.Println(count)
}

C++: #include <iostream>
#include <sstream>
#include <cstdlib>
#include <queue>

using namespace std;

const int INF = 100000000;
const int MAX_W = 1000;
const int MAX_H = 1000;
int H, W, N;
char field[MAX_H][MAX_W] = {'X'};
int d[MAX_H][MAX_W] = {INF};
typedef pair<int, int> P;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
    queue<P> que;
    P start;
    int pow = 1;
    while (std::cin >> H >> W >> N, H) {
        for (int i = 0; i < H; i++) {
            std::cin >> field[i];
        }
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                d[i][j] = INF;
                if (field[i][j] == 'S') {
                    start = P(i, j);
                }
            }
        }
        que.push(start);
        d[start.first][start.second] = 0;
        int count = 0;
        while (que.size()) {
            P p = que.front();
            que.pop();
            if (field[p.first][p.second] == '0' + N and N <= pow) {
                count = d[p.first][p.second];
                break;
            }

            for (int i = 0; i < 4; i++) {
                int nx = p.second + dx[i];
                int ny = p.first + dy[i];
                if (nx >= 0 and ny >= 0 and nx < W and ny < H and field[ny][nx] != 'X' and d[ny][nx] == INF) {
                    if (field[ny][nx] == '.') {
                        que.push(P(ny, nx));
                        d[ny][nx] = d[p.first][p.second] + 1;
                    } else {
                        if (field[ny][nx] <= '0' + pow) {
                            //cout << "pow:" << pow << ", cheese:" << field[ny][nx] << endl;
                            int s = d[p.first][p.second];
                            field[ny][nx] = '.';
                            for (int i = 0; i < H; i++) {
                                for (int j = 0; j < W; j++) {
                                    d[i][j] = INF;
                                }
                            }


                            while (que.size()) {
                                que.pop();
                            }
                            que.push(P(ny, nx));
                            d[ny][nx] = s + 1;
                            if(pow == N) {
                                field[ny][nx] = '0' + N;
                            }
                            pow++;
                            break;
                        } else {
                            que.push(P(ny, nx));
                            d[ny][nx] = d[p.first][p.second] + 1;
                        }
                    }
                }
            }
        }

        std::cout << count << std::endl;
        break;
    }

    return 0;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long int  cheese[1001][1001];
long long int  check[1001][1001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
			
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(-1==cheese[y.a][y.b]){
			continue;
		}
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&check[y.a+q[i]][y.b+p[i]]!=1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(obj) (obj).begin(),(obj).end()

const int inf=-1;
typedef pair<int,int> P;

int h,w,n;
char a[1001][1001];
int d[1001][1001];

int bfs(P st,P go){
    int dx[4]={0,1,0,-1};
    int dy[4]={-1,0,1,0};
    queue<P> que;
    que.push(st);
    d[st.first][st.second]=0;
    while(que.size()){
        P ck=que.front();
        que.pop();
        if(ck.first==go.first&&ck.second==go.second)break;
        for(int i=0;i<4;i++){
            int nx=ck.first+dx[i];
            int ny=ck.second+dy[i];
            if(nx>0&&nx<=h&&ny>0&&ny<=w&&a[nx][ny]!='X'&&d[nx][ny]==inf){
                que.push(P(nx,ny));
                d[nx][ny]=d[ck.first][ck.second]+1;
            }
        }
        /*for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cout<<setw(2)<<d[i][j]<<' ';
            }
            cout<<endl;
        }
        cout<<endl;*/
    }
    return d[go.first][go.second];
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    P st;
    P go;
    P memo[10];
    char direct[10]={'S','1','2','3','4','5','6','7','8','9'};
    cin>>h>>w>>n;
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin>>a[i][j];
            d[i][j]=inf;
            for(int k=0;k<=n;k++){
               if(a[i][j]==direct[k]){
                   memo[k].first=i;
                   memo[k].second=j;
               }
            } 
        }
    }
    st.first=memo[0].first;
    st.second=memo[0].second;
   
    int cnt=0;
    for(int i=1;i<=n;i++){
        go.first=memo[i].first;
        go.second=memo[i].second;
       // cout<<'#'<<cnt<<endl;
        cnt+=bfs(st,go);
        for(int j=1;j<=h;j++){
            for(int k=1;k<=w;k++){
                d[j][k]=inf;
            }
        }
        st.first=memo[i].first;
        st.second=memo[i].second;
    }
    cout<<cnt<<endl;
    return 0;
}