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;

#define MAX_DATA 100000
struct status {
	int health;
	int x;
	int y;
	int minutes;
};

queue<status> bfs;
bool checked[11][1001][1001];//health,x,y
//int lastnum[11] = {0,0,0,0,0,0,0,0,0,0};
int maxhp = 0;
///queue???push??????
///??¢??????????????\??????????????¨?????????status?????´?????????????????????
bool PushToQueue(status a) {
	/*for (int i = 0;i < lastnum[a.health];i++) {
		if (checked[i].health == a.health && checked[a.health][i].x == a.x && checked[a.health][i].y == a.y && checked[a.health][i].minutes == a.minutes){
			cout << "health:" << a.health << "/" << i << "/" << lastnum[a.health] << endl;
			return true;
		} 
	}
	checked[a.health][lastnum[a.health]++] = a;*/
	if (checked[a.health][a.x][a.y] == true) return false;
	if (a.health < maxhp) return false;
	bfs.push(a);
	if (a.health > maxhp) {
		maxhp = a.health;
		//cout << "maxhp has changed to " << maxhp << endl;
	}
	checked[a.health][a.x][a.y] = true;
	return true;
}

int main() {
	for (int a = 0;a < 11;a++) {
		for (int b = 0;b < 1000;b++) {
			for (int c = 0;c < 1000;c++) {
				checked[a][b][c] = false;
			}
		}
	}

	int H , W , N;

	cin >> H >> W >> N;
	char data[H][W];
	
	for (int i = 0;i < H;i++) {
		cin >> data[i];
	}
	//cout << "A" << endl;
	//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});
				goto getout;
			}
		}
	}


	getout:
	int result = 10000;
	for (;;) {
		if (bfs.empty()) break;
		struct status popped = bfs.front();
		bfs.pop();
		//cout << popped.health << "/" << popped.x << "/" << popped.y << "/" << popped.minutes << "/" << endl;
		if (popped.health == N+1) {
			if (popped.minutes < result) {
				result = popped.minutes;
			}
		}
		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 (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] == '.') {
						PushToQueue((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1});
					}
					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
							PushToQueue((struct status) {popped.health + 1,popped.x + x,popped.y + y,popped.minutes + 1});
						}
						else {
							//the mouse doesn't eat.
							PushToQueue((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1});
						}
					}
				}
			}
		}
	}
	cout << result << endl;
	return 0;
}

C++: #include "stdio.h"
#include "queue"
#include "utility"
using namespace std;
typedef pair<int, int>P;

queue<P>Q;
P now;
P goal[10];
int N;
int W;
int H;
char map[1000][1001] = {};
int x[4] = { 1,0,-1,0 };
int y[4] = { 0,1,0,-1 };
int dis[1000][1001] ;
int ans=0;
int box = 0;

int main() {
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 0; i < H; i++) {
		scanf("%s", map[i]);
	}
	//printf("\n");
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (map[i][j] >= '1'&&map[i][j] <= '9') {
				goal[map[i][j] - '0'].first = i;
				goal[map[i][j] - '0'].second = j;
				map[i][j] = '.';
			}
			else if (map[i][j] == 'S') {
				now.first = i;
				now.second = j;
				map[i][j] = '.';
			}
		}
	}
	for (int i = 1; i <= N; i++) {
		Q.push(now);
		//printf("%d %d????????¢?´¢\n", now.first, now.second);
		box = 0;
		for (int j = 0; j < H; j++) {
			for (int k = 0; k < W; k++) {
				dis[j][k] = 10000000;
			}
		}
		dis[Q.front().first][Q.front().second] = 0;
		while (!Q.empty()) {
			for (int j = 0; j < 4; j++) {
				if (map[Q.front().first + y[j]][Q.front().second + x[j]] == '.') {
					if (dis[Q.front().first + y[j]][Q.front().second + x[j]] > dis[Q.front().first][Q.front().second] + 1) {
						now.first = Q.front().first + y[j];
						now.second = Q.front().second + x[j];
						dis[now.first][now.second] = dis[Q.front().first][Q.front().second] + 1;
					//	printf("%d %d??????%d %d????????£???\n", Q.front().first, Q.front().second, now.first, now.second);
						Q.push(now);
						if (now.first == goal[i].first&&now.second == goal[i].second) {
						//	printf("hijiki\n");
							box = dis[now.first][now.second];
							while (!Q.empty()) {
								Q.pop();
							}
							if (Q.empty())break;
						}
					}
				}
			}
			//printf("hijiki\n");
			if (!Q.empty()) {
				Q.pop();
			}
			//printf("hijiki\n");
		}
		//printf("hijiki\n");
		ans += box;
	}
	printf("%d\n", ans);
	return 0;
}

C++: #include <iostream>  
#include <cstdio>  
#include <cstdlib>  
#include <queue>  
using namespace std;  
  
const int N = 1000;  
const int INF = 0x3f3f3f3f;  
  
int n, m, f;  
char map[N][N+1];  
  
bool legal(int x, int y, int k)  
{  
    return x >= 0 && x < n && y >= 0 && y < m  
        && map[x][y] != 'X';  
}  
  
void input()  
{  
    cin >> n >> m >> f;  
    for (int i = 0; i < n; i ++) {  
        scanf("%s", map[i]);  
    }  
}  
  
int BFS(int k)  
{  
    int i, j;  
    int d[N][N];  
    typedef pair<int, int> P;  
    queue <P> q;  
    for (i = 0; i < n; i ++) {  
        for (j = 0; j < m; j ++) {  
            d[i][j] = INF;  
            if ((k == 0 && map[i][j] == 'S') || map[i][j] == '0'+k) {  
                q.push(P(i, j));  
                d[i][j] = 0;  
                map[i][j] = '.';  
            }  
        }  
    }  
  
    while( q.size() ) {  
        P p = q.front();  
        q.pop();  
        int x = p.first;  
        int y = p.second;  
  
        if (map[x][y] == INF)  
            return d[x][y];  
  
        int pos[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};  
        for (i = 0; i < 4; i ++) {  
            int nx = x + pos[i][0];  
            int ny = y + pos[i][1];  
            if (legal(nx, ny, k) && d[nx][ny] == INF) {  
                q.push(P(nx, ny));  
                d[nx][ny] = d[x][y]+1;  
                if (map[nx][ny] == '0'+k+1) {  
                    return d[nx][ny];  
                }  
            }  
        }  
    }  
  
    return INF;  
}  
  
void solve()  
{  
    int res = 0;  
    for (int i = 0; i < f; i ++) {  
        res += BFS(i);  
    }  
    printf("%d\n", res);  
}  
  
int main(void)  
{  
    input();  
  
    solve();  
  
    return 0;  
}