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 <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int minDist(const vector<string>& plane, char wall, int y1, int x1, int y2, int x2)
{
    int dy[] = {0, 0, -1, 1};
    int dx[] = {-1, 1, 0, 0};

    int h = plane.size();
    int w = plane[0].size();

    queue<pair<int, int> > q;
    vector<vector<bool> > check(h, vector<bool>(w, false));
    q.push(make_pair(y1, x1));
    check[y1][x1] = true;
    int ret = 0;
    while(!q.empty()){
        queue<pair<int, int> > q1;
        while(!q.empty()){
            int y = q.front().first;
            int x = q.front().second;
            q.pop();
            if(y == y2 && x == x2)
                return ret;
            for(int i=0; i<4; ++i){
                int y0 = y + dy[i];
                int x0 = x + dx[i];
                if(0 <= y0 && y0 < h && 0 <= x0 && x0 < w && plane[y0][x0] != wall && !check[y0][x0]){
                    q1.push(make_pair(y0, x0));
                    check[y0][x0] = true;
                }
            }
        }
        ++ ret;
        q = q1;
    }
    return -1;
}

int main()
{
    int h, w, n;
    cin >> h >> w >> n;

    vector<string> plane(h);
    for(int i=0; i<h; ++i)
        cin >> plane[i];

    vector<int> y(n+1), x(n+1);
    for(int i=0; i<h; ++i){
        for(int j=0; j<w; ++j){
            if(plane[i][j] == 'S'){
                y[0] = i;
                x[0] = j;
            }else if('0' <= plane[i][j] && plane[i][j] <= '9'){
                y[plane[i][j]-'0'] = i;
                x[plane[i][j]-'0'] = j;
            }
        }
    }

    int ret = 0;
    for(int i=0; i<n; ++i)
        ret += minDist(plane, 'X', y[i], x[i], y[i+1], x[i+1]);
    cout << ret << endl;

    return 0;
}

C++: #include<iostream>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
#define INF 10000000
#define rep(i,s,t) for(int i=s;i<=(t);i++)
#define MP(a,b) make_pair(a,b)
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int h,w,n;
char m[1000][1000];

int bfs(int s){
	static int d[1000][1000];
	queue<pair<int,int> > q;
	pair<int,int> a,b;
	rep(i,1,h)rep(j,1,w)
		if(m[i][j]=='0'+s){
			d[i][j]=0;
			q.push(MP(i,j));
		}
		else d[i][j]=INF;
	while(1){
		a = q.front();
		q.pop();
		rep(k,0,3){
			b = MP(a.first+dy[k],a.second+dx[k]);
			if(m[b.first][b.second]=='X')continue;
			if(d[a.first][a.second]+1 < d[b.first][b.second]){
				d[b.first][b.second] = d[a.first][a.second]+1;
				if(m[b.first][b.second]=='0'+s+1)return d[b.first][b.second];
				q.push(b);
			}
		}
	}
	return -1;
}
int main(){
	cin>>h>>w>>n;
	memset(m,'X',sizeof(m));
	rep(i,1,h)rep(j,1,w){
		cin>>m[i][j];
		if(m[i][j]=='S')m[i][j]='0';
	}
	int ans=0;
	rep(i,0,n-1)ans += bfs(i);
	cout<<ans<<endl;
}

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

using namespace std;

int h, w, n;

char field[1010][1010];

using pos = pair<int, int>;

pos pass[10];

int distance(pos start, pos goal) {
    bool visited[1010][1010];
    for (int i = 0; i < 1010; i++) {
        for (int j = 0; j < 1010; j++) {
            visited[i][j] = false;
        }
    }
    queue<pair<int, pos>> q;
    q.push(make_pair(0, start));

    while (!q.empty()) {
        auto s = q.front();
        q.pop();
        if (s.second.first < 0 || s.second.second < 0 || s.second.first >= h || s.second.second >= w) { continue; }
        if (field[s.second.first][s.second.second] == 'X') { continue; }
        if (visited[s.second.first][s.second.second]) { continue; }
        if (s.second == goal) {
            return s.first;
        }
        visited[s.second.first][s.second.second] = true;
        q.push(make_pair(s.first + 1, make_pair(s.second.first, s.second.second + 1)));
        q.push(make_pair(s.first + 1, make_pair(s.second.first, s.second.second - 1)));
        q.push(make_pair(s.first + 1, make_pair(s.second.first + 1, s.second.second)));
        q.push(make_pair(s.first + 1, make_pair(s.second.first - 1, s.second.second)));
    }
}

int main() {
    scanf("%d", &h);
    scanf("%d", &w);
    scanf("%d", &n);
    for (int i = 0; i < h; i++) {
        scanf("%s", field[i]);
        for (int j = 0; j < w; j++) {
            if (field[i][j] == 'S') {
                pass[0] = make_pair(i, j);
            } else if (field[i][j] >= '1' && field[i][j] <= '9') {
                pass[field[i][j] - '0'] = make_pair(i, j);
            }
        }
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += distance(pass[i], pass[i + 1]);
    }
    printf("%d\n", sum);
}