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>
#include <vector>
#include <queue>
#include <algorithm>
#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;
    }
    */
    fill(d, sizeof(d), 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++: // 2013/11/01 Tazoe

#include <iostream>
#include <queue>
using namespace std;

// 位置
struct point{
	int y;
	int x;
};

// 位置＋時間
struct state{
	int y;
	int x;
	int t;
};

int main()
{
	int H, W, N;
	cin >> H >> W >> N;

	char F[1002][1002];
	for(int y=0; y<=H+1; y++){
		for(int x=0; x<=W+1; x++){
			F[y][x] = 'X';
		}
	}

	struct point P[10];			// 巣(0)と工場(1-N)の位置

	for(int y=1; y<=H; y++){
		for(int x=1; x<=W; x++){
			cin >> F[y][x];

			if(F[y][x]=='S'){
				P[0].y = y;
				P[0].x = x;
				F[y][x] = '.';
			}
			else if('0'<=F[y][x] && F[y][x]<='9'){
				P[F[y][x]-'0'].y = y;
				P[F[y][x]-'0'].x = x;
				F[y][x] = '.';
			}
		}
	}

	int sum = 0;				// 合計時間

	for(int i=0; i<N; i++){			// P[i]からP[i+1]までの時間を求める
		struct state s;
		s.y = P[i].y;
		s.x = P[i].x;
		s.t = 0;

		queue<struct state> q;
		q.push(s);				// スタートをキューに挿入

		bool memo[1002][1002];
		for(int y=1; y<=H; y++){
			for(int x=1; x<=W; x++){
				memo[y][x] = false;
			}
		}
		memo[s.y][s.x] = true;			// スタートを訪問済

		while(true){
			if(q.empty())				// キュートップに頂点がなければ
				break;

			s = q.front();

			if(s.y==P[i+1].y && s.x==P[i+1].x){	// ゴールならば
				sum += s.t;
				break;
			}

			if(F[s.y][s.x+1]=='.' && !memo[s.y][s.x+1]){	// 東
				struct state t;
				t.y = s.y;
				t.x = s.x+1;
				t.t = s.t+1;

				q.push(t);
				memo[t.y][t.x] = true;
			}

			if(F[s.y][s.x-1]=='.' && !memo[s.y][s.x-1]){	// 西
				struct state t;
				t.y = s.y;
				t.x = s.x-1;
				t.t = s.t+1;

				q.push(t);
				memo[t.y][t.x] = true;
			}

			if(F[s.y+1][s.x]=='.' && !memo[s.y+1][s.x]){	// 南
				struct state t;
				t.y = s.y+1;
				t.x = s.x;
				t.t = s.t+1;

				q.push(t);
				memo[t.y][t.x] = true;
			}

			if(F[s.y-1][s.x]=='.' && !memo[s.y-1][s.x]){	// 北
				struct state t;
				t.y = s.y-1;
				t.x = s.x;
				t.t = s.t+1;

				q.push(t);
				memo[t.y][t.x] = true;
			}

			q.pop();
		}
	}

	cout << sum << endl;

	return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

char s[1000][1000];

char state[1000][1000];

int H, W, N;


int main()
{
	//freopen("in.txt", "r", stdin);

	while (scanf("%d%d%d", &H, &W, &N)==3) {
		if (H==0 && W==0) break;
		int i, j;
		char c;
		int si, sj;

		for (i=0; i<H; i++) {
			for (j=0; j<W; j++) {
				do { c=getchar();
				} while (c!='.' && c!='S' && c!='X' && !isdigit(c));
				s[i][j] = c;
				if (c=='S') {
					si = i; sj = j;
				}
			}
		}

		memset(state, 0, sizeof(state));
		state[si][sj] = 1;
		
		int step=0;
		int want=1;
		queue<int> q1, q2, *q, *nq, *qtmp;

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

		q = &q1; nq = &q2;
		q1.push((si<<16)|sj);

		while (want<=N) {
			int old_want=want;
			while (!nq->empty()) nq->pop();
			step++;
			while (!q->empty()) {
				
				int i, j, value;
				value = q->front(); q->pop();
				i = value>>16; j = value&0xffff;

				int k;
				for (k=0; k<4; k++) {
					int ti, tj;
					ti = i+dy[k]; tj = j+dx[k];
					if (ti<0 || ti>=H || tj<0 || tj>=W || s[ti][tj]=='X') continue;
					if (state[ti][tj]==want) continue;
					if (s[ti][tj]==want+'0') {
						want++; 
					}
					
					state[ti][tj] = want;
					nq->push((ti<<16)|tj);
					if (want!=old_want) break;
				}
				if (want!=old_want) break;
			}

			if (want!=old_want) {
				while (nq->size()>1) nq->pop();
			}

			qtmp = q; q = nq; nq = qtmp;



		}

		printf("%d\n", step);

	}

	return 0;
}