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 <queue>
#include <algorithm>
#include <cstring>
#define Max_H 1000
#define Max_W 1000
using namespace std; 

typedef pair<int,int> P;

int H,W,N;
int moush;
int minTime;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
char map[Max_H+1][Max_W+1];
int d[Max_H+1][Max_W+1];

void solve(){
	memset(d,-1,sizeof(d));
	deque<P> que;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(map[i][j]=='S'){
				que.push_back(P(i,j));
				d[i][j]=0;
				break;
			}
		}
	}
	moush=1;
	while(moush!=N+1){
		P p=que.front();
		que.pop_front();
		for(int i=0;i<=3;i++){
			int tx=p.first+dx[i],ty=p.second+dy[i];
			if(tx<0||ty<0||tx>=H||ty>=W||map[tx][ty]=='X') continue;
			if(d[tx][ty]==-1) {
				if(map[tx][ty]-'0'==moush){
					moush++;
					que.clear();
					que.push_back(P(tx,ty));
					minTime+=d[p.first][p.second]+1;
					memset(d,-1,sizeof(d));
					d[tx][ty]=0;
					break;
				}else{
					que.push_back(P(tx,ty));
					d[tx][ty]=d[p.first][p.second]+1;
				}
			}
		}
	}	 
	printf("%d\n",minTime);
}

int main(){
	scanf("%d%d%d",&H,&W,&N);
	for(int i=0;i<H;i++){
		scanf("%s",map[i]);
	}
	solve();
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<list>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>

using namespace std;

int h, w, n;
vector<pair<int, int>> map;
vector<pair<int, int>> List;

queue<int> q;

int loop(int start, int end) {
	for (int i = 0; i < h * w; i++) {
		map[i].second = 0;
	}
	while (!q.empty()) {
		q.pop();
	}
	queue<int> tmp;
	q.push(start);
	for (int i = 1; true; i++) {
		while (!q.empty()) {
			int front = q.front();
			q.pop();
			map[front].second = -1;

			if (front / w != 0) {
				if (map[front - w].first != 'X' && map[front - w].second != -1) {
					if (front - w == end) {
						return i;
					}
					map[front - w].second = -1;
					tmp.push(front - w);
				}
			}

			if (front / w != h - 1) {
				if (map[front + w].first != 'X' && map[front + w].second != -1) {
					if (front + w == end) {
						return i;
					}
					map[front + w].second = -1;
					tmp.push(front + w);
				}
			}

			if (front % w != 0) {
				if (map[front - 1].first != 'X' && map[front - 1].second != -1) {
					if (front - 1 == end) {
						return i;
					}
					map[front - 1].second = -1;
					tmp.push(front - 1);
				}
			}

			if (front % w != w - 1) {
				if (map[front + 1].first != 'X' && map[front + 1].second != -1) {
					if (front + 1 == end) {
						return i;
					}
					map[front + 1].second = -1;
					tmp.push(front + 1);
				}
			}
		}
		swap(q, tmp);
	}
}

void set_map() {
	pair<int, int> map_p;
	pair<int, int> list_p;
	for (int i = 0; i < h; i++) {
		string tmp;
		cin >> tmp; // map状況
		for (int j = 0; j < w; j++) {
			map_p.first = tmp[j]; // map状況
			map_p.second = 0; //queのための値
			map.push_back(map_p);
			if (map_p.first != 'X' && map_p.first != '.') {
				list_p.first = i * w + j; // map上の何番目なのか
				if (map_p.first == 'S') { // 何番目のゴールなのか
					list_p.second = 0;
				} else {
					list_p.second = map_p.first - '0';
				}
				vector<pair<int, int>>::iterator ite = List.begin();
				int k = 0;
				for (; k < List.size(); ite = std::next(ite, 1), k++) {
					if (list_p.second < List[k].second) {
						List.insert(ite, list_p);
						break;
					}
				}
				if (k == List.size()) {
					List.push_back(list_p);
				}
			}
		}
	}
}

int main(int argc, char **argv){

	cin >> h >> w >> n;
	set_map();
	int count = 0;
	for (int i = 1; i < List.size(); i++) {
		count += loop(List[i - 1].first, List[i].first);
	}
	cout << count << endl;
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

#define MAX 1000
int y[MAX][MAX];
int x[MAX][MAX];
int px[10];
int py[10];
bool z[MAX][MAX][5];
bool a[MAX][MAX];
int sum2;
int sum3;
int main(){
	memset(z,false,sizeof(z));
	char T[12]={'.','1','2','3','4','5','6','7','8','9','S','X'};
	int n,m,c,d,sum,f,g;
	string S;
	sum=0;
	cin>>n>>m>>c;
	for(int i=0;i<n;i++){
		cin>>S;
		for(int j=0;j<m;j++){
			for(int k=0;k<12;k++){
				if(S[j]==T[k]){
					x[i][j]=k;
					if(k==10){
						px[0]=i;
						py[0]=j;
					}
					if(k>=1 && k<=9){
						px[k]=i;
						py[k]=j;
					}
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			y[i][j]=-1;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(i>=1){
				if(x[i-1][j]!=11){
					z[i][j][1]=true;
				}
			}
			if(j>=1){
				if(x[i][j-1]!=11){
					z[i][j][2]=true;
				}
			}
			if(i<n-1){
				if(x[i+1][j]!=11){
					z[i][j][3]=true;
				}
			}
			if(j<m-1){
				if(x[i][j+1]!=11){
					z[i][j][4]=true;
				}
			}
		}
	}
	queue<int>R;
	for(int i=1;i<=c;i++){
		memset(a,false,sizeof(a));
		for(int j=0;j<n;j++){
			for(int k=0;k<m;k++){
				y[j][k]=-1;
			}
		}
		while(!R.empty()){
			R.pop();
		}
		sum2=0;
		a[px[i-1]][py[i-1]]=true;
		if(z[px[i-1]][py[i-1]][1]==true){
			a[px[i-1]-1][py[i-1]]=true;
			y[px[i-1]-1][py[i-1]]=1;
			R.push((px[c-1]-1)*1000+py[c-1]);
			sum2++;
		}
		if(z[px[i-1]][py[i-1]][2]==true){
			a[px[i-1]][py[i-1]-1]=true;
			y[px[i-1]][py[i-1]-1]=1;
			R.push((px[c-1])*1000+(py[c-1]-1));
			sum2++;
		}
		if(z[px[i-1]][py[i-1]][3]==true){
			a[px[i-1]+1][py[i-1]]=true;
			y[px[i-1]+1][py[i-1]]=1;
			R.push((px[c-1]+1)*1000+py[c-1]);
			sum2++;
		}
		if(z[px[i-1]][py[i-1]][4]==true){
			a[px[i-1]][py[i-1]+1]=true;
			y[px[i-1]][py[i-1]+1]=1;
			R.push((px[c-1])*1000+(py[c-1]+1));
			sum2++;
		}
		d=0;
		while(y[px[i]][py[i]]==-1){
			sum3=0;
			d++;
			for(int j=1;j<=sum2;j++){
				f=R.front()/1000;
				g=R.front()%1000;
				if(z[f][g][1]==true && a[f-1][g]==false){
					a[f-1][g]=true;
					y[f-1][g]=d+1;
					R.push((f-1)*1000+g);
					sum3++;
				}
				if(z[f][g][2]==true && a[f][g-1]==false){
					a[f][g+1]=true;
					y[f][g+1]=d+1;
					R.push(f*1000+(g-1));
					sum3++;
				}
				if(z[f][g][3]==true && a[f+1][g]==false){
					a[f+1][g]=true;
					y[f+1][g]=d+1;
					R.push((f+1)*1000+g);
					sum3++;
				}
				if(z[f][g][4]==true && a[f][g+1]==false){
					a[f][g+1]=true;
					y[f][g+1]=d+1;
					R.push(f*1000+(g+1));
					sum3++;
				}
				R.pop();
			}
			sum2=sum3;
		}
		sum += y[px[i]][py[i]];
	}
	cout<<sum<<endl;
	return 0;
}