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 <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define INF 0xfffffff
typedef long long int lli;
typedef pair<lli,lli> P;
lli ans,sgx[10],sgy[10],w,h,n,dtmp[1000][1000],d[1000][1000],sx,sy,gx,gy,nx,ny;
lli dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char s[1001];
queue <P> que;
int main()
{
	scanf("%lld %lld %lld",&h,&w,&n);
	for(int i=0;i<h;i++){
		scanf("%s",s);
		for(int j=0;j<w;j++){
			if(s[j]=='X') dtmp[i][j]=-1;
			if(s[j]=='.') dtmp[i][j]=INF;
			if('1'<=s[j]&&s[j]<='9'){
				dtmp[i][j]=INF;
				sgx[s[j]-'0']=j;
				sgy[s[j]-'0']=i;
			}
			if(s[j]=='S'){
				dtmp[i][j]=INF;
				sgx[0]=j;
				sgy[0]=i;
			}
		}
	}
	for(int i=0;i<n;i++){
		while(!que.empty()) que.pop();
		for(int j=0;j<h;j++) for(int k=0;k<w;k++) d[j][k]=dtmp[j][k];
		sx=sgx[i];
		sy=sgy[i];
		gx=sgx[i+1];
		gy=sgy[i+1];
		d[sy][sx]=0;
		que.push(P(sx,sy));
		while(1){
			P p=que.front();
			que.pop();
			nx=p.first;
			ny=p.second;
			for(int j=0;j<4;j++){
				if(0<=nx+dx[j]&&0<=ny+dy[j]&&nx+dx[j]<w&&ny+dy[j]<h&&d[ny+dy[j]][nx+dx[j]]>d[ny][nx]+1){
					d[ny+dy[j]][nx+dx[j]]=d[ny][nx]+1;
					if(nx+dx[j]==gx&&ny+dy[j]==gy){
						ans+=d[ny+dy[j]][nx+dx[j]];
						goto next;
					}
					que.push(P(nx+dx[j],ny+dy[j]));
				}
			}
		}
		next: ;
	}
	printf("%lld\n",ans);
}

C++: #include <iostream>
#include <queue>

using namespace std;

struct pos{
		int x,y;
	pos(int _x,int _y){x=_x;y=_y;}
};

int a[1000][1000],m[1000][1000],sh,sw;

int  ans(int p,int q,int h,int w,int count);

void map(int H,int W){
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cout << m[i][j] << " ";
		}
		cout<<"\n";
	}
	cout<<"\n";
}

int main(){
	int H,W,count,d,T;
	char b;
	cin>>H;
	cin>>W;
	cin>>d;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin>>b;
			if('1'<=b&&b<='9'){
				a[i][j]=b-48;
			}else if(b=='X'){
				a[i][j]=-1;
			}else if(b=='S'){
				sh=i;
				sw=j;
				a[i][j]=0;
			}else if(b=='.'){
				a[i][j]=0;
			}
		}
	}
	for(T=0,count=0;count<d;count++){
		//map(H,W);
		T+=ans(sh,sw,H,W,count);
	}
	//map(H,W);
	cout<<T<<endl;
	return 0;
}

int ans(int p,int q,int h,int w,int count){
	int gx,gy;
	//cout<<sh<<" "<<sw<<"\n";
	int dy[4]={-1,0,0,1},dx[4]={0,-1,1,0};
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			m[i][j]=-1;
			if(a[i][j]==count+1){
				gx=i;
				gy=j;
			}
		}
	}
	sh=gx;
	sw=gy;
	m[p][q]=0;
	queue<pos> Q;
	Q.push(pos(p,q));
	while(Q.size()){
		//cout<<Q.size()<<" \n";
		pos b=Q.front();
		Q.pop();
		for(int i=0;i<=3;i++){
			if(0<=b.x+dx[i]&&b.x+dx[i]<h&&0<=b.y+dy[i]&&b.y+dy[i]<w&&m[b.x+dx[i]][b.y+dy[i]]==-1){
				if(a[b.x+dx[i]][b.y+dy[i]]!=-1){
					Q.push(pos(b.x+dx[i],b.y+dy[i]));
					m[b.x+dx[i]][b.y+dy[i]]=m[b.x][b.y]+1;
				}
			}
		}
	}
	return m[gx][gy];
}

C++: #include <cstdio>
#include <utility>
#include <queue>
#include <cstdlib>
using namespace std;

int H, W, N;
char field[1001][1001];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;

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

void bfs()
{
    for (int n = 1; n < N + 1; n++)
    {
        queue<pair<int, int>> que;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                d[i][j] = INF;
            }
        }
        que.push(start);
        d[start.first][start.second] = 0;

        while (que.size())
        {
            pair<int, int> p = que.front();
            que.pop();
            if (field[p.first][p.second] == '0' + (char)n)
            {
                cheese = p;
                break;
            }

            for (int i = 0; i < 4; i++)
            {
                int nx = p.first + dx[i];
                int ny = p.second + dy[i];

                if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' && d[nx][ny] == INF)
                {
                    que.push(pair<int, int>(nx, ny));
                    d[nx][ny] = d[p.first][p.second] + 1;
                }
            }
        }
        result += d[cheese.first][cheese.second];
        start = cheese;
    }
}

int main() {
    scanf(" %d %d %d", &H, &W, &N);
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            scanf(" %c", &field[i][j]);
            if (field[i][j] == 'S')
            {
                start.first = i;
                start.second = j;
            }
        }
    }
    bfs();
    printf("%d\n", result);
}= 0; i < 4; i++)
            {
                int nx = p.first + dx[i];
                int ny = p.second + dy[i];

                if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' && d[nx][ny] == INF)
                {
                    que.push(pair<int, int>(nx, ny));
                    d[nx][ny] = d[p.first][p.second] + 1;
                }
            }
        }
        result += d[cheese.first][cheese.second];
        start = cheese;
    }
}

int main() {
    scanf(" %d %d %d", &H, &W, &N);
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            scanf(" %c", &field[i][j]);
            if (field[i][j] == 'S')
            {
                start.first = i;
                start.second = j;
            }
        }
    }
    bfs();
    printf("%d\n", result);
}