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 <sstream>
#include <cstdlib>
#include <queue>

using namespace std;

const int INF = 100000000;
const int MAX_W = 1001;
const int MAX_H = 1001;
int H, W, N;
char field[MAX_H][MAX_W] = {'X'};
int d[MAX_H][MAX_W] = {INF};
typedef pair<int, int> P;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
    queue<P> que;
    P start;
    int pow = 1;
    while (std::cin >> H >> W >> N, H) {
        for (int i = 0; i < H; i++) {
            std::cin >> field[i];
        }
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                d[i][j] = INF;
                if (field[i][j] == 'S') {
                    start = P(i, j);
                }
            }
        }
        que.push(start);
        d[start.first][start.second] = 0;
        int count = 0;
        while (que.size()) {
            P p = que.front();
            que.pop();
            if (field[p.first][p.second] == '0' + N and N <= pow) {
                count = d[p.first][p.second];
                break;
            }

            for (int i = 0; i < 4; i++) {
                int nx = p.second + dx[i];
                int ny = p.first + dy[i];
                if (nx >= 0 and ny >= 0 and nx < W and ny < H and field[ny][nx] != 'X' and d[ny][nx] == INF) {
                    if (field[ny][nx] == '.') {
                        que.push(P(ny, nx));
                        d[ny][nx] = d[p.first][p.second] + 1;
                    } else {
                        if (field[ny][nx] <= '0' + pow) {
                            //cout << "pow:" << pow << ", cheese:" << field[ny][nx] << endl;
                            int s = d[p.first][p.second];
                            field[ny][nx] = '.';
                            for (int i = 0; i < H; i++) {
                                for (int j = 0; j < W; j++) {
                                    d[i][j] = INF;
                                }
                            }


                            while (que.size()) {
                                que.pop();
                            }
                            que.push(P(ny, nx));
                            d[ny][nx] = s + 1;
                            if(pow == N) {
                                field[ny][nx] = '0' + N;
                            }
                            pow++;
                            break;
                        } else {
                            que.push(P(ny, nx));
                            d[ny][nx] = d[p.first][p.second] + 1;
                        }
                    }
                }
            }
        }

        std::cout << count << std::endl;
        break;
    }

    return 0;
}

C++: #include <bits/stdc++.h>

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)
#define mp make_pair

using namespace std;

typedef pair<int, int> P;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int h, w, n;
int ans = 0;
int sx, sy, gx, gy;
char field[1010][1010];
bool went[1010][1010];

/*struct state{
	int x;
	int y;
	int count;
};*/

int bfs(char tmp) {
	queue< pair<int, P> > q;
	memset(went, false, sizeof(went));
	q.push(mp(0, mp(sx, sy)));

	while(!q.empty()) {
		pair<int, P> p = q.front();
		q.pop();
		//cout << p.first << " " << p.second.first << " " << p.second.second << endl;
		if((field[p.second.second][p.second.first]) == tmp){
			sx = p.second.first; sy = p.second.second;
			return p.first;
		}
		went[p.second.second][p.second.first] = true;
		rep(i, 4) {
			int nx = p.second.first + dx[i];
			int ny = p.second.second + dy[i];
			if(nx >= 0 && ny >= 0 && nx < w && ny < h && !went[ny][nx] && field[ny][nx] != 'X') {
				q.push(mp(p.first + 1, mp(nx, ny)));
			}
		}
	}
	return 0;
}

int main() {
	memset(field, 0, sizeof(field));
	cin >> h >> w >> n;
	rep(i, h) rep(j, w) {
		cin >> field[i][j];
		if(field[i][j] == 'S') sx = j, sy = i;
		if(field[i][j] == 'G') gx = j, gy = i;
	}
	/*rep(i, h) {
		rep(j, w) cout << field[i][j];
		cout << endl;
	}*/
	for(int i = 1; i <= n; ++i) {
		ans += bfs('0' + i);
	}
	cout << ans << endl;
	return 0;
}

C++: // 2013/11/06 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;

		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;			// スタートを訪問済

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

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

			s = q.front();
			q.pop();

			s.t++;

			s.x++;					// 東
			if(s.y==P[i+1].y && s.x==P[i+1].x)
				break;

			if(F[s.y][s.x]=='.' && !memo[s.y][s.x]){
				memo[s.y][s.x] = true;
				q.push(s);
			}
			s.x--;

			s.x--;					// 西
			if(s.y==P[i+1].y && s.x==P[i+1].x)
				break;

			if(F[s.y][s.x]=='.' && !memo[s.y][s.x]){
				memo[s.y][s.x] = true;
				q.push(s);
			}
			s.x++;

			s.y++;					// 南
			if(s.y==P[i+1].y && s.x==P[i+1].x)
				break;

			if(F[s.y][s.x]=='.' && !memo[s.y][s.x]){
				memo[s.y][s.x] = true;
				q.push(s);
			}
			s.y--;

			s.y--;					// 北
			if(s.y==P[i+1].y && s.x==P[i+1].x)
				break;

			if(F[s.y][s.x]=='.' && !memo[s.y][s.x]){
				memo[s.y][s.x] = true;
				q.push(s);
			}
			s.y++;
		}

		sum += s.t;
	}

	cout << sum << endl;

	return 0;
}