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>
struct cheese{
    int x,y;
};
using namespace std;
int map[1001][1001],walk[1001][1001];
int main(void){
    int X,Y,sx,sy,gx,gy,xx,yy,N,Ncnt=1,i,i2,ans=0,bx[4]={1,-1,0,0},by[4]={0,0,1,-1};
    char nyuryoku[1001][1001];
    queue<cheese> jotai;
    cheese ima,tugi;
    cin>>Y>>X>>N;
    for(i=1;i<=Y;i++){
        for(i2=1;i2<=X;i2++){
            cin>>nyuryoku[i2][i];
        }
    }
    for(i=1;i<=Y;i++){
        for(i2=1;i2<=X;i2++){
            switch(nyuryoku[i2][i]){
                case 'S':
                    map[i2][i]=0;
                    sx=i2;
                    sy=i;
                    break;
                case 'X':
                    map[i2][i]=-1;
                    break;
                case '.':
                    map[i2][i]=0;
                    break;
                case '1':
                    map[i2][i]=1;
                    break;
                case '2':
                    map[i2][i]=2;
                    break;
                case '3':
                    map[i2][i]=3;
                    break;
                case '4':
                    map[i2][i]=4;
                    break;
                case '5':
                    map[i2][i]=5;
                    break;
                case '6':
                    map[i2][i]=6;
                    break;
                case '7':
                    map[i2][i]=7;
                    break;
                case '8':
                    map[i2][i]=8;
                    break;
                case '9':
                    map[i2][i]=9;
                    break;
            }
        }
    }
    while(Ncnt<=N){
        for(i=1;i<=Y;i++)for(i2=1;i2<=X;i2++)walk[i2][i]=-1;
        walk[sx][sy]=0;
        ima.x=sx;
        ima.y=sy;
        jotai.push(ima);
        while(!jotai.empty()){
            ima=jotai.front();
            jotai.pop();
            for(int I=0;I<4;I++){
                xx=ima.x+bx[I];
                yy=ima.y+by[I];
                //cout<<"xx="<<xx<<" yy="<<yy<<endl<<map[ima.x][ima.y]<<endl;
                if(map[ima.x][ima.y]==Ncnt){
                    gx=ima.x;
                    gy=ima.y;
                }
                else if(xx>0&&yy>0&&xx<=X&&yy<=Y&&((walk[xx][yy]>walk[ima.x][ima.y]+1)||walk[xx][yy]==-1)&&map[xx][yy]!=-1){
                    tugi.x=xx;
                    tugi.y=yy;
                    walk[xx][yy]=walk[ima.x][ima.y]+1;
                    /*
                    for(i=1;i<=Y;i++){
                        for(i2=1;i2<=X;i2++){
                            if(walk[i2][i]>-1)cout<<' ';
                                cout<<walk[i2][i]<<' ';
                            }
                        cout<<endl;
                    }
                    cout<<endl;
                    */
                    jotai.push(tugi);
                }
            }
        }
        ans+=walk[gx][gy];
        sx=gx;
        sy=gy;
        Ncnt++;
    }
    cout<<ans<<endl;
    /*for(i=1;i<=Y;i++){
        for(i2=1;i2<=X;i2++){
            if(map[i2][i]>-1)cout<<' ';
            cout<<map[i2][i]<<' ';
        }
        cout<<endl;
    }*/
}

C++: #include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;

char in[1001][1002];
char done[1001][1002];
int dist[1001][1002];
int H,W,N;
int main() {
	int a=0,b=0;
	cin >> H >> W >> N;
	for(int i=1;i<=H;i++){
		cin >> (in[i]+1);
		for(int j=1;j<=W;j++){
			if(in[i][j]=='S'){
				a=i;
				b=j;
			}
		}
	}
	int ret=0;
	for(int t=1;t<=N;t++){
		queue<P> q;
		q.push(P(a,b));
		done[a][b]=t;
		dist[a][b]=0;
		bool found=false;
		while(!found){
			int i=q.front().first;
			int j=q.front().second;
			q.pop();
			int dx[]={0,-1,0,1};
			int dy[]={-1,0,1,0};
			for(int k=0;k<4;k++){
				int x=i+dx[k];
				int y=j+dy[k];
				if(in[x][y]!=0&&in[x][y]!='X'&&done[x][y]<t){
					done[x][y]=t;
					dist[x][y]=dist[i][j]+1;
					q.push(P(x,y));
					if(in[x][y]==t+'0'){
						a=x;
						b=y;
						ret+=dist[x][y];
						found=true;
						break;
					}
				}
			}
		}
	}
	cout << ret << endl;

}

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

int H, W, N;
char field[1000][1000];
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);
}