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>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int h,w;
    char n;
    cin >> h >> w >> n;
    char map[h][w];
    long long int did[h][w]={};
    long long 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};
    long long count=0;
    while(q.size()){
        //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;*/
        long long MIN=10000000000000000;
        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();
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

int h, w, n;
char maze[1001][1001];
int sx, sy;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int d[1001][1001];

void bfs(int x, int y, char point)
{
	queue<pair<int, int>> que;
	que.push({x, y});

	d[x][y] = 0;

	while (que.size()){
		pair<int, int> p = que.front();
		que.pop();

		if (maze[p.first][p.second] == point){
			sx = p.first, sy = p.second;
			return;
		}

		for (int i = 0; i < 4; i++){
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if (0 <= nx and nx < h and 0 <= ny and ny < w and maze[nx][ny] != 'X' and d[nx][ny] == -1){
				que.push({nx, ny});
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
}
int main()
{
	cin >> h >> w >> n;
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			cin >> maze[i][j];
			if (maze[i][j] == 'S'){
				sx = i;
				sy = j;
			}
		}
	}

	long long ans = 0;
	memset(d, -1, sizeof(d));
	bfs(sx, sy, '1');
	ans += d[sx][sy];

	for (int i = 2; i < n+1; i++){
		memset(d, -1, sizeof(d));
		bfs(sx, sy, char(i+'0'));
		ans += d[sx][sy];
	}

	cout << ans << endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <queue>

char map[1000][1000];
int check[1000][1000];
int cx[10];
int cy[10];

int main(){
	int i,j,k,l,h,w,ch,pd=0;
	int tex,tey,ted;
	std::queue<int> qx;
	std::queue<int> qy;
	std::queue<int> qd;

	scanf("%d %d %d",&h,&w,&ch);
	scanf("%c",&map[0][0]);
	for(i=0;i<h;i++){
		for(j=0;j<=w;j++){
			scanf("%c",&map[i][j]);
			if(map[i][j]=='S'){cy[0]=i; cx[0]=j;}
			if(map[i][j]=='1'){cy[1]=i; cx[1]=j;}
			if(map[i][j]=='2'){cy[2]=i; cx[2]=j;}
			if(map[i][j]=='3'){cy[3]=i; cx[3]=j;}
			if(map[i][j]=='4'){cy[4]=i; cx[4]=j;}
			if(map[i][j]=='5'){cy[5]=i; cx[5]=j;}
			if(map[i][j]=='6'){cy[6]=i; cx[6]=j;}
			if(map[i][j]=='7'){cy[7]=i; cx[7]=j;}
			if(map[i][j]=='8'){cy[8]=i; cx[8]=j;}
			if(map[i][j]=='9'){cy[9]=i; cx[9]=j;}
		}
	}
	for(i=0;i<h;i++){
		for(j=0;j<=w;j++)check[i][j]=0;
	}

	i=0;
	while(i<ch){
		qx.push(cx[i]); qy.push(cy[i]); qd.push(0);
		++i;
		while(!qx.empty()){
			tex=qx.front(); tey=qy.front(); ted=qd.front();
			printf("%d,%d,%d\n",tex,tey,ted);
			qx.pop(); qy.pop(); qd.pop();
			check[tey][tex]=1;
			if(tey==cy[i] && tex==cx[i]){
				pd+=ted;
				while(!qx.empty())qx.pop();
				while(!qy.empty())qy.pop();
				while(!qd.empty())qd.pop();
				for(k=0;k<h;k++){
					for(l=0;l<=w;l++)check[k][l]=0;
				}
				printf("%d\n",pd);
				break;
			}
			++ted;
			if(tex+1<w && map[tey][tex+1]!='X' && check[tey][tex+1]==0){
				qx.push(tex+1); qy.push(tey); qd.push(ted);
			}
			if(tex-1>=0 && map[tey][tex-1]!='X' && check[tey][tex-1]==0){
				qx.push(tex-1); qy.push(tey); qd.push(ted);
			}
			if(tey+1<h && map[tey+1][tex]!='X' && check[tey+1][tex]==0){
				qx.push(tex); qy.push(tey+1); qd.push(ted);
			}
			if(tey-1>=0 && map[tey-1][tex]!='X' && check[tey-1][tex]==0){
				qx.push(tex); qy.push(tey-1); qd.push(ted);
			}
			check[tey][tex+1]=1;
			check[tey][tex-1]=1;
			check[tey+1][tex]=1;
			check[tey-1][tex]=1;
		}
	}

	printf("%d\n",pd);
	return 0;
}