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;
	int bx;
	int by;
};

queue<status> bfs;
//status checked[11][MAX_DATA];//
//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 (a.health < maxhp) return false;
	bfs.push(a);
	if (a.health > maxhp) maxhp = a.health;

	return true;
}

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] == '.') {
						PushToQueue((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
							PushToQueue((struct status) {popped.health + 1,popped.x + x,popped.y + y,popped.minutes + 1,popped.x,popped.y});
						}
						else {
							//the mouse doesn't eat.
							PushToQueue((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1,popped.x,popped.y});
						}
					}
				}
			}
		}
	}
	cout << result << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 1001;
static const ll MOD = 1000000007;

//--global--//

int inp[MAX][MAX];
int out[MAX][MAX];

//----------//

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

struct node{
    int x, y, cnt;
};

int main(int argc, const char * argv[]) {
    //提出時、消す----//
    //--------------//
    
    int h, w, n;
    cin >> h >> w >> n;
    pair<int, int> start;
    pair<int, int> next;
    
    rep(i, h){
        string str;
        cin >> str;
        rep(j, w){
            if(str[j] == '.'){
                inp[i][j] = 0;
            }
            else if(str[j] == 'X'){
                inp[i][j] = -1;
            }
            else if(str[j] == 'S'){
                inp[i][j] = 10;
                start = make_pair(i, j);
            }
            else{
                inp[i][j] = str[j]-'0';
                if(inp[i][j] == 1)
                    next = make_pair(i, j);
            }
        }
    }

    // rep(i, h){
    //     rep(j, w){
    //         if(j)cout << " ";
    //         cout << inp[i][j];
    //     }
    //     cout << endl;
    // }


    node now = {start.first, start.second, 0};
    int k = 1;
    queue<node> q;
    q.push({start.second, start.first, 0});

    while(q.size() > 0){
        now = q.front(); q.pop();
        //cout << now.x << ", "<< now.y << ", " << now.cnt << endl;
        rep(i, 4){
            pair<int, int> next = make_pair(now.x + dx[i], now.y + dy[i]);
            if(0 <= next.first && next.first < w && 0 <= next.second && next.second < h )
                if(out[next.second][next.first] == 0 && inp[next.second][next.first] != -1){
                    int a = now.cnt;
                    a++;
                    q.push({next.first, next.second, a});
                    out[next.second][next.first] = 1;
                }
        }
        // cout << now.x << ", " << now.y << endl;
        if(inp[now.y][now.x] == k){
            //cout << now.cnt << endl;
            if(k >= n){
                cout << now.cnt <<endl;
                break;
            }
            while(q.size()>0){
                q.pop();
            }
            q.push(now);
            rep(i, h){
                fill(out[i], out[i]+w, 0);
            }
            k++;
        }
    }

    //cout << "adsf" << endl;

    

    
    return 0;
}