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<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct data{
    int x,y;
};
queue <data> q;
data start,scop;
char field[1001][1001];
int ans[1001][1001],cop[1001][1001],H,W,N,cou,acou,s;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int main(){
    cin>>H>>W>>N;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin>>field[i][j];
            if(field[i][j]=='S'){
                //data start;
                start.x=i,start.y=j;
                //q.push(start);
            }
            if(field[i][j]=='1')cop[i][j]=1;
            if(field[i][j]=='2')cop[i][j]=2;
            if(field[i][j]=='3')cop[i][j]=3;
            if(field[i][j]=='4')cop[i][j]=4;
            if(field[i][j]=='5')cop[i][j]=5;
            if(field[i][j]=='6')cop[i][j]=6;
            if(field[i][j]=='7')cop[i][j]=7;
            if(field[i][j]=='8')cop[i][j]=8;
            if(field[i][j]=='9')cop[i][j]=9;
            if(field[i][j]=='X')cop[i][j]=-1;
        }
    }
    fill(ans[0],ans[0]+1001*1001,10000);
    //ans[start.x][start.y]=0;
    data now,a;
    for(int i=0;i<N;i++){
        queue <data> que;
        que.push(start);
        while(!que.empty()){
        /*for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cout<<ans[i][j];
                }
            cout<<endl;
        }*/
            //cout<<ans[start.x][start.y]<<endl;
            now=que.front();
            que.pop();
            if(ans[now.x][now.y]!=10000||field[now.x][now.y]=='X')continue;
            /*if(cop[now.x][now.y]==i+1){
                q.empty();
                q.push(now);
                break;
            }*/
            for(int j=0;j<4;j++){
            if(ans[now.x+dx[j]][now.y+dy[j]]>=0&&now.x+dx[j]<1000&&now.x+dx[j]>=0&&now.y+dy[j]<1001&&now.y+dy[j]>=0)
                s=min(ans[now.x][now.y],ans[now.x+dx[j]][now.y+dy[j]])+1;
                //cout<<ans[now.x][now.y]<<" "<<now.x+dx[j]<<" "<<now.y+dy[j]<<endl;
                ans[now.x][now.y]=min(ans[now.x][now.y],s);
                //cout<<ans[now.x][now.y]<<endl;
            }
            if(cop[now.x][now.y]==i+1){
                //while (!q.empty()) q.pop();
                //q.push(now);
                break;
            }
            for(int j=0;j<4;j++){
                if(now.x+dx[j]>W-1&&now.x+dx[j]<0&&now.y+dy[j]>H-1&&now.y+dy[j]<0)continue;
                a.x=now.x+dx[j],a.y=now.y+dy[j];
                que.push(a);
            }
            ans[start.x][start.y]=0;
        }
        /*for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cout<<ans[i][j];
            }
            cout<<endl;
        }*/
        cou+=ans[now.x][now.y];
        fill(ans[0],ans[0]+1001*1001,10000);
        start.x=now.x,start.y=now.y;
        //ans[now.x][now.y]=0;
    }
    /*for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cout<<ans[i][j];
        }
        cout<<endl;
    }*/
    cout<<cou<<endl;
return 0;
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

#define MAX 1000
int y[MAX][MAX];
int x[MAX][MAX];
int px[10];
int py[10];
bool z[MAX][MAX][5];
bool a[MAX][MAX];
int sum2;
int sum3;
int main(){
	memset(z,false,sizeof(z));
	char T[12]={'.','1','2','3','4','5','6','7','8','9','S','X'};
	int n,m,c,d,sum,f,g;
	string S;
	sum=0;
	cin>>n>>m>>c;
	for(int i=0;i<n;i++){
		cin>>S;
		for(int j=0;j<m;j++){
			for(int k=0;k<12;k++){
				if(S[j]==T[k]){
					x[i][j]=k;
					if(k==10){
						px[0]=i;
						py[0]=j;
					}
					if(k>=1 && k<=9){
						px[k]=i;
						py[k]=j;
					}
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			y[i][j]=-1;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(i>=1){
				if(x[i-1][j]!=11){
					z[i][j][1]=true;
				}
			}
			if(j>=1){
				if(x[i][j-1]!=11){
					z[i][j][2]=true;
				}
			}
			if(i<n-1){
				if(x[i+1][j]!=11){
					z[i][j][3]=true;
				}
			}
			if(j<m-1){
				if(x[i][j+1]!=11){
					z[i][j][4]=true;
				}
			}
		}
	}
	queue<int>R;
	for(int i=1;i<=c;i++){
		memset(a,false,sizeof(a));
		for(int j=0;j<n;j++){
			for(int k=0;k<m;k++){
				y[j][k]=-1;
			}
		}
		while(!R.empty()){
			R.pop();
		}
		sum2=0;
		a[px[i-1]][py[i-1]]=true;
		if(z[px[i-1]][py[i-1]][1]==true){
			a[px[i-1]-1][py[i-1]]=true;
			y[px[i-1]-1][py[i-1]]=1;
			R.push((px[i-1]-1)*1000+py[i-1]);
			sum2++;
		}
		if(z[px[i-1]][py[i-1]][2]==true){
			a[px[i-1]][py[i-1]-1]=true;
			y[px[i-1]][py[i-1]-1]=1;
			R.push((px[i-1])*1000+(py[i-1]-1));
			sum2++;
		}
		if(z[px[i-1]][py[i-1]][3]==true){
			a[px[i-1]+1][py[i-1]]=true;
			y[px[i-1]+1][py[i-1]]=1;
			R.push((px[i-1]+1)*1000+py[i-1]);
			sum2++;
		}
		if(z[px[i-1]][py[i-1]][4]==true){
			a[px[i-1]][py[i-1]+1]=true;
			y[px[i-1]][py[i-1]+1]=1;
			R.push((px[i-1])*1000+(py[i-1]+1));
			sum2++;
		}
		d=0;
		while(y[px[i]][py[i]]==-1){
			sum3=0;
			d++;
			for(int j=1;j<=sum2;j++){
				f=R.front()/1000;
				g=R.front()%1000;
				if(z[f][g][1]==true && a[f-1][g]==false){
					a[f-1][g]=true;
					y[f-1][g]=d+1;
					R.push((f-1)*1000+g);
					sum3++;
				}
				if(z[f][g][2]==true && a[f][g-1]==false){
					a[f][g-1]=true;
					y[f][g-1]=d+1;
					R.push(f*1000+(g-1));
					sum3++;
				}
				if(z[f][g][3]==true && a[f+1][g]==false){
					a[f+1][g]=true;
					y[f+1][g]=d+1;
					R.push((f+1)*1000+g);
					sum3++;
				}
				if(z[f][g][4]==true && a[f][g+1]==false){
					a[f][g+1]=true;
					y[f][g+1]=d+1;
					R.push(f*1000+(g+1));
					sum3++;
				}
				R.pop();
			}
			sum2=sum3;
		}
		sum += y[px[i]][py[i]];
	}
	cout<<sum<<endl;
	return 0;
}