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 <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <array>
#include <stack>
#include <queue>
#include <chrono>
#include <numeric>

#pragma warning(disable:4996)
#define REP(i,n) for(int i = 0 ; i < n ; ++i)
typedef long long Int;

using namespace std;

struct point{
	Int j, i,step;
	point(Int i_, Int j_,Int s) :i(i_), j(j_),step(s){}
};

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

queue<point> que;
char tile[1001][1001];
int H, W;
bool ed[1001][1001];

point solve(char target)
{
	while (!que.empty()){
		point now(que.front());
		//ed[now.i][now.j] = true;
		que.pop();
		REP(i, 4){
			int x = now.j + dx[i];
			int y = now.i + dy[i];

			if (x < 0 || y < 0 || x > W || y > H || ed[y][x])
				continue;

			if (tile[y][x] == target){
				return point(y, x,now.step+1);
			}

			if (tile[y][x] != 'X'){
				que.push(point(y, x, now.step + 1));
				ed[y][x] = true;
			}
		}
	}
}

int main()
{
	cin.tie(0); ios::sync_with_stdio(false);
	//fstream fs("input.txt");
	////////////ここから開始///////////
	int n(0);
	cin >> H >> W >> n;

	REP(i, 1001)REP(j, 1001){ tile[i][j] = 'X'; ed[i][j] = false; }

	REP(i, H)REP(j, W)cin >> tile[i][j];

	REP(i, H)REP(j, W){ if (tile[i][j] == 'S')que.emplace(i, j, 0); }

	for (int i = 0; i < n; ++i){
		REP(i_1, H + 1)REP(j_1, W + 1){ ed[i_1][j_1] = false; }
		point t = solve('1' + i);
		que = {};
		que.emplace(t);
	}

	cout << que.front().step << endl;

	return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<int,int> P;
const int INF=1000000;
int H,W,N,d[1000][1000],ans=0;
int dx[]={-1,0,0,1};
int dy[]={0,-1,1,0};
char m[1000][1000];
pair<int,int> f[11];
int bfs(int sx,int sy,int gx,int gy){
	for(int i=0;i<H;i++)
		for(int j=0;j<W;j++)d[j][i]=INF;
	queue<P> que;
	que.push(P(sx,sy));
	d[sx][sy]=0;
	while(!que.empty()){
		P p=que.front();que.pop();
		if(p==P(gx,gy))break;
		for(int j=0;j<4;j++){
			int nx=p.first+dx[j];
			int ny=p.second+dy[j];
			if(0<=nx&&nx<W&&0<=ny&&ny<H&&m[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++)
		for(int j=0;j<W;j++){
			cin>>m[j][i];
			int num=m[j][i]-'0';
			if(m[j][i]=='S')f[0]=P(j,i);
			else if(0<num&&num<=N)f[num]=P(j,i);
		}
	for(int i=0;i<N;i++){
		ans+=bfs(f[i].first,f[i].second,f[i+1].first,f[i+1].second);
	}
	cout<<ans<<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];
    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();
    }
}