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 = 100000000;
	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 <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
int h, w, n;
char field[1000][1000];
bool visited[10][1000][1000];
struct state {
	int i, j, d;
};

bool out(int i, int j) {
	return i < 0 || h <= i || j < 0 || w <= j;
}

int search(int i, int j, int num) {
	if(num == n + 1) return 0;
	int sum = 0;
	queue<state> q;
	q.push({ i, j, 0 });
	while(!q.empty()) {
		state s = q.front(); q.pop();
		if(out(s.i, s.j) || field[s.i][s.j] == 'X' || visited[num][s.i][s.j]) continue;
		if(field[s.i][s.j] == '0' + num) {
			sum += s.d;
			break;
		}
		visited[num][s.i][s.j] = true;
		int dir[] = { 1, 0, -1, 0, 1 };
		for(int k = 0; k < 4; k++) {
			if(!visited[num][s.i + dir[k]][s.j + dir[k + 1]])
				q.push({ s.i + dir[k], s.j + dir[k + 1], s.d + 1 });
		}
	}
	for(int ii = 0; ii < h; ii++) {
		for(int jj = 0; jj < w; jj++) {
			if(field[ii][jj] == '0' + num) {
				sum += search(ii, jj, num + 1);
			}
		}
	}
	return sum;
}


int main() {
	cin >> h >> w >> n;
	for(int i = 0; i < h; i++) {
		cin >> field[i];
	}
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			if(field[i][j] == 'S') {
				cout << search(i, j, 1) << endl;
				return 0;
			}
		}
	}

}

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

typedef std::pair<int, int> Pair;

constexpr int MAX_HW = 1000;
constexpr int MAX_N = 9;
constexpr int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int H, W, N;
int sx[MAX_N], sy[MAX_N], gx[MAX_N], gy[MAX_N];
char map[MAX_HW][MAX_HW];

int bfs(const int& n);

int main(){
    scanf("%d%d%d", &H, &W, &N);
    
    for(int i = 0; i < H; ++i){
        scanf("\n");
        for(int j = 0; j < W; ++j){
            scanf("%c", &map[j][i]);
            if(map[j][i] == 'S'){
                sx[0] = j, sy[0] = i;
            }
            if(map[j][i] >= '1' && map[j][i] < (N + '0')){
                sx[map[j][i] - '0'] = j, sy[map[j][i] - '0'] = i;
                gx[map[j][i] - '0' - 1] = j, gy[map[j][i] - '0' - 1] = i;
            }
            if(map[j][i] == (N + '0')){
                gx[N - 1] = j, gy[N - 1] = i;
            }
        }
    }
    
    int ans = 0;
    for(int i = 0; i < N; ++i){
        ans += bfs(i);
    }
    
    printf("%d\n", ans);
    
    return 0;
}

int bfs(const int& n){
    int dis[MAX_HW][MAX_HW];
    memset(dis, -1, sizeof(dis));
    dis[sx[n]][sy[n]] = 0;
    
    std::queue<Pair> que;
    que.push(Pair(sx[n], sy[n]));
    
    while(que.size()){
        Pair p = que.front(); que.pop();
        
        if(p.first == gx[n] && p.second == gy[n]){
            break;
        }
        
        for(int i = 0; i < 4; ++i){
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if(nx >= 0 && ny >= 0 && nx < W && ny < H && map[nx][ny] != 'X' && dis[nx][ny] == -1){
                que.push(Pair(nx, ny));
                dis[nx][ny] = dis[p.first][p.second] + 1;
            }
        }
    }
    
    return dis[gx[n]][gy[n]];
}