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 <bits/stdc++.h>

using namespace std;

int H, W, N;
vector<vector<char>> area;
vector<pair<int, int>> pos;		// S ~ 9 の場所

int bfs(pair<int, int> start, pair<int, int> goal) {
	const vector<int> nh = {1, 0, -1, 0};
	const vector<int> nw = {0, 1, 0, -1};

	vector<vector<int>> depth(H, vector<int>(W, 1e9));		// start からの距離。 十分大きい値に初期化する
	depth[start.first][start.second] = 0;	// start の場所の時間は 0

	queue<pair<int, int>> que;
	que.push(start);
	while (que.size()) {
		pair<int, int> now = que.front();
		que.pop();

		for (int i = 0; i < 4; ++i) {
			// 現在位置と隣接する座標(4近傍)
			int next_h = now.first + nh[i];
			int next_w = now.second + nw[i];

			if (next_h < 0 || next_w < 0 ||
				next_h >= H || next_w >= W) continue;
			if (area[next_h][next_w] == 'X') continue;			// 障害物
			if (depth[next_h][next_w] <=
				depth[now.first][now.second] + 1) continue;		// すでに訪れている = 今いる場所までの時間よりも値が小さい

			depth[next_h][next_w] = depth[now.first][now.second] + 1;		// 次の場所へかかる時間 = 今いる場所までの時間 + 1
			que.push(make_pair(next_h, next_w));
		}
	}

	return depth[goal.first][goal.second];
}

int main() {
	cin >> H >> W >> N;
	area.resize(H, vector<char>(W));
	pos.resize(N + 1);
	for (int h = 0; h < H; ++h) {
		for (int w = 0; w < W; ++w) {
			cin >> area[h][w];
			if (area[h][w] == 'S') {
				pos[0] = make_pair(h, w);
			}
			if ('1' <= area[h][w] && area[h][w] <= '9') {
				pos[area[h][w] - '0'] = make_pair(h, w);
			}
		}
	}

	int ans = 0;
	// S ~ 1 の距離、 1 ~ 2 の距離 ... の和を求めればよい
	for (int i = 0; i < N; ++i) {
		ans += bfs(pos[i], pos[i+1]);
	}

	cout << ans << endl;

	return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

int W, H, N;
char map[1005][1005];
int fact_x[10], fact_y[10];

const int vec_x[] = {-1, 1, 0, 0}, vec_y[] = {0, 0, -1, 1};
int dist[1005][1005];

int main(void)
{
	cin >> H >> W >> N;
	for(int y = 0; y < H; y++){
		for(int x = 0; x < W; x++){
			cin >> map[x][y];
			if(map[x][y] >= '1' && map[x][y] <= '9'){
				fact_x[map[x][y] - '0'] = x;
				fact_y[map[x][y] - '0'] = y;
				map[x][y] = '.';
			}else if(map[x][y] == 'S'){
				fact_x[0] = x;
				fact_y[0] = y;
				map[x][y] = '.';
			}
		}
	}
	
	int ans = 0;
	int sx, sy, gx, gy;
	int cx, cy, nx, ny;
	
	for(int i = 0; i < N; i++){
		
		queue<int> Q;
		
		sx = fact_x[i];
		sy = fact_y[i];
		gx = fact_x[i+1];
		gy = fact_y[i+1];
		
		for(int x = 0; x < W; x++){
			for(int y = 0; y < H; y++){
				dist[x][y] = -1;
			}
		}
		
		Q.push(sx * 10000 + sy);
		dist[sx][sy] = 0;
		
		while((int)Q.size()){
			cx = Q.front() / 10000;
			cy = Q.front() % 10000;
			Q.pop();
			
			for(int dir = 0; dir < 4; dir++){
				nx = cx + vec_x[dir];
				ny = cy + vec_y[dir];
				if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
				if(map[nx][ny] != '.' || dist[nx][ny] != -1) continue;
				Q.push(nx * 10000 + ny);
				dist[nx][ny] = dist[cx][cy] + 1;
			}
		}
		
		ans += dist[gx][gy];
	}
	
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
int main(void){

	int h, w, n;
	int **map;
	char **str;
	queue<pair<int, int> > q;

	//???????¢????
	cin >> h >> w >> n;
	str = new char*[h];
	for (int i = 0; i < h; i++) str[i] = new char[w];
	map = new int *[h+2];
	for (int i = 0; i < h + 2; i++) map[i] = new int[w + 2];
	for (int i = 0; i < h + 2; i++){
		for (int j = 0; j < w + 2; j++){

			if (i == 0 || j == 0 || i == h + 1 || j == w + 1) map[i][j] = 100;
		}
	}

	//?????????????£??????°???????????????
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){


			cin >> str[i][j];
			map[i + 1][j + 1] = 0;
			if (str[i][j] == 'S') q.push(make_pair(i, j));
			if (str[i][j] == 'X') map[i + 1][j + 1] = 100;
		}
	}

	for (int i = 0; i < h + 2; i++){
		for (int j = 0; j < w + 2; j++){

			cout << map[i][j] << " ";
		}
		cout << endl;
	}

	int c = 0;
	char cstr[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	pair<int, int> xy;
	q.front().first++;
	q.front().second++;

	//??¢?´¢
	while (n >= c+1){
		bool flg = false;

		//??????????????°?????£??\
		xy = q.front();
		q.pop();
		//???
		if (map[xy.first][xy.second]>map[xy.first + 1][xy.second]){

			//??????
			map[xy.first + 1][xy.second] = map[xy.first][xy.second] + 1;
			flg = true;
			//????¨??????????????????????
			if (str[xy.first + 1 - 1][xy.second - 1] == cstr[c]){
				
				c++;
				//queue_cleaner
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first + 1, xy.second));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}

				}
				continue;
			}
			q.push(make_pair(xy.first + 1, xy.second));
		}
		//???
		if (map[xy.first][xy.second]>map[xy.first][xy.second + 1]){

			map[xy.first][xy.second + 1] = map[xy.first][xy.second] + 1;
			flg = true;
			if (str[xy.first- 1][xy.second + 1 - 1] == cstr[c]){

				c++;
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first, xy.second + 1));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}
				}
				continue;
			}
			q.push(make_pair(xy.first, xy.second + 1));
		}
		//???
		if (map[xy.first][xy.second]>map[xy.first - 1][xy.second]){

			map[xy.first - 1][xy.second] = map[xy.first][xy.second] + 1;
			flg = true;
			if (str[xy.first - 1 - 1][xy.second- 1] == cstr[c]){

				c++;
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first - 1, xy.second));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}
				}
				continue;
			}
			q.push(make_pair(xy.first - 1, xy.second));
		}
		//???
		if (map[xy.first][xy.second]>map[xy.first][xy.second - 1]){

			map[xy.first][xy.second-1] = map[xy.first][xy.second] + 1;
			flg = true;
			if (str[xy.first - 1][xy.second - 1 - 1] == cstr[c]){

				c++;
				while (!q.empty()) q.pop();
				q.push(make_pair(xy.first, xy.second - 1));
				for (int i = 1; i < h + 1; i++){
					for (int j = 1; j < w + 1; j++){

						if (!(i == q.front().first && j == q.front().second) && map[i][j] != 100){
							map[i][j] = 0;
						}
					}
				}
				continue;
			}
			q.push(make_pair(xy.first, xy.second - 1));
		}
		if (flg) map[xy.first][xy.second]++;
		for (int i = 0; i < h + 2; i++){
			for (int j = 0; j < w + 2; j++){

				cout << map[i][j] << " ";
			}
			cout << endl;
		}
	}

	cout << map[q.front().first][q.front().second] << endl;
}