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>
#include<set>
using namespace std;

#define INF 20000000

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

typedef pair<int, int> P;

int main() {
	char map[1002][1002];
	static int smp[1002][1002];
	int h, w, n, c=0, x, y, ans=0;
	queue<P> q;
	cin >> h >> w >> n;
	for(int i=0; i<h+2; ++i) for(int j=0; j<w+2; ++j) {
		map[i][j] = 'X'; smp[i][j] = INF;
	}
	for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
		cin >> map[i][j]; smp[i][j] = INF;
		if(map[i][j]=='S') {x = j; y = i;}
	}
	q.push(P(x, y)); smp[y][x] = 0;
	while(1) {
		x = q.front().first;
		y = q.front().second;
		q.pop();
		if(map[y][x]=='1'+c) {
			ans += smp[y][x]; c++;
			if(c==n) break;
			while(!q.empty()) q.pop();
			for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) smp[i][j] = INF;
		}
		for(int i=0; i<4; ++i) {
			if(map[y+dy[i]][x+dx[i]]!='X'&&smp[y+dy[i]][x+dx[i]]==INF) {
				q.push(P(x+dx[i], y+dy[i]));
				smp[y+dy[i]][x+dx[i]] = smp[y][x]+1;
			}
		}
	}
	cout << smp[y][x] << endl;
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#define N 1005
#define INFINITY 2000000000
using namespace std;
void bfs();
int h,w,n,num[N][N],d[N*N],M[N*N][N*N],u,m;
string color[N*N],in[N*N];
int main(){
  int i,j,k;
  cin >> h >> w >> n;
  for(i=0;i<h+2;i++)
    for(j=0;j<w+2;j++)
      num[i][j]=-1;
  for(i=1;i<=h;i++) cin >> in[i],in[i]='X'+in[i];
  k=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      if(in[i][j]!='X') num[i][j]=k,k++;
    }
  }
  m=max(h,w);
  for(i=0;i<=m;i++)
    for(j=0;j<=m;j++)
      M[i][j]=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      if(num[i][j]!=-1&&num[i+1][j]!=-1)
	M[num[i][j]][num[i+1][j]]=M[num[i+1][j]][num[i][j]]=1;
      if(num[i][j]!=-1&&num[i][j+1]!=-1)
	M[num[i][j]][num[i][j+1]]=M[num[i][j+1]][num[i][j]]=1;
    }
  }
  bfs();
  return 0;
}
void bfs(){
  int i,j,p=1,t=0,flag=0,x=1;
  char start='S',goal='1';
  queue<int> q;
  while(1){
    if(flag==1) start=x+'0',goal=x+'0'+1,x++;
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	color[num[i][j]]="WHITE",d[num[i][j]]=INFINITY;
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==start) color[num[i][j]]="GLAY",d[num[i][j]]=0,q.push(num[i][j]);
      }
    }
    while(q.empty()!=1){
      u=q.front();
      q.pop();
      for(i=1;i<=h;i++){
	for(j=1;j<=w;j++){
	  if(M[u][num[i][j]]&&color[num[i][j]]=="WHITE"){
	    color[num[i][j]]="GLAY";
	    d[num[i][j]]=d[u]+1;
	    q.push(num[i][j]);
	  }
	}
      }
      color[u]="BLACK";
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==goal) t+=d[num[i][j]];
      }
    }
    flag=1;
    if(x>n) break;
  }
  cout << t << endl;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll = long long;
const ll INF = 1e9;

typedef pair<int, int> P;
int H, W, N;
vector<P> F(10);
string M[1001];

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

ll bfs(P ps, P pg) {
    ll d[1001][1001];
    queue<P> que;
    for(int i = 0; i < H; i++) for (int j = 0; j < W; j++) d[i][j] = INF;

    que.push(ps);
    d[ps.second][ps.first] = 0;

    while(que.size()) {
        P p = que.front(); que.pop();
        if (p.first == pg.first && p.second == pg.second) break;

        for (int i = 0; i < 4; i++) {
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if (M[ny][nx] != 'X' && nx >= 0 && nx < W && ny >= 0 && ny < H && d[ny][nx] == INF) {
                que.push(P(nx, ny));
                d[ny][nx] = d[p.second][p.first] + 1;
            }
        }
    }

    return d[pg.second][pg.first];
}

int main() {
    ll ans = 0;
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        cin >> M[i];
        for (int k = 0; k < W; k++) {
            if (M[i][k] == 'S') {
                F[0] = P(k, i);
            }
            if (isdigit(M[i][k])) {
                F[M[i][k] - '0'] = P(k, i);
            }
        }
    }

    for (int i = 0; i < N; i++) {
        ans += bfs(F[i], F[i+1]);
    }

    cout << ans << endl;

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
#define MAX 1005

int h, w, n;
char city[MAX][MAX];
struct node{
	int x,y,time;
} start;
int direc1[4] = { -1,0,1,0 }, direc2[4] = { 0,1,0,-1 };
map<int,int>visited;

void dfs(char end) {
	queue<node> que;
	que.push(start);
	visited[start.x*w+start.y]=1;
	int nextx,nexty;
	while (que.size()!=0)
	{
		node now=que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			nextx=now.x + direc1[i];
			nexty=now.y+direc2[i];
			if (nextx >= 0 && nextx< h&&nexty>= 0 && nexty < w && city[nextx][nexty]!='X' && !visited[nextx*w+nexty]) {	

				if (city[nextx][nexty]==end)
				{
					start.x=nextx;
					start.y=nexty;
					start.time=now.time+1;
					return ;
				}
				else{
					visited[nextx*w+nexty]=1;
					node now1;
					now1.x=nextx;
					now1.y=nexty;
					now1.time=now.time+1;
					que.push(now1);
				}
			}
		}
	}

}



int main() {
	cin >> h >> w >> n;	
	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			cin >> city[i][j];
			if (city[i][j] == 'S') {
				start.x= i;
				start.y = j;
				start.time=0;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		visited.clear();
		dfs(i+'1');
	}
	cout << start.time << endl;
	//system("pause");
	return 0;
}