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;
typedef pair<int, int> P;
const int MAX_HW = 1000;
int n, h, w, rat, s[2], g[2], flag[MAX_HW+1][MAX_HW+1][2];
int town[MAX_HW][MAX_HW];
char town2[MAX_HW][MAX_HW];
queue <P> que;
P p;
void bfs(int sy, int sx){
	que.push(P(sy, sx));
	flag[sy][sx][0]=rat;
	while (que.size()){
	RE:
		p = que.front();
		que.pop();
		for (int i = -1; i <= 1; i++){
			for (int j = -1; j <= 1; j++){
				if (i == 0 || j == 0){
					if (town2[p.first + i][p.second + j] != 'X'&&flag[p.first + i][p.second + j][0] != rat&&p.first + i < h&&p.first + i >= 0 && p.second + j < w&&p.second + j >= 0) {
							flag[p.first+i][p.second+j][0] = rat;
							flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
							if (town[p.first + i][p.second + j] == rat){
							if (rat == n) return;
							rat++;
							flag[p.first+i][p.second+j][0] = rat;
							while (que.size()){
								que.pop();
							}
							que.push(P(p.first + i, p.second + j));
							goto RE;
						}
						else{
						que.push(P(p.first + i, p.second + j));
						}
					}
				}
			}
		}
	}
}
int main(){
	cin >> h >> w >> n;
	rat = 1;
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			cin >> town2[i][j];
			town[i][j] = town2[i][j] - 48;
			if (town2[i][j] == 'S'){
				s[0] = i;
				s[1] = j;
			}
			if (town[i][j] == n){
				g[0] = i;
				g[1] = j;
			}
		}
	}
	bfs(s[0], s[1]);
	cout << flag[g[0]][g[1]][1] << endl;
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define INF 0x7f7f7f7f
#define PI acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define lowbit(x)   (x)&(-x)
#define Mid(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define CLR(arr, val)    memset(arr, val, sizeof(arr))

using namespace std;

char map[1024][1024];
bool vis[1024][1024];
int cur, h, w;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

struct node {
	int x, y, step;
} sta, now;

bool check(int x, int y) {
	if (x >= 0 && x < h && y >= 0 && y < w && !vis[x][y] && map[x][y] != 'X')	return true;
	return false;
}



void bfs(node s) {
	memset(vis, 0, sizeof(vis));
	
	queue<node> que;
	que.push(s);
	while (!que.empty()) {
		now = que.front(); que.pop();
		vis[now.x][now.y] = true;
		
		node next;
		next.step = now.step + 1;
		for (int i = 0; i < 4; i++) {
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];

			if (check(next.x, next.y)) {
				if (map[next.x][next.y] == cur + '0') {
					map[next.x][next.y] = '.';
					sta = next;
					return ;
				}
				que.push(next);
			}
		}
	}
}

int main() {
	int n;
	while (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')	sta.x = i, sta.y = j, map[i][j] = '.';
			}
		}
		sta.step = 0;
		for (cur = 1; cur <= n; cur++) {
			bfs(sta);
		}
		cout << sta.step << endl;
	}
	return 0;
}

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

typedef pair<int,int> P;

int h,w,n;
char m[1000][1000];
int visited[1000][1000];

int main(){
	cin>>h>>w>>n;

	int pos;
	for(int y=0;y<h;y++){
		for(int x=0;x<w;x++){
			cin>>m[x][y];
			if(m[x][y]=='S') pos=x+y*1000;
		}
	}

	memset(visited,0,sizeof(visited));
	int ans=0;
	for(int now=1;now<=n;now++){
		queue<P> q;
		q.push(make_pair(pos,0));
		while(1){
			pos=q.front().first;
			int x=pos%1000;
			int y=pos/1000;
			int cost=q.front().second;
			q.pop();
			if(visited[x][y]==now || m[x][y]=='X') continue;
			if(m[x][y]==('0'+now)){
				ans+=cost;
				break;
			}
			visited[x][y]=now;
			if(0<x) q.push(make_pair(pos-1,cost+1));
			if(x+1<w) q.push(make_pair(pos+1,cost+1));
			if(0<y) q.push(make_pair(pos-1000,cost+1));
			if(y+1<h) q.push(make_pair(pos+1000,cost+1));
		}
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include <queue>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

#define INF (1 << 26)

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

int main()
{
	int H, W, N;

	cin >> H >> W >> N;

	vector<string> D(H);

	for (int i = 0; i < H; i++)
		cin >> D[i];

	int route = 0;

	for (int i = 1; i <= N; i++)
	{
		queue<pair<int, int> > que;

		vector<vector<int> > memo(H, vector<int>(W, INF));

		char start;

		if (i == 1)
		{
			start = 'S';
		}
		else
		{
			start = i + 48;
		}

		pair<int, int> start_;

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (D[i][j] == start)
				{
					start_.first = j;
					start_.second = i;
				}
			}
		}

		que.push(start_);

		memo[start_.second][start_.first] = route;

		while (que.size())
		{
			pair<int, int> place = que.front(); que.pop();

			int x, y;

			x = place.first;
			y = place.second;

			for (int dir = 0; dir < 4; dir++)
			{
				if (0 <= x + dx[dir] && x + dx[dir] < W && 0 <= y + dy[dir] && y + dy[dir] < H)
				{
					if (D[y + dy[dir]][x + dx[dir]] != 'X' && memo[y + dy[dir]][x + dx[dir]] == INF)
					{
						place.first = x + dx[dir];
						place.second = y + dy[dir];

						que.push(place);

						memo[y + dy[dir]][x + dx[dir]] = memo[y][x] + 1;

						if (D[y + dy[dir]][x + dx[dir]] - 48 == i)
						{
							route += memo[y][x] + 1;

							goto Exit;
						}
					}
				}
			}
		}

	Exit:;
	}

	cout << route << endl;

	return 0;
}