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 <vector>
#include <string>
#include <utility>
#include <cstring>
#include <queue>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
typedef pair<int,int> pii;

int cost[1100][1100];

int main() {
	int H, W, N;
	cin >> H >> W >> N;
	vector<string> s;
	s.push_back(string(W+2,'X'));
	rep(i,H) {
		string t;
		cin >> t; s.push_back("X" + t + "X");
	}
	s.push_back(string(W+2,'X'));

	int res = 0;
	rep(i,N) {
		queue<pii> q;
		memset(cost, -1, sizeof(cost));
		rep(j,H+2) {
			rep(k,W+2) {
				if(s[j][k] == (i ? i+'0' : 'S')) {
					q.push(pii(j,k));
					cost[j][k] = 0;
				}
			}
		}
		while(q.size()) {
			pii a = q.front(); q.pop();
			const int dxy[] = {1,0,-1,0,1};
			if(s[a.fi][a.se] == i+'1')
				res += cost[a.fi][a.se];
			rep(j,4) {
				int y = a.fi + dxy[j];
				int x = a.se + dxy[j+1];
				if(s[y][x] != 'X' && !(cost[y][x]+1)) {
					cost[y][x] = cost[a.fi][a.se]+1;
					q.push(pii(y,x));
				}
			}
		}
	}
	cout << res << endl;
}

C++: #include <iostream>
#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <utility>
using namespace std;
typedef pair<int, int> P;
char maze[1005][1005];
int d[1005][1005];
int dirx[4] = {0, -1, 0, 1};
int diry[4] = {1, 0, -1, 0};
int h, w, n, hard; 
int si, sj;
const int INF = 0x3f3f3f3f;
int tot = 0; 

void BFS(int ai, int aj){
    queue<P> que;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            d[i][j] = INF;
        }
    }
    que.push(P(ai, aj));
    d[ai][aj] = 0;
    while(!que.empty()){
        P a = que.front();
        que.pop();
        int x = a.first, y = a.second;
        if(maze[x][y]-'0' == hard){
            tot += d[x][y];
            si = x; sj = y;
            break;
        }
        for(int i = 0; i < 4; i++){
            int dx = x+dirx[i], dy = y+diry[i];
            if(dx>=0 && dx<h && dy>=0 && dy<w && maze[dx][dy]!='X' && d[dx][dy]==INF){
                d[dx][dy] = d[x][y]+1;
                que.push(P(dx, dy));
            }
        }
    }
}

int main(int argc, char const *argv[]){
    while(scanf("%d%d%d", &h, &w, &n) != EOF){
        getchar();
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                scanf("%c", &maze[i][j]);
                if(maze[i][j] == 'S'){
                    si = i; sj = j;
                }
            }
            getchar();
        }
        tot = 0;
        hard = 1;
        for(int i = 0; i < n; i++){
            BFS(si, sj);
            hard++;
        }
        cout << tot << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	int h,w;
	char n;
	cin >> h >> w >> n;
	char map[h][w];
	int did[h][w]={};
	int va[h][w]={};
	queue <pair <int ,int > > q;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin >> map[i][j];
			if(map[i][j]=='S'){
				did[i][j]=1;
				q.push(make_pair(i,j));
			}
			if(map[i][j]=='X')
				did[i][j]=1;
		}
	}
	char N='1';
	int dx[4]={0,1,0,-1};
	int dy[4]={1,0,-1,0};
	int count=0;
	int s=10;
	while(q.size()){

		s--;
		//cout << "------------------------------------------------------" << endl;
		/*for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cout << did[i][j];
			}
			cout << endl;
		}
		cout << "q.front().first=" << q.front().first << " q.front().second=" << q.front().second << endl;
		cout << map[q.front().first][q.front().second] << endl;*/
		int MIN=10000;
		for(int i=0;i<4;i++){
			if(q.front().first + dy[i]<0||h<=q.front().first + dy[i]||q.front().second + dx[i]<0||w<=q.front().second + dx[i]);
			else if(va[q.front().first+dy[i]][q.front().second+dx[i]]!=0){
				va[q.front().first][q.front().second]= min(MIN,va[q.front().first+dy[i]][q.front().second+dx[i]]);
				MIN = va[q.front().first][q.front().second];
			}
		}
		va[q.front().first][q.front().second]++;
		if(map[q.front().first][q.front().second]==N){
			//cout << "******************************" << endl;
			if(N==n){
				count+=va[q.front().first][q.front().second];
				//cout << va[q.front().first][q.front().second] << endl;
				cout << count-1 << endl;
				break;
			}
			else{
				count+=va[q.front().first][q.front().second];
				//cout << va[q.front().first][q.front().second] << endl;
				//cout <<"count=" <<count << endl;
				N++;
				q.push(make_pair(q.front().first,q.front().second));
				q.push(make_pair(q.front().first,q.front().second));
				while(q.size()-1)
					q.pop();
				for(int i=0;i<h;i++){
					for(int j=0;j<w;j++){
						va[i][j]=0;
						did[i][j]=0;
						if(map[i][j]=='X')
							did[i][j]=1;
					}
				}
				//cout << q.front().first << " " << q.front().second << endl;
				did[q.front().first][q.front().second]=1;
			}
		}

		for(int i=0;i<4;i++){
			if(q.front().first + dy[i]<0||h<=q.front().first + dy[i]||q.front().second + dx[i]<0||w<=q.front().second + dx[i]);
			else if(did[q.front().first + dy[i]][q.front().second + dx[i]]!=1){
				q.push(make_pair(q.front().first+dy[i],q.front().second+dx[i]));
				did[q.front().first + dy[i]][q.front().second + dx[i]]=1;
			}
		}
		q.pop();
	}
}