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 <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

typedef long long ll;

using namespace std;

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

int main(){
    int h,w,n,ans=0;
    bool fl[1000][1000]={0};
    char in[1000][1001];
    queue<pair<int,int> > qp;
    pair<int,int> p;
    
    scanf("%d%d%d",&h,&w,&n);
    
    for(int i=0;i<h;i++)
        scanf("%s",in[i]);
    
    for(int i=0;i<h;i++)
        for(int j=0;j<w;j++)
            if(in[i][j]=='S'){
                qp.push(make_pair(i,j));
                qp.push(make_pair(-5,-5));
                break;
            }
    
    for(int i=1;i<=n;i++){
        while(1){
            p=qp.front();
            qp.pop();
            if(p.first==-5 && p.second==-5){
                ans++;
                qp.push(make_pair(-5,-5));
                continue;
            }
            if(p.first<0 || h<=p.first || p.second<0 || w<=p.second || fl[p.first][p.second]) continue;
            if(in[p.first][p.second]=='0'+i) break;
            if(in[p.first][p.second]=='X') continue;
            
            for(int j=0;j<4;j++)
                qp.push(make_pair(p.first+mo[j],p.second+mo[(j+1)%4]));
            
        }
        while(!qp.empty())
            qp.pop();
        qp.push(make_pair(p.first,p.second));
        qp.push(make_pair(-5,-5));
    }
            
    printf("%d\n",ans);
    
    return 0;
}

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

struct status {
	int health;
	int x;
	int y;
	int minutes;
};
queue<status> bfs;

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

	cin >> H >> W >> N;
	char data[H][W];
	
	for (int i = 0;i < H;i++) {
		cin >> data[i];
	}
	
	//Find 'S'
	for (int y = 0;y < H;y++) {
		for (int x = 0;x < W;x++) {
			if(data[x][y] == 'S') {
				bfs.push((struct status) {0,x,y,0});
				goto getout;
			}
		}
	}
	getout:
	int result;
	for (;;) {
		if (bfs.empty()) break;
		struct status popped = bfs.front();
		bfs.pop();
		
		if (popped.health == N+1) {
			result = popped.minutes;
			break;
		}
		else {
			for (int x = -1;x < 2;x++) {
				for (int y = -1;y < 2;y++) {
					if (popped.x + x <0 || popped.x + x >= W || popped.y + y < 0 || popped.y + y >= H) continue;
					
					if (data[y][x] == 'S'||data[y][x] == 'X') continue;
					else if (data[y][x] == '.') {
						bfs.push((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1});
					}
					else {
						int factory = data[y][x];
						if (factory == popped.health + 1) {
							//eat
							bfs.push((struct status) {popped.health + 1,popped.x + x,popped.y + y,popped.minutes + 1});
						}
						else {
							//the mouse doesn't eat.
							bfs.push((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1});
						}
					}
				}
			}
		}
	}
	cout << result << endl;
	return 0;
}

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

int H, W, N;
int po[10][2];
int ans = 0;
bool cit[1000][1000];
int tow[1000][1000];
int movx[4] = {0, 1, 0, -1}, movy[4] = {1, 0, -1, 0};

bool isinside(int a, int b){
    return (a >= 0 || b >= 0 || a < H || b < W);
}

int diss(int sx, int sy, int gx, int gy){
    queue<int> que;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            tow[i][j] = 1000001;
        }
    }
    tow[sx][sy] = 0;
    que.push(sx);
    que.push(sy);
    while(que.size() >= 2){
        int ax = que.front();
        que.pop();
        int ay = que.front();
        que.pop();
        int ad = tow[ax][ay];
        for(int ii = 0; ii < 4; ii++){
            int i = movx[ii], j = movy[ii];
            if(isinside(ax + i, ay + j)){
                if(cit[ax + i][ay + j] && tow[ax + i][ay + j] > ad + 1){
                    tow[ax + i][ay + j] = ad + 1;
                    que.push(ax + i);
                    que.push(ay + j);
                }
                if(ax + i == gx && ay + j == gy){
                    return ad + 1;
                }
            }
        }
    }
}

int main(){
    cin >> H >> W >> N;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            char a;
            cin >> a;
            if(a == '.'){
                cit[i][j] = true;
            }else if(a =='X'){
                cit[i][j] = false;
            }else if(a == 'S'){
                cit[i][j] = true;
                po[0][0] = i;
                po[0][1] = j;
            }else{
                cit[i][j] = true;
                int aa = a - '0';
                po[aa][0] = i;
                po[aa][1] = j;
            }
        }
    }
    for(int i = 0; i < N; i++){
        int an = diss(po[i][0], po[i][1], po[i + 1][0], po[i + 1][1]);
        ans += an;
    }
    cout << ans << endl;
    return 0;
}

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

const int INF = 1<<30;
const int maxn = 1000+10;

int w,h,n;
char mp[maxn][maxn];
int d[maxn][maxn];

int dir[4][2] = {
	{-1,0},
	{1,0},
	{0,1},
	{0,-1},
};

int fac[10][2];

int bfs(int sx,int sy,int gx,int gy){
	memset(d,-1,sizeof(d));

	queue<pair<int,int> > Q;
	Q.push(make_pair(sx,sy));
	d[sx][sy] = 0;
	while(!Q.empty()){
		pair<int,int> p = Q.front(); Q.pop();
		if(p.first==gx && p.second==gy) break;

		for(int i=0; i<4; i++){
			int tx = p.first + dir[i][0];
			int ty = p.second + dir[i][1];
			if(tx>=0 && tx<h && ty>=0 && ty<w && mp[tx][ty]!='X' && d[tx][ty]==-1){
				Q.push(make_pair(tx,ty));
				d[tx][ty] = d[p.first][p.second] + 1;
			}
		}
	}

	return d[gx][gy];
}

int main(){
	scanf("%d%d%d",&h,&w,&n);	
	for(int i=0; i<h; i++)
		for(int j=0; j<w; j++)
			cin >> mp[i][j];

	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			if(mp[i][j] == 'S'){
				fac[0][0] = i;
				fac[0][1] = j;
				mp[i][j] = '.';
			}else if(isdigit(mp[i][j])){
				int index = mp[i][j] - '0';
				fac[index][0] = i;
				fac[index][1] = j;
			}
		}
	}

	int step = 0;
	for(int i=0; i<n; i++){
		step += bfs(fac[i][0],fac[i][1],fac[i+1][0],fac[i+1][1]);
	}

	printf("%d\n",step);
}