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<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;

const int N=1000;
const int wal[][2]={{1,0},{-1,0},{0,1},{0,-1}};

int n,m,k,ans;
char ma[N+5][N+5];
bool vis[N+5][N+5];
struct node
{
	int x,y,p;
}t;

inline bool Pan(int x,int y)
{
	return (x>=1 && x<=n) && (y>=1 && y<=m);
}

queue<node> Q;
void Bfs(int bx,int by,int p)
{
	while(!Q.empty()) Q.pop();
	
	vis[bx][by] = 1;
	t.x=bx,t.y=by,t.p=ans;
	Q.push(t);
	
	while(!Q.empty())
	{
		node q=Q.front(); Q.pop();
		if(ma[q.x][q.y] - '0' == p+1)
		{
			ans = q.p;
			return ;
		}
		for(int i=0;i<4;i++)
		{
			int fx=q.x+wal[i][0],fy=q.y+wal[i][1];
			if(Pan(fx,fy) && !vis[fx][fy] && ma[fx][fy] != 'X')
			{
				vis[fx][fy] = 1;
				t.x=fx;t.y=fy;t.p=q.p+1;
				Q.push(t);
			}
		}
	}
}

int main()
{
	scanf("%d %d %d",&n,&m,&k);
	for(int i=1;i<=n;i++)
		scanf("%s",ma[i]+1);
	for(int p=0;p<k;p++)
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				if(ma[i][j] == ( !p ? 'S' : p+'0'))
				{
					memset(vis,0,sizeof(vis));
					Bfs(i,j,p);
					break;
				}
	printf("%d\n",ans);
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	int h,w;
	char n;
	cin >> h >> w >> n;
	char map[h][w];
	int did[h][w]={};
	int va[h][w]={};
	queue <pair <int ,int > > q;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin >> map[i][j];
			if(map[i][j]=='S'){
				did[i][j]=1;
				q.push(make_pair(i,j));
			}
			if(map[i][j]=='X')
				did[i][j]=1;
		}
	}
	char N='1';
	int dx[4]={0,1,0,-1};
	int dy[4]={1,0,-1,0};
	int count=0;
	int s=10;
	while(q.size()){

		s--;
		//cout << "------------------------------------------------------" << endl;
		/*for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cout << did[i][j];
			}
			cout << endl;
		}
		cout << "q.front().first=" << q.front().first << " q.front().second=" << q.front().second << endl;
		cout << map[q.front().first][q.front().second] << endl;*/
		int MIN=10000;
		for(int i=0;i<4;i++){
			if(q.front().first + dy[i]<0||h<=q.front().first + dy[i]||q.front().second + dx[i]<0||w<=q.front().second + dx[i]);
			else if(va[q.front().first+dy[i]][q.front().second+dx[i]]!=0){
				va[q.front().first][q.front().second]= min(MIN,va[q.front().first+dy[i]][q.front().second+dx[i]]);
				MIN = va[q.front().first][q.front().second];
			}
		}
		va[q.front().first][q.front().second]++;
		if(map[q.front().first][q.front().second]==N){
			//cout << "******************************" << endl;
			if(N==n){
				count+=va[q.front().first][q.front().second];
				//cout << va[q.front().first][q.front().second] << endl;
				cout << count-1 << endl;
				break;
			}
			else{
				count+=va[q.front().first][q.front().second];
				//cout << va[q.front().first][q.front().second] << endl;
				//cout <<"count=" <<count << endl;
				N++;
				q.push(make_pair(q.front().first,q.front().second));
				q.push(make_pair(q.front().first,q.front().second));
				while(q.size()-1)
					q.pop();
				for(int i=0;i<h;i++){
					for(int j=0;j<w;j++){
						va[i][j]=0;
						did[i][j]=0;
						if(map[i][j]=='X')
							did[i][j]=1;
					}
				}
				//cout << q.front().first << " " << q.front().second << endl;
				did[q.front().first][q.front().second]=1;
			}
		}

		for(int i=0;i<4;i++){
			if(q.front().first + dy[i]<0||h<=q.front().first + dy[i]||q.front().second + dx[i]<0||w<=q.front().second + dx[i]);
			else if(did[q.front().first + dy[i]][q.front().second + dx[i]]!=1){
				q.push(make_pair(q.front().first+dy[i],q.front().second+dx[i]));
				did[q.front().first + dy[i]][q.front().second + dx[i]]=1;
			}
		}
		q.pop();
	}
}

C++: #include<iostream>
#include<queue>
#include<stdlib.h>
#include<string.h>
using namespace std;

const static int MAX=1000;

char map[MAX][MAX];
int H,W,N;

int startx,starty;
int nextx[4]={0,0,1,-1},nexty[4]={1,-1,0,0};

bool bmap[MAX][MAX];

int res=0,now=1;

void input()
{
	cin>>H>>W>>N;
	for(int i=0;i<H;i++)
	{
		for(int j=0;j<W;j++)
		{
			cin>>map[i][j];
			if(map[i][j]=='S')
			{
				startx=i;
				starty=j;
			}
		}
	} 
}

int solve()
{
	memset(bmap,0,sizeof(bmap));
	queue<pair<int,int> >q;
	queue<int>dis;
	q.push(make_pair(startx,starty));
	dis.push(0);
	bmap[startx][starty]=true;
	while(!q.empty())
	{
		pair<int,int> tpos=q.front();
		q.pop();
		int tdis=dis.front();
		dis.pop();
		if(atoi(&map[tpos.first][tpos.second])==now)
		{
			
			startx=tpos.first;
			starty=tpos.second;
			now++; 
			return tdis;
		}
		for(int i=0;i<4;i++)
		{
			int tempx=tpos.first+nextx[i];
			int tempy=tpos.second+nexty[i];
			if(tempx>=0&&tempx<H&&tempy>=0&&tempy<W&&map[tempx][tempy]!='X'&&!bmap[tempx][tempy])
			{
				bmap[tempx][tempy]=true;
				q.push(make_pair(tempx,tempy));
				dis.push(tdis+1);
			}
		}
	}
}

int main()
{
	input();
	for(int i=0;i<N;i++)
	{
		res+=solve();
	}
	cout<<res<<endl;
	return 0; 
}