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<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std;

typedef pair<int,int> PII;
typedef pair<int,PII> PIII;

vector<string> m(1000);

int main(){

    int H,W,N;
    
    cin >> H >> W >> N;

    for(int i=0;i<H;++i)
        cin >> m[i];

    int sx[10],sy[10];

    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j)
            if(m[i][j]=='S'){
                sx[0] = i;
                sy[0] = j;
            } else if(isdigit(m[i][j])){
                int x = m[i][j]-'0';
                sx[x] = i;
                sy[x] = j;
            }
    }

    int ans = 0;

    for(int n=0;n<N;++n){
    
        priority_queue<PIII,vector<PIII>,greater<PIII> > q;
        q.push(make_pair(0,make_pair(sx[n],sy[n])));

        m[sx[n]][sy[n]]     = n   + '0';
        m[sx[n+1]][sy[n+1]] = n+1 + '0';

        while(!q.empty()){

            pair<int,PII> p = q.top(); q.pop();

            if(p.second.first == sx[n+1] && p.second.second == sy[n+1]){
                ans += p.first;
                break;
            }

            const int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

            for(int i=0;i<4;++i){

                int x = p.second.first+d[i][0];
                int y = p.second.second+d[i][1];

                if(0<=x && x<H && 0<=y && y<W && m[x][y]!='X' && m[x][y]!=n+'0'){

                    m[x][y] = n+'0';
                    q.push(make_pair(p.first+1,make_pair(x,y)));

                }

            }

        }

    }

    cout << ans << endl;

    return 0;

}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <queue>

using namespace std;

typedef pair <int, int> point;
typedef pair < point, int> kotori;
// 2130
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

bool vst[1280][1280];
string field[1280];
int h, w, n;

void vst_clear(void)
{
	for (int i = 0; i < 1280; i++){
		for (int j = 0; j < 1280; j++){
			vst[i][j] = false;
		}
	}
}

bool is_region(int y, int x)
{
	return (0 <= y) && (y < h) && (0 <= x) && (x < w);
}

int main(void)
{
	ios::sync_with_stdio(0); cin.tie(0);

	point start, goal;
	point f[10];

	cin >> h >> w >> n;
	for (int i = 0; i < h; i++){
		cin >> field[i];
		for (int j = 0; j < w; j++){
			if (field[i][j] == 'S'){
				field[i][j] = '0';
			}
			if (isdigit(field[i][j])){
				f[field[i][j] - '0'] = make_pair(i, j);
			}
		}
	}

	int ret = 0;
	for (int g = 1; g <= n; g++){
		vst_clear();
		start = f[g - 1];
		goal = f[g];
		vst[start.first][start.second] = true;

		queue < kotori > q;
		kotori a;
		a.first = start;
		a.second = 0;
		q.push(a);
		// point, step;
		while (!q.empty()){
			kotori hanayo = q.front(); q.pop();
			vst[hanayo.first.first][hanayo.first.second] = true;
			if (hanayo.first == goal){
				ret += hanayo.second;
				break;
			}
			for (int i = 0; i < 4; i++){
				int ny = hanayo.first.first + dy[i], nx = hanayo.first.second + dx[i];
				if (!is_region(ny, nx)) continue;
				if (vst[ny][nx]) continue;
				if (field[ny][nx] == 'X') continue;
				kotori p; p.first = make_pair(ny, nx); p.second = hanayo.second + 1;
				q.push(p);
			}
		}
	}
	cout << ret << endl;

	return 0;
}

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

using namespace std;

typedef pair<int, int> pii;

// ?????°
constexpr int MAX_SCALE = 1010;
constexpr int INF = 1000;

// input
int height, width, max_search_num;
int sx, sy;
int gx, gy;
char maze[MAX_SCALE][MAX_SCALE];

// ???????????§??????????????¢?????????
int d[MAX_SCALE][MAX_SCALE];
queue<pii> relay_point;

int ans = 0;

int bfs(pii relay_pos, int num)
{
    // init
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            d[y][x] = INF;
            if (maze[y][x] == num + '0') {
                gx = x; gy = y;
            }
        }
    }

    // ??????????¨??????????
    queue<pii> que;
    que.push(pii(sx, sy));
    int dx[4] = {1, 0, 0, -1};
    int dy[4] = {0, -1, 1, 0};

    // start??°???
    d[relay_pos.second][relay_pos.first] = 0;

    // 1~9?????§?¨?
    pii q = relay_point.front(); relay_point.pop();

    while (!que.empty()) {
        pii p = que.front(); que.pop();

        if (p.first == q.first && p.second == q.second) {
            break;
        }

        for (int i = 0; i < 4; ++i) {
            int nx = p.first + dx[i], ny = p.second + dy[i];

            if (nx >= 0 && nx < width && ny >= 0 && ny < height && maze[nx][ny] != 'X' && d[nx][ny] == INF) {
                que.push(pii(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }

    ans += d[gy][gx];

    if (num != max_search_num) bfs(pii(gx, gy), num + 1);

    return ans;
}

void solve()
{
    int res = bfs(pii(sx, sy), 1);
    cout << res << endl;
}

int main()
{
    cin >> height >> width >> max_search_num;

    static int point = 1;

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            cin >> maze[y][x];
            if (maze[y][x] == 'S') {
                sx = x; sy = y;
            } else if (maze[y][x] == point + '0') {
                relay_point.push(pii(x, y));
                point++;
            }
        }
    }
    
    solve();
    
    return 0;
}