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 <stdio.h>

bool syo[1000][1000];
int zi[1000][1000];
int kox[1000000];
int koy[1000000];
int kon;
int koxs[1000000];
int koys[1000000];
int kons;
int cx[10];
int cy[10];
int sx,sy;

int main()
{
	int h,w,n;
	scanf("%d %d %d",&h,&w,&n);
	for(int i = 0; i < h; i++)
	{
		char ww[1111];
		scanf("%s",ww);
		for(int ii = 0; ii < w; ii++)
		{
			if('1' <= ww[ii] && ww[ii] <= '9')
			{
				int d = ww[ii] - '1';
				cy[d] = i;
				cx[d] = ii;
			}
			if(ww[ii] == 'S')
			{
				sy = i;
				sx = ii;
			}
			if(ww[ii] == 'X')
			{
				syo[i][ii] = true;
			}
			else
			{
				syo[i][ii] = false;
			}
		}
	}
	int time = 0;
	int nx = sx;
	int ny = sy;
	for(int k = 0; k < n; k++)
	{
		for(int i = 0; i < h; i++)
		{
			for(int ii = 0; ii < w; ii++)
			{
				zi[i][ii] = 9999;
			}
		}
		zi[ny][nx] = 0;
		kox[0] = nx;
		koy[0] = ny;
		kon = 1;
		while(1)
		{
			kons = 0;
			for(int i = 0; i < kon; i++)
			{
				if(syo[koy[i]][kox[i]])
				{
					continue;
				}
				if(kox[i] == cx[k] && koy[i] == cy[k])
				{
					goto aa;
				}
				if(kox[i] > 0)
				{
					if(zi[koy[i]][kox[i] - 1] > zi[koy[i]][kox[i]])
					{
						zi[koy[i]][kox[i] - 1] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i] - 1;
						koys[kons] = koy[i];
						kons++;
					}
				}
				if(kox[i] < w - 1)
				{
					if(zi[koy[i]][kox[i] + 1] > zi[koy[i]][kox[i]])
					{
						zi[koy[i]][kox[i] + 1] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i] + 1;
						koys[kons] = koy[i];
						kons++;
					}
				}
				if(koy[i] > 0)
				{
					if(zi[koy[i] - 1][kox[i]] > zi[koy[i]][kox[i]])
					{
						zi[koy[i] - 1][kox[i]] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i];
						koys[kons] = koy[i] - 1;
						kons++;
					}
				}
				if(koy[i] < h - 1)
				{
					if(zi[koy[i] + 1][kox[i]] > zi[koy[i]][kox[i]])
					{
						zi[koy[i] + 1][kox[i]] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i];
						koys[kons] = koy[i] + 1;
						kons++;
					}
				}
			}
			for(int i = 0; i < kons; i++)
			{
				kox[i] = koxs[i];
				koy[i] = koys[i];
			}
			kon = kons;
		}
aa:
		time += zi[cy[k]][cx[k]];
		nx = cx[k];
		ny = cy[k];
	}
	printf("%d\n",time);
	return 0;
}

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,100000);
    //ans[start.x][start.y]=0;
    data now,a;
    for(int i=0;i<N;i++){
        while(!q.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=q.front();
            q.pop();
            if(ans[now.x][now.y]!=100000||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];
                q.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,100000);
        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 <cstdio>
#include <cstring>
#include <utility>
#include <queue>
using namespace std;

#define INF (1<<30)

typedef pair<int,int> P;

queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int H,W,N,sx,sy,sum=0;

int main() {
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++) {
		scanf("%s",map[i]);
		char *ptr=strchr(map[i],'S');
		if(ptr!=NULL) sx=ptr-map[i],sy=i;
	}
	que.push(P(sx,sy));
	for(int i=1;i<=N;i++) {
		for(int j=0;j<H;j++) {
			for(int k=0;k<W;k++) {
				vst[j][k]=INF;
			}
		}
		vst[sy][sx]=0;
		while(!que.empty()) {
			P p=que.front();
			que.pop();
			if(map[p.second][p.first]==i+'0') {
				sum+=vst[p.second][p.first];
				while(!que.empty()) que.pop();
				que.push(P(p.first,p.second));
				sx=p.first,sy=p.second;
				break;
			}
			for(int j=0;j<4;j++) {
				int nx=p.first+dx[j],ny=p.second+dy[j];
				if(map[ny][nx]!='X'&&vst[ny][nx]==INF) {
					vst[ny][nx]=vst[p.second][p.first]+1;
					que.push(P(nx,ny));
				}
			}
		}
	}
	printf("%d\n",sum);
	return 0;
}