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>

using namespace std;

const int INF = 100000;
const int MAX_N = 1004;
const int MAX_M = 1004;
char field[MAX_N][MAX_M];
int n, m, k, dis[MAX_N][MAX_M];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

struct state {
    int x, y;
} p[12];

int bfs(int sx, int sy, int gx, int gy) {
    for (int i = 0; i<n; i++)
        for (int j = 0; j<m; j++)
            dis[i][j] = INF;
    queue<state>que;
    state start;
    start.x = sx, start.y = sy;
    dis[start.x][start.y] = 0;
    que.push(start);
    while (que.size()) {
        state cur = que.front();
        que.pop();
        if (cur.x == gx && cur.y == gy)return dis[cur.x][cur.y];
        for (int i = 0; i < 4; i++) {
            int nx = cur.x + dx[i], ny = cur.y + dy[i];
            if (dis[nx][ny] == INF && field[nx][ny] != 'X') {
                dis[nx][ny] = dis[cur.x][cur.y] + 1;
                state news;
                news.x = nx, news.y = ny;
                que.push(news);
            }
        }
    }
}

void solve() {
    int ans = 0;
    for (int i = 0; i <= k - 1;i++) {
        ans += bfs(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y);
    }
    printf("%d\n", ans);
}

int main() {
    
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0;i<n; i++)
        scanf("%s",field[i]);
    for (int i = 0; i <n; i++)
        for (int j = 0; j < m; j++) {
            if (field[i][j] == 'S')
                p[0].x = i, p[0].y = j;
            if (field[i][j]-'0'>0&&field[i][j]-'0'<10) {
                int id = field[i][j] - '0';
                p[id].x = i;
                p[id].y = j;
            }
        }
    solve();
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
static const int MAX_H = 1000;
static const int MAX_W = 1000;
static const int MAX_N = 10;
static const int INF = 1 << 30;
typedef pair<int, int> P;

int H, W, N;
char s[MAX_H][MAX_W + 1];
P p[MAX_N];
int dist[MAX_H][MAX_W];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int bfs(int n, int m){
	queue<P> qa;
	fill(dist[0], dist[H], INF);
	qa.push(p[n]);
	dist[p[n].first][p[n].second] = 0;
	while(!qa.empty()){
		P t = qa.front(); qa.pop();
		if(t == p[m]) break;
		for(int i = 0; i < 4; i++){
			int nx = t.first + dx[i];
			int ny = t.second + dy[i];
			if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
			if(s[nx][ny] == 'X') continue;
			if(dist[nx][ny] == INF){
				qa.push(make_pair(nx, ny));
				dist[nx][ny] = dist[t.first][t.second] + 1;
			}
		}
	}
	return dist[p[m].first][p[m].second];
}

int main(){
	cin >> H >> W >> N;
	for(int i = 0; i < H; i++){
		cin >> s[i];
		for(int j = 0; j < W; j++){
			if(s[i][j] == 'S'){
				p[0] = make_pair(i, j);
			}else if(s[i][j] >= '1' && s[i][j] <= '9'){
				p[s[i][j] - '0'] = make_pair(i, j);
			}
		}
	}
	int ans = 0;
	for(int i = 0; i < N; i++){
		ans += bfs(i, i + 1);
	}
	cout << ans << endl;
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;

struct OZAWA
{
	int x;
	int y;
	int c;
};
int Flag[1000][1000];
char MAP[1000][1000];
queue<OZAWA> Q;
OZAWA ba,v;
int main(void)
{
	int H,W,N,Nc=0,X[10],Y[10];
	int mv[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
	char Cheese[10] ={'1','2','3','4','5','6','7','8','9'};
	memset(MAP,'X',sizeof(MAP));
	memset(Flag,0,sizeof(Flag));
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			//printf("%d %d ",i,j);
			cin >> MAP[i][j];
			//printf("Q");
			if(MAP[i][j] == 'S'){
				ba.x = j;
				ba.y = i;
				ba.c = 0;
				Flag[i][j] = 1;
				Q.push(ba);
				//printf("Sin\n");
			}
			for(int ii=0;ii<9;ii++){
				if(MAP[i][j] == Cheese[ii]){

					X[ii] = j;
					Y[ii] = i;
					//printf("\n@@@\n");
				}
			}
		}
	}
	//printf("S %d %d\n",ba.x,ba.y);
	//for(int ii=0;ii<2;ii++)
	//printf("%d %d\n",X[0],Y[0]);
	while(!Q.empty()){
		//printf("QA\n");
		ba = Q.front();
		Q.pop();
		if(ba.x==X[Nc] && ba.y==Y[Nc]){
			//printf("OZA");
			Nc++;
			memset(Flag,0,sizeof(Flag));
			while(!Q.empty()){
				Q.pop();
			}
			Q.push(ba);
		}
		//printf("aaa\n");
		if(Nc == N) break;
		for(int i=0;i<4;i++){
			if(MAP[ba.y+mv[i][1]][ba.x+mv[i][0]]!='X' && Flag[ba.y+mv[i][1]][ba.x+mv[i][0]]==0 && (ba.x+mv[i][0])>=0&&(ba.x+mv[i][0])<W&&(ba.y+mv[i][1])>=0&&(ba.y+mv[i][1])<H){
				v.x=ba.x+mv[i][0];
				v.y=ba.y+mv[i][1];
				v.c=ba.c+1;
				Q.push(v);
				Flag[v.y][v.x] = 1;
				//printf("%d %d\n",v.x,v.y);
			}
		}
	}
	cout <<ba.c<<endl;
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1000][1000],C[11]={"0123456789"};
int D[1000][1000];
int A[1000][1000],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
	memset(A,0,sizeof(A));
   memset(D,0,sizeof(D));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();
		A[S.y][S.x]=1;
		S.x+=x[l];
		S.y+=y[l];
		S.Z++;
		
		if(A[S.y][S.x]==1) continue;
		
		if(s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
			S.x-=x[l];
			S.y-=y[l];
		}
		else if(s[S.y][S.x]=='.'){}
		else{
			if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			memset(A,0,sizeof(A));
			l=-1;
			}

		} 
		P.push(S);
	}
	printf("%d\n",S.Z);
	return 0;
}