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 <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <cstdio>
#include <string.h>
#include <cstring>

using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)

typedef long long ll;
//各地点の行と列をPairで持たせる
typedef pair<int,int> P;
int y,x,n,nx,ny,nextx,nexty;
int ans = 0;
int counter = 0;
int d[1000][1000] = {0};
char c[1100][1100];
int d1[] = {0,0,-1,1};
int d2[] = {1,-1,0,0};
int dc = 0;

int main(){
    cin >> y >> x >> n;
    queue<P> que;
    REP(i,y){
        cin >> c[i];
    }
    REP(i,y){
        bool isFoundStart = false;
        REP(j,x){
            if(c[i][j] == 'S'){
                isFoundStart = true;
                que.push(P(i,j));
                d[i][j] = 0;
                break;
            }
        }
        if(isFoundStart) break;
    }
   
    while(counter < n){
        int memo[1100][1100] = {0};
        while(que.size()){
            P p = que.front(); que.pop();
            ny = p.first;
            nx = p.second;
            dc = d[ny][nx];
 
            if(c[ny][nx] - '0' == counter + 1){
                counter++;
    
                if(counter == n) {
                    cout << dc << endl;
                    return 0;
                }
                queue<P> empty;
                swap(que, empty);
                que.push(P(ny,nx));
                c[ny][nx] = '.';
              
                break;
            }
        
            REP(i,4){
                nextx = nx + d1[i];
                nexty = ny + d2[i];

                if(nexty < 0 || nexty >= y || nextx < 0 || nextx >= x) continue;
                if(c[nexty][nextx] == 'X' || memo[nexty][nextx] != 0) continue;
            
                que.push(P(nexty,nextx));
     
                d[nexty][nextx] = dc + 1;
            
            }
            memo[ny][nx] = 1;
        }
    }
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
#define INF 100000000
using namespace std;

//firstにh、secondにwを入れる
typedef pair<int, int> PII;

//S、1...Nまでの座標を格納する
PII cheese[10];

const int MAX_H = 1010, MAX_W = 1010;
int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];

int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};

bool infield(int h, int w){
    return (0<=h && h<H && 0<=w && w<W);
}

//sからgまでの最短手数を返す
int bfs(PII s, PII g){
    queue<PII> que;
    //init
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++)
            d[i][j] = INF;
    }
    que.push(s);
    d[s.first][s.second] = 0;
    
    while(!que.empty()){
        PII p = que.front(); que.pop();
        if(p == g)
            break;
        for(int i=0; i<4; i++){
            int nh = p.first+dh[i];
            int nw = p.second+dw[i];
            
            if(infield(nh, nw) && field[nh][nw]!='X' && d[nh][nw]==INF){
                que.push(PII(nh, nw));
                d[nh][nw] = d[p.first][p.second] + 1;
            }
        }
    }
    
    return d[g.first][g.second];
}

void solve(){
    int ans = 0;
    
    //cheeseリストを作る
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            if(field[i][j] == 'S')
                cheese[0] = PII(i, j);
            
            for(char c='1'; c<='9'; c++){
                if(field[i][j] == c)
                    cheese[c-'0'] = PII(i, j);
            }
        }
    }
    for(int i=0; i<N; i++)
        ans += bfs(cheese[i], cheese[i+1]);
    printf("%d\n", ans);
}

int main(){
    scanf("%d%d%d", &H, &W, &N);
    for(int i=0; i<H; i++)
        scanf("%s", field[i]);
    solve();
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<typeinfo>
using namespace std;
char field[1001][1001],kazu[9];
int ans[1001][1001]={100000},H,W,N,cou,acou,num=1,lkazu[9];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
struct data{
    int x,y;
};
queue <data> q;
int main(){
    data now;
    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'){
                ans[i][j]=0;
                data start;
                start.x=j,start.y=i;
                q.push(start);
            }
            if(field[i][j]=='1')kazu[1]='1';
            if(field[i][j]=='2')kazu[2]='2';
            if(field[i][j]=='3')kazu[3]='3';
            if(field[i][j]=='4')kazu[4]='4';
            if(field[i][j]=='5')kazu[5]='5';
            if(field[i][j]=='6')kazu[6]='6';
            if(field[i][j]=='7')kazu[7]='7';
            if(field[i][j]=='8')kazu[8]='8';
            if(field[i][j]=='9')kazu[9]='9';
        }
    }
    for(int i=0;i<N;i++){
        while(!q.empty()){
            now=q.front();
            q.pop();
            if(ans[now.y][now.x]!=0){
                if(ans[now.y][now.x]!=100000||field[now.y][now.x]=='X')continue;
            }
            if(field[now.y][now.x]==kazu[i+1]){
                cout<<1;
                cou=ans[now.y][now.x];
                break;
            }
            for(int j=0;j<4;j++){
            ans[now.y][now.x]=min(ans[now.y][now.x],ans[now.y+dy[j]][now.x+dx[j]])+1;
            }
            for(int j=0;j<4;j++){
                if(now.x+dx[i]>W-1||now.x+dx[i]<0||now.y+dy[i]>H-1||now.y+dy[i]<0)continue;
                now.x=now.x+dx[i],now.y=now.y+dy[i];
                q.push(now);
            }
        }
        fill(ans[0],ans[0]+1000*1000,100000);
        q.push(now);
        ans[now.x][now.y]=0;
        acou+=cou+1;
        cou=0;
        num++;
    }
     for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cout<<ans[i][j];
        }
        cout<<endl;
     }
    cout<<acou<<endl;
}