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<cstdio>
#include<queue>
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)

using namespace std;


struct P{
    int x;
    int y;
};

int w,h,n;
const int INF = 1e+8;
char feld[1010][1010];
int d[1010][1010];
int sx,sy,gx,gy;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans;
P che[15];


void bfs(){
    REP(i,n){
        P sp=che[i];
        sx=che[i].x;
        sy=che[i].y;
        gx=che[i+1].x;
        gy=che[i+1].y;
        REP(y,h)REP(x,w)d[y][x]=INF;
        queue<P> que;
        que.push(sp);
        d[sy][sx]=0;
        while(!que.empty()){
            P p=que.front();
            que.pop();
            int x=p.x;
            int y=p.y;
            if(x==gx&&y==gy){
                ans+=d[gy][gx];
                break;
            }
            REP(i,4){
                int nx=x+dx[i];
                int ny=y+dy[i];
                if(nx<0||nx>=w||ny<0||ny>=h)continue;
                if(feld[ny][nx]!='X'&&d[ny][nx]==INF){
                    P next;
                    next.x=nx;
                    next.y=ny;
                    que.push(next);
                    d[ny][nx]=d[y][x]+1;
                }
            }
        }
    }
}

int main(){
    cin>>h>>w>>n;
    REP(y,h){
        REP(x,w){
            scanf(" %c",&feld[y][x]);
            if(feld[y][x]=='S'){
                che[0].x=x;
                che[0].y=y;
            }else if(feld[y][x]>='1'&&feld[y][x]<='9'){
                char c=feld[y][x];
                che[c-'0'].x=x;
                che[c-'0'].y=y;
            }
        }
    }
    bfs();
    cout<<ans<<endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> P;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int H,W,n;
int res=0;
char chee[105][105];
int d[105][105];
void bfs(int x,int y,char ch)
{
    memset(d,INF,sizeof(d));
    d[x][y]=0;
    queue<P> que;
    que.push(P(x,y));
    while(!que.empty()) {
        P p=que.front();
        que.pop();
        if(chee[p.first][p.second]==ch) {
            res+=d[p.first][p.second];
            break;
        }
        for(int i=0;i<4;i++) {
            int nx=p.first+dx[i];
            int ny=p.second+dy[i];
            if(nx<0||nx>=H||ny<0||ny>=W) continue;
            if(d[nx][ny]==INF&&chee[nx][ny]!='X') {
                que.push(P(nx,ny));
                d[nx][ny]=d[p.first][p.second]+1;
            }

        }

    }

}
void rec(char ch)
{
    for(int i=0;i<H;i++) {
        for(int j=0;j<W;j++) {
            if(chee[i][j]==ch) {
                bfs(i,j,ch+1);
                return;
            }
        }
    }
}
int main()
{
    cin>>H>>W>>n;
    for(int i=0;i<H;i++)
        scanf("%s",chee[i]);
    res=0;
    for(int i=0;i<H;i++) {
        for(int j=0;j<W;j++) {
            if(chee[i][j]=='S') {

                bfs(i,j,'1');
                break;
            }
        }
    }
    for(int k=1;k<n;k++) {
        rec(k+'0');
    }

    printf("%d\n",res);

    return 0;
}

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

struct status {
	int health;
	int x;
	int y;
	int minutes;
	int bx;
	int by;
};

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[y][x] == 'S') {
				bfs.push((struct status) {1,x,y,0,-100,-100});
				goto getout;
			}
		}
	}
	getout:
	int result;
	for (;;) {
		if (bfs.empty()) break;
		struct status popped = bfs.front();
		bfs.pop();
		//cout << popped.health << "/" << popped.x << "/" << popped.y << "/" << popped.minutes << "/" << popped.bx << "/" << popped.by << endl;
		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(x != 0 && y != 0) continue;
					if (popped.x + x <0 || popped.x + x >= W || popped.y + y < 0 || popped.y + y >= H) continue;
					if (popped.x + x == popped.bx && popped.y + y == popped.by) continue;
					
					if (data[popped.y + y][popped.x + x] == 'S'||data[popped.y + y][popped.x + x] == 'X') continue;
					else if (data[popped.y + y][popped.x + x] == '.') {
						bfs.push((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1,popped.x,popped.y});
					}
					else {
						//cout << "*****************************factory" << endl;
						int factory = data[popped.y + y][popped.x + x] -48 ;
						//cout << data[popped.y + y][popped.x + x]<< endl;
						if (factory == popped.health) {
							//eat
							bfs.push((struct status) {popped.health + 1,popped.x + x,popped.y + y,popped.minutes + 1,popped.x,popped.y});
						}
						else {
							//the mouse doesn't eat.
							bfs.push((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1,popped.x,popped.y});
						}
					}
				}
			}
		}
	}
	cout << result << endl;
	return 0;
}