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<vector>

using namespace std;

int nowS = 0;
int x, y, n;
int mp[1000][1000] = {};
int k[4] = {1, -1, 0, 0};
int h[4] = {0, 0, 1, -1};
int cx[9] = {};
int cy[9] = {};
vector<int> bufX, bufY, bufD;

bool search(int xx, int yy, int dd) {
	bufX.erase(bufX.begin());
	bufY.erase(bufY.begin());
	bufD.erase(bufD.begin());

	cout << xx << " " << yy << endl;

	for (int i = 0; i < 4; i++) {
		if (xx + k[i] < 0 || xx + k[i] >= x || yy + h[i] < 0 || yy + h[i] >= y)
			continue;
		if (mp[yy + h[i]][xx + k[i]] != 0) {
			continue;
		}
		if (cx[nowS] == xx + k[i] && cy[nowS] == yy + h[i]) {
			nowS++;
			bufX.clear();
			bufY.clear();
			bufD.clear();
			if (nowS == n) {
				cout << dd + 1 << endl;
				return true;
			}
			else {
				bufX.push_back(xx + k[i]);
				bufY.push_back(yy + h[i]);
				bufD.push_back(dd + 1);
			}
			return false;
		}
		if(mp[yy + h[i]][xx + k[i]] == 0){
			bufX.push_back(xx + k[i]);
			bufY.push_back(yy + h[i]);
			bufD.push_back(dd + 1);
		}
	}
	return false;
}

int main() {
	int ch = 0;
	cin >> y >> x >> n;
	for (int i = 0; i < y; i++) {
		char c[1001];
		cin >> c;
		for (int j = 0; j < x; j++) {
			if(c[j] == 'X')
				mp[i][j] = -1;
			else if(c[j] == 'S'){
				bufX.push_back(j);
				bufY.push_back(i);
				bufD.push_back(0);
				mp[i][j] = 0;
			}	
			else {
				mp[i][j] = 0;
			}
			if (c[j] >= 48 && c[j] <= 57) {
				int m;
				m = c[j] - 49;
				cx[m] = j;
				cy[m] = i;		
				ch++;
			}
		}
	}
	

	while(!search(bufX[0], bufY[0], bufD[0])){}

	cin >> n;

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
typedef pair<int,int> P;
int da[] = {1,0,-1,0};
int db[] = {0,1,0,-1};
int h,w,n,ans=0;
char m[1002][1002];
int d[1002][1002];
int main(){
	scanf("%d%d%d",&h,&w,&n);
	for(int i = 0;i < h;i++){
		scanf("%s",m[i]);
	}
	int a,b;
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++){
			if(m[i][j] == 'S'){
				a = i,b = j;
				break;
			}
		}
	}
	for(int c = 1;c <= n;c++){
		for(int i = 0;i <= h;i++){
			for(int j = 0;j <= w;j++){
				d[i][j] = INF;
			}
		}
		d[a][b] = 0;
		queue<P> que;
		que.push(P(a,b));
		while(que.size()){
			P p = que.front(); que.pop();
			int aa = p.first,bb = p.second;
			if(m[aa][bb] - '0' == c){
				ans += d[aa][bb];
				a = aa,b = bb;
				break;
			}
			for(int i = 0;i < 4;i++){
				int na = aa + da[i];
				int nb = bb + db[i];
				if(na >= 0 && na < h && nb >= 0 && nb < w && m[na][nb] != 'X' && d[na][nb] == INF){
					d[na][nb] = d[aa][bb] + 1;
					que.push(P(na,nb));
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define INF (1<<25)

int d[1000][1000];
char grid[1000][1000];
pair<int,int>point[10];
int dy[4]={-1,1,0,0};
int dx[4]={0,0,-1,1};
int h,w,n;

int bfs(int sy, int sx, int gy, int gx){
	queue< pair<int,int>>Q;
	Q.push( {sy,sx} );

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			d[i][j]=INF;
		}
	}
	d[sy][sx]=0;

	while(Q.empty()==false){
		pair<int,int> now=Q.front();	Q.pop();
		int y=now.first, x=now.second;

		for(int i=0;i<4;i++){
			int nY=y+dy[i], nX=x+dx[i];
			if(nY<0||nX<0||nY>=h||nX>=w)continue;
			else if(d[nY][nX]==INF && grid[nY][nX]!='X'){
				d[nY][nX]=d[y][x]+1;
				Q.push( {nY,nX} );
			}

			if(nY==gy && nX==gx){
				return d[nY][nX];
			}
		}
	}

	cout<<"ERROR"<<endl;
	return -1;
}

int main(){
	cin>>h>>w>>n;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>grid[i][j];

			if(grid[i][j]=='S'){
				point[0].first=i;	point[0].second=j;
			}else if('1'<=grid[i][j]&&grid[i][j]<='9'){
				int pos=grid[i][j]-'0';
				point[pos].first=i;		point[pos].second=j;
			}
		}
	}

	int ans=0;
	for(int i=0;i<n;i++){
		int sy=point[i].first, sx=point[i].second;
		int gy=point[i+1].first, gx=point[i+1].second;
		
		ans+=bfs(sy,sx,gy,gx);
	}
	cout<<ans<<endl;

	return 0;
}

C++: 

#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstdio>

using namespace std;

typedef pair<int,int> P;
const int INF=100000000;
int h,w,n;
char maze[1005][1005];
int d[1005][1005];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
int ans;

int bfs(char charstart,char charend){
    int x,y;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(maze[i][j]==charstart){
                x=i;y=j;
            }
        }
    }
    queue<P> que;
    que.push(P(x,y));
    while(que.size()){
        x=que.front().first;
        y=que.front().second;
        que.pop();
        if(maze[x][y]==charend){
            return d[x][y];
        }
        for(int i=0;i<4;i++){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<h&&ny>=0&&ny<w&&d[nx][ny]==0&&maze[nx][ny]!='X'){
                d[nx][ny]=d[x][y]+1;
                que.push(P(nx,ny));
            }
        }
    }
}

void solve(){
    scanf("%d%d%d",&h,&w,&n);
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> maze[i][j];
        }
    }
    ans+=bfs('S','1');
    for(int i=1;i<=n-1;i++){
        memset(d,0,sizeof(d));
        char s = char(i+48);
        char e = char(i+1+48);
        ans+=bfs(s,e);
    }
    printf("%d\n",ans);
}

int main(){
    solve();
    return 0;
}