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 <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
 
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge { LL to, cost; };
 
LL H, W, N;
std::string field[1123];
LL Sx, Sy, xy[11];
LL ans;
 
LL d[1123456];
std::vector<edge>G[1123456];
void dijkstra(LL s)
{
    std::priority_queue<P, std::vector<P>, std::greater<P>>que;
    std::fill(d, d + H * W + 1, INF);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty())
    {
        P p = que.top(); que.pop();
        LL v = p.second;
        if (d[v] < p.first) { continue; }
        rep(i, G[v].size())
        {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost)
            {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}
 
int main()
{
    try {
        in >> H >> W >> N;
        rep(i, H) { in >> field[i]; }
 
        rep(h, H)
        {
            rep(w, W)
            {
                if (field[h][w] == 'X') { continue; }
                if (field[h][w] == 'S') { Sx = h; Sy = w; }
                if (field[h][w] != '.') { xy[field[h][w] - '0'] = h*W + w; }
                if(false)if (h < H - 1)
                {
                    if (field[h + 1][w] != 'X')
                    {
                        G[h*W + w].push_back({ (h + 1)*W + w,1 });
                        G[(h + 1)*W + w].push_back({ h*W + w,1 });
                    }
                }
                if(false)if (w < W - 1)
                {
                    if (field[h][w + 1] != 'X')
                    {
                        G[h*W + w].push_back({ h*W + (w + 1),1 });
                        G[h*W + (w + 1)].push_back({ h*W + w,1 });
                    }
                }
            }
        }
        //dijkstra(Sx*W + Sy);
        //rep(i, N)
        //{
        //  ans += d[xy[i + 1]];
        //  dijkstra(xy[i + 1]);
        //}
 
        out << ans << std::endl;
        return 0;
    }
    catch (std::bad_alloc) { return 0; }
    catch (...) { return 0; }
}

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

#define INF 999999999
#define MAX_X 1000
#define MAX_Y 1000
#define MAX_HEALTH 10
struct status {
	int health;
	int x;
	int y;
	int minutes;
};

queue<status> bfs;
bool checked[MAX_HEALTH+1][MAX_X+1][MAX_Y+1];//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 = INF;
	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<queue>
using namespace std;

typedef pair<int,int> P;

const int MAX = 1000, INF = 100000000;
char map[MAX][MAX+1];
int H, W, N;
int sx, sy;
int gx, gy;
int steps;
int d[MAX][MAX];

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

int bfs(){
	for(int i = 0; i < H; i++)
		for(int j = 0; j < W; j++) d[i][j] = INF;
	
	queue<P> que;
	que.push(P(sx,sy));
	d[sx][sy] = 0;
	
	while(que.size()){
		P p = que.front(); que.pop();
		
		if(p.first == gx && p.second == gy) break;
		
		for(int i = 0; i < 4; i++){
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if(nx >= 0 && nx < H && ny >= 0 && ny < W && map[nx][ny] != 'X' 
			&& d[nx][ny] == INF){
				que.push(P(nx,ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
		
	return d[gx][gy];
}

int main(){
	cin >> H >> W >> N;
	for(int i = 0; i < H; i++)
		cin >> map[i];
	
	steps = 0;
	int n = 0;
	while(n < N){
		for(int i = 0; i < H; i++)
			for(int j = 0; j < W; j++){
				if((map[i][j] == 'S' && n == 0) || map[i][j] == n + '0'){
					sx = i; sy = j;
				}
				if(map[i][j] == n + 1 + '0'){
					gx = i; gy = j;
				}
			}
		
		steps += bfs();
		n++;
	}
	cout << steps << endl;
	return 0;
}