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 <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

using namespace std;
typedef vector<int> veci;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define foreach(i, n) for (typeof(n) i = 0; i < (n); ++i)
#define foreach_eq(i, n) for (typeof(n) i = 0; i <= (n); ++i)
#define foreach_from(i, m, n) for (typeof(n) i = m; i < (n); ++i)
#define foreach_eq_from(i, m, n) for (typeof(n) i = m; i <= (n); ++i)

inline bool between(int lower, int upper, int x) { return lower <= x && x < upper; }
inline bool between(pii lower, pii upper, pii x) {
    return between(lower.first, upper.first, x.first)
        && between(lower.second, upper.second, x.second);
}
pii dirctions[4] = { pii(0,-1), pii(0,1), pii(-1,0), pii(1,0) };
template <typename S, typename T> inline
pair<S,T> pair_plus(const pair<S,T> & p, const pair<S,T> & q) {
    return pair<S,T>(p.first + q.first, p.second + q.second);
}

#define MAX_H 1000
#define MAX_W 1000
#define MAX_N 9

int H, W, N;
bool orig_map[MAX_H][MAX_W];
pii factories[MAX_N + 1];
queue<pair<pii, int> > que;

int main() {
    cin >> H >> W >> N;
    foreach (i, H) foreach (j, W) {
        char c;
        cin >> c;
        orig_map[i][j] = true;
        if (c == '.') {
        } else if ('0' <= c && c <= '9') {
            factories[(int)c - (int)'0'] = pii(i,j);
        } else if (c == 'S') {
            factories[0] = pii(i,j);
        } else {
            assert(c == 'X');
            orig_map[i][j] = false;
        }
    }

    int result = 0;
    foreach (n, N) {
        bool data[MAX_H][MAX_W];
        foreach (i, MAX_H) foreach (j, MAX_W) data[i][j] = orig_map[i][j];
        data[factories[n].first][factories[n].second] = false;
        que.push(pair<pii,int>(factories[n], 0));
        int found = 0;
        while (not found) {
            pii pos = que.front().first;
            int cnt = que.front().second;
            que.pop();
            foreach (d, 4) {
                pii nxt = pair_plus(pos, dirctions[d]);
                if (not between(pii(0,0), pii(H,W), nxt)) continue;
                if (nxt == factories[n+1]) {
                    found = cnt + 1;
                } else if (data[nxt.first][nxt.second]) {
                    data[nxt.first][nxt.second] = false;
                    que.push(pair<pii,int>(nxt, cnt + 1));
                }
            }
        }
        result += found;
        que = queue<pair<pii,int> >();
    }

    cout << result << endl;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1002][1002],C[11]={"0123456789"};
int D[1002][1002];
int A[1002][1002],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
	memset(A,0,sizeof(A));
   memset(D,0,sizeof(D));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			A[S.y][S.x]=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();

		S.x+=x[l];
		S.y+=y[l];
		S.Z++;

		if(A[S.y][S.x]==1||s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
		continue;
		}
				A[S.y][S.x]=1;
		if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			//memset(A,0,sizeof(A));
			l=-1;

		} 
		P.push(S);
	}
	printf("%d\n",S.Z);
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
char s[1005][1005];
int vis[1005][1005];
int a[10][2];
const int direction[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int H,W,N;
int Bfs(int x,int y,int x2,int y2) {
    for(int i=0;i<H;i++)
        for(int j=0;j<W;j++)
            vis[i][j]=-1;
    queue<pair<int,int> >Q;
    Q.push(pair<int,int>(x,y));
    vis[x][y]=0;
    while(!Q.empty()) {
        pair<int,int>p1;
        p1=Q.front(), Q.pop();
        if(p1.first==x2&&p1.second==y2) break;
        for(int i=0;i<4;i++) { 
            int dx=p1.first+direction[i][0], dy=p1.second+direction[i][1];
            if(dx>=0&&dx<W&&dy>=0&&dy>=0&&dy<H&&s[dx][dy]!='X'&&vis[dx][dy]==-1) {
                Q.push(pair<int,int>(dx,dy));
                vis[dx][dy]=vis[p1.first][p1.second]+1;
            }
        }
        
    }
    return vis[x2][y2];
}
int main() {
	scanf("%d%d%d",&H,&W,&N);
    	getchar();
        for(int i=0;i<H;i++) {
            for(int j=0;j<W;j++)
                scanf("%c",&s[i][j]);
            getchar();
        }
        for(int i=0;i<H;i++) {
            for(int j=0;j<W;j++) {
                if(s[j][i]=='S') {
                    s[j][i]='.';
                    a[0][0]=j;
                    a[0][1]=i;
                }
                else if(isdigit(s[j][i])) {
                    int temp=s[j][i]-'0';
                    a[temp][0]=j;
                    a[temp][1]=i;
                }
            }
        }
        int ans=0;
        for (int i=0;i<N;i++)
            ans+=Bfs(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
        printf("%d\n",ans);
}