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 <queue>
using namespace std;
int main(void){

	int h, w, n;
	int **map;
	char **str;
	queue<pair<int, int>> q;

	//???????¢????
	cin >> h >> w >> n;
	str = new char*[h];
	for (int i = 0; i < h; i++) str[i] = new char[w];
	map = new int *[h+2];
	for (int i = 0; i < h + 2; i++) map[i] = new int[w + 2];
	for (int i = 0; i < h + 2; i++){
		for (int j = 0; j < w + 2; j++){

			if (i == 0 || j == 0 || i == h + 1 || j == w + 1) map[i][j] = 100;
		}
	}

	//?????????????£??????°???????????????
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){


			cin >> str[i][j];
			map[i + 1][j + 1] = 0;
			if (str[i][j] == 'S') q.push(make_pair(i, j));
			if (str[i][j] == 'X') map[i + 1][j + 1] = 100;
		}
	}

	for (int i = 0; i < h + 2; i++){
		for (int j = 0; j < w + 2; j++){

			cout << map[i][j] << " ";
		}
		cout << endl;
	}

	int c = 0;
	char cstr[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	pair<int, int> xy;
	q.front().first++;
	q.front().second++;

	//??¢?´¢
	while (n >= c+1){
		bool flg = false;

		//??????????????°?????£??\
		xy = q.front();
		q.pop();
		//???
		if (map[xy.first][xy.second]>map[xy.first + 1][xy.second]){

			//??????
			map[xy.first + 1][xy.second] = map[xy.first][xy.second] + 1;
			flg = true;
			//????¨??????????????????????
			if (str[xy.first + 1 - 1][xy.second - 1] == cstr[c]){
				
				c++;
				//queue_cleaner
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first + 1, xy.second));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}

				}
				continue;
			}
			q.push(make_pair(xy.first + 1, xy.second));
		}
		//???
		if (map[xy.first][xy.second]>map[xy.first][xy.second + 1]){

			map[xy.first][xy.second + 1] = map[xy.first][xy.second] + 1;
			flg = true;
			if (str[xy.first- 1][xy.second + 1 - 1] == cstr[c]){

				c++;
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first, xy.second + 1));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}
				}
				continue;
			}
			q.push(make_pair(xy.first, xy.second + 1));
		}
		//???
		if (map[xy.first][xy.second]>map[xy.first - 1][xy.second]){

			map[xy.first - 1][xy.second] = map[xy.first][xy.second] + 1;
			flg = true;
			if (str[xy.first - 1 - 1][xy.second- 1] == cstr[c]){

				c++;
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first - 1, xy.second));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}
				}
				continue;
			}
			q.push(make_pair(xy.first - 1, xy.second));
		}
		//???
		if (map[xy.first][xy.second]>map[xy.first][xy.second - 1]){

			map[xy.first][xy.second-1] = map[xy.first][xy.second] + 1;
			flg = true;
			if (str[xy.first - 1][xy.second - 1 - 1] == cstr[c]){

				c++;
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first, xy.second - 1));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}
				}
				continue;
			}
			q.push(make_pair(xy.first, xy.second - 1));
		}
		if (flg) map[xy.first][xy.second]++;
		for (int i = 0; i < h + 2; i++){
			for (int j = 0; j < w + 2; j++){

				cout << map[i][j] << " ";
			}
			cout << endl;
		}
	}

	cout << map[q.front().first][q.front().second] << endl;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>

using namespace std;
int H, W,N;
int flag = 0;  //   ?志是否?束
int flagt = 0;   //   ??一个?点有几个相??点
char SearchMap[1001][1001];
int SearchFlag[1001][1001] = {0};
struct node
{
	int IndexX;
	int IndexY;
	int value;
};

queue<node> q;                                                            

void FoundTheNext(int x,int y,int val)
{                                     
	if (SearchFlag[x][y] < val)
	{
		if (SearchMap[x][y] == 'X')
			return;
		if (SearchMap[x][y] - '0' == val)
		{
			if (val == N)
				flag = 1;
			else
			{
				q.push({ x,y,val + 1 });
				SearchMap[x][y] = '.';
				flagt++;
			}
			
		}
		if (SearchMap[x][y] == '.'|| SearchMap[x][y]-'0'!=val)
		{
			q.push({ x,y,val});
			flagt++;
		}
		return;
	}
	
}

int main()
{
	scanf("%d %d %d",&H,&W,&N);
	int starti = 0, startj = 0;  //  ???始的位置

	for (int i = 1; i <= H; i++)
	{
		getchar();
		for (int j = 1; j <= W; j++)
		{
			scanf("%c", &(SearchMap[i][j]));
			if (SearchMap[i][j] == 'S')
			{
				starti = i;
				startj = j;
			}
		}
	}

	q.push({starti,startj,1});  //  ?始体力?1
	int resultTime = 0;   //  ??走的?数、??

	int t1 = 1;

	while (!q.empty()&&flag==0)
	{
		
		resultTime++;
		flagt = 0;

		while (t1--&&flag == 0)
		{
			node nodeflag = q.front();

			int x = nodeflag.IndexX, y = nodeflag.IndexY, value = nodeflag.value;

			q.pop();

			SearchFlag[x][y] = value;   //   ?置??数据的?

			if (x > 1)
				FoundTheNext(x - 1, y, value);
			if (x < H)
				FoundTheNext(x + 1, y, value);
			if (y > 1)
				FoundTheNext(x, y - 1, value);
			if (y < W)
				FoundTheNext(x, y + 1, value);
		}
		t1 = flagt;
	}
	if (flag == 1)
		printf("%d\n",resultTime);
	return 1;

}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <utility>
using namespace std;
#define INF 1000000
typedef pair<int,int> P;

int H,W,N,i,j,minute;
char field[1000][1000];
P factory[10];

int solve(int i){
	queue<P> s;
	P now;
	int dist[H][W];
	for(int k=0;k<H;k++){
		for(int j=0;j<W;j++){
			dist[k][j]=INF;
		}
	}
	int x[4]={1,0,-1,0},y[4]={0,1,0,-1};
	dist[factory[i].first][factory[i].second]=0;
	s.push(factory[i]);
	while(!s.empty()){
		now=s.front(); s.pop();
		if(now == factory[i+1]) break;
		for(int k=0;k<4;k++){
			int nx=now.first+x[k],ny=now.second+y[k];
			if(0<=nx && nx<H && 0<=ny && ny<W && field[nx][ny]!='X' && dist[nx][ny] == INF){
				s.push(P(nx,ny));
				dist[nx][ny] = dist[now.first][now.second]+1;
			}
		}
	}
	return dist[now.first][now.second];
}

int main(){
	cin>>H>>W>>N;
	for(i=0;i<H;i++){
		for(j=0;j<W;j++){
			cin>>field[i][j];
			if(1<=field[i][j]-'0' && field[i][j]-'0'<=N){factory[field[i][j]-'0']=P(i,j);}
			if(field[i][j]=='S'){factory[0]=P(i,j);}
		}
	}
	for(i=0;i<N;i++){
		minute+=solve(i);//i??????????????????????£?????????¢?´¢
	}
	cout<<minute<<endl;
	return 0;
}