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 <queue>
#include <climits>

#define N 1001
using namespace std;


char a[N][N];
int c[N][N];

int w,h,n;

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

char cz[10] = {'0','1','2','3','4','5','6','7','8','9'};

queue<int> xque;
queue<int> yque;


int main(){
	
	
	
		
		
		scanf(" %d",&h);
		scanf(" %d",&w);
        scanf(" %d",&n);
		
		
		
		
		int si=0,sj=0;
		for(int j=0;j<h;j++){
			for(int i=0;i<w;i++){
				scanf(" %c",&a[i][j]);
				if(a[i][j] == 'S'){
					si=i; sj=j;
				} 
			}
		}
		
		int ans=0;
        for(int p=1;p<=n;p++){
            for(int i=0;i<w;i++){
			 for(int j=0;j<h;j++){
			    c[i][j] = 100000000;
			 }
		    }
            while(xque.size()){
                xque.pop();
                yque.pop();
            }
            xque.push(si);
            yque.push(sj);
            c[si][sj] = 0;
            while(xque.size()){
                int i = xque.front(); xque.pop();
                int j = yque.front(); yque.pop();
                
                if(a[i][j] == cz[p]){
                    ans += c[i][j];
                    si = i; sj = j;
                    break;
                }
                
                for(int k=0;k<4;k++){
                    int nx = i+dx[k];
                    int ny = j+dy[k];
                    int tm = c[i][j]+1;
                    if((0<=nx)&&(nx<w)&&(0<=ny)&&(ny<h)&&(a[nx][ny]!='X')&&(c[nx][ny]>tm)){
                        xque.push(nx);
                        yque.push(ny);
                        c[nx][ny] = tm;
                    }		
                }
                
            }
        }
		
		printf("%d\n", ans);
	
	
	
	
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
#include <map>

#define p(s) puts("s")

using namespace std;
//from は残りのチーズ数
int bfs(int from);
//訪れたら1
typedef pair<int, int> P;
char atlas[1000][1000] = {0};

int h, w, n;
//int start_i , start_j;
int dj[] = {0,1,0,-1 };
int di[] = {-1,0,1,0 };

int main(int argc, char const* argv[])
{
    //行数 一行の長さ 工場の数
    cin >> h >> w >> n;
    //atlas配列初期化&start位置取得
    for (int i = 0; i < h; i++) 
    {
        for (int j = 0; j < w; j++) 
        {
            cin >> atlas[i][j];
        }
    }

    int sum = 0;
    for(int i=0; i<n; i++)
    {
        //cout << "--------------------------------------------------" << endl;
        //cout << "call bfs(" << i << ")" << endl;
        sum += bfs(i);
        //cout << "bfs(" << i << ")  = " << bfs(i) << endl;
    }
    cout << sum << endl;
    return 0;
}



//チーズsからs+1までの最短経路を返す関数
int bfs(int s)
{
    //cout << "enter bfs(" << s << ")"  << endl;

    //スタートを取得
    P start;
    for(int i=0; i<h; i++)
    {
        for(int j=0; j < w; j++)
        {
            // s==0  0=>S
            if (s == 0 && atlas[i][j] == 'S' )          { start = P(i,j);} //cout<< "start = "<<  i << ", " <<j <<endl; }
            else if (s != 0 && atlas[i][j] == '0' + s)  { start = P(i,j);} //cout<< "start = "<<  i << ", " <<j <<endl; }
        }
    }

    //is_visit初期化
    int is_visit[1000][1000] = {0};
    queue<P> q;
    //map初期化
    map<P, int> dis;

    //一つ入れる
    q.push(start);
    dis[start] = 0;

    while( q.size() )
    {
        //一つ取り出す
        P now = q.front(); q.pop();

        //終了判定
        if(atlas[now.first][now.second] == '0'+s+1)  {
            //cout << "end of bfs(" << s << ")" << "now = (" << now.first <<" ,"<<now.second <<")"<< endl; 
            return dis[now];
        }

        //分岐
        for(int i=0; i < 4; i++) {
            P next = P(now.first + di[i], now.second + dj[i]);
            // 訪れてないか　枠の中か 障害物は無いか
            if( -1 < next.first && next.first < h && 
                -1 < next.second && next.second < w && 
                dis.find(next) == dis.end() &&
                atlas[next.first][next.second] != 'X'
                )
            {
                q.push(next);
                dis[next] = dis[now] + 1;
            }
        }
    }
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int m[1002][1002]={0},p[1002][1002]={0};
struct r{
	int g,g2;
	unsigned long long l;
};
int main(){
	int h,w,n,i,j,a,b[2],l2=1;
	b[0]=1;
	b[1]=-1;
	char c[2001];
	r gl;
	queue<r> q;
	cin>>h>>w>>n;
	for(i=0;i<=h+1;i++){
		m[i][0]=-1;
		m[i][w+1]=-1;
	}
	for(i=0;i<=w+1;i++){
		m[0][i]=-1;
		m[h+1][i]=-1;
	}
	for(i=1;i<=h;i++){
		scanf("%s",c);
		for(j=1;j<=w;j++){
			a=c[j-1];
			if(a>=49&&a<=57){m[i][j]=a-48;}
			if(c[j-1]=='X'){m[i][j]=-1;}
			if(c[j-1]=='S'){
				gl.g=i;
				gl.g2=j;
				gl.l=0;
				q.push(gl);
			}
		}
	}
	while(1){
		gl=q.front();
		q.pop();
		if(m[gl.g][gl.g2]==l2){
			l2++;
			if(l2==n+1){break;}
			while(!q.empty()){q.pop();}
		}
		gl.l++;
		p[gl.g][gl.g2]=l2;
		for(i=0;i<2;i++){
			if(m[gl.g+b[i]][gl.g2]!=-1&&p[gl.g+b[i]][gl.g2]!=l2){
				p[gl.g+b[i]][gl.g2]=l2;
				gl.g+=b[i];
				q.push(gl);
				gl.g-=b[i];
			}
			if(m[gl.g][gl.g2+b[i]]!=-1&&p[gl.g][gl.g2+b[i]]!=l2){
				p[gl.g][gl.g2+b[i]]=l2;
				gl.g2+=b[i];
				q.push(gl);
				gl.g2-=b[i];
			}
		}

	}
	cout<<gl.l<<endl;
	return 0;
}