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<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int h, w, n;

bool CanMove(int y, int x, vector< vector<char> > m){
    if(y < 0) return false;
    if(x < 0) return false;
    if(y >= h) return false;
    if(x >= w) return false;
    if(m[y][x] == 'X') return false;
    return true;
}

//vector< vector<int> > MakeAnsMap(int y, int x, int step, vector< vector<int> > ans_map, vector< vector<char> > m){
//    ans_map[y][x] = step;
//    int my[] = {0, 0, 1, -1};
//    int mx[] = {1, -1, 0, 0};
//
//    REP(i, 4){
//        int ny = y + my[i];
//        int nx = x + mx[i];
//        if(CanMove(ny, nx, m) && ans_map[ny][nx] > step + 1){
//            ans_map = MakeAnsMap(ny, nx, step + 1, ans_map, m);
//        }
//    }
//    return ans_map;
//}

class C{
    public:
    int y, x, step;
    C(int _y, int _x, int _step);
    bool operator>(const C &c) const{
        return step > c.step;
    }
};

C::C(int _y, int _x, int _step){
    y = _y;
    x = _x;
    step = _step;
}

int MakeAnsMap(int gy, int gx, int y, int x, int tmp, vector< vector<char> > m){
    priority_queue<C, vector<C>, greater<C> > open;
    open.push( C(y, x, tmp) );
    vector< vector<int> > closed(m.size(), vector<int>(m[0].size()));
    REP(i, m.size()) REP(j, m[0].size()) closed[i][j] = INF;
    int my[] = {0, 0, 1, -1};
    int mx[] = {1, -1, 0, 0};
    int ret;

    while(!open.empty()){
        C now = open.top();
        open.pop();
        if(now.y == gy && now.x == gx){
            ret = now.step;
            break;
        }

        closed[now.y][now.x] = now.step;

        REP(i, 4){
            int ny = now.y + my[i], nx = now.x + mx[i];
            if(CanMove(ny, nx, m) && closed[ny][nx] > now.step + 1){
                open.push( C(ny, nx, now.step + 1) );
            }
        }
    }
    return ret;
}

int main(){
    cin >>h >>w >>n;
    vector< vector<char> > m(h, vector<char>(w) );
    vector<int> y(n + 2), x(n + 2);

    //start of input
    REP(i, h){
        REP(j, w){
            cin >>m[i][j];
            if(m[i][j] == 'S'){
                y[0] = i;
                x[0] = j;
            }
            if(m[i][j] - '0' > 0 && m[i][j] - '0' <= n){
                int num = m[i][j] - '0';
                y[num] = i;
                x[num] = j;
            }
        }
    }
    //end of input

    vector< vector<int> > ans_map(h, vector<int>(w, INF) );
    int ans = MakeAnsMap(y[1], x[1], y[0], x[0], 0, m);

    FOR(i, 1, n){
        ans = MakeAnsMap(y[i + 1], x[i + 1], y[i], x[i], ans, m);
    }
    cout <<ans <<endl;
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int H,W,N;
const int MAX_H=1000;
const int MAX_W=1000;
string S[MAX_H];
int ans=0;
int X[10],Y[10];
int D[MAX_W][MAX_H];
int cou;
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
int xs,ys,xg,yg;
int E[MAX_W][MAX_H];
int solve(){
	cou=0;
	while(D[xg][yg]==-1){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				E[j][i]=D[j][i];
			}
		}
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(D[j][i]==cou){
					for(int k=0;k<4;k++){
						if(j+dx[k]>=0&&j+dx[k]<W&&i+dy[k]>=0&&i+dy[k]<H){
							if(D[j+dx[k]][i+dy[k]]==-1&&S[i+dy[k]][j+dx[k]]!='X'){
								E[j+dx[k]][i+dy[k]]=cou+1;
							}
						}
					}
				}
			}
		}
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)D[j][i]=E[j][i];
		cou++;
	    }
	return D[xg][yg];
}

int main()
{
	scanf("%d%d%d",&H,&W,&N);
	for(int i=0;i<H;i++)cin >> S[i];
	for(int i=0;i<W;i++){
		for(int j=0;j<H;j++){
			if(S[j][i]!='X'&&S[j][i]!='.'){
				if(S[j][i]=='S'){
					X[0]=i;Y[0]=j;
				}
				else{
					X[S[j][i]-'0']=i;
					Y[S[j][i]-'0']=j;
				}
			}
		}
	}
	for(int ti=0;ti<N;ti++){
		for(int i=0;i<W;i++){
		    for(int j=0;j<H;j++){
			    D[i][j]=-1;
		    }
	    }
		xs=X[ti];
		ys=Y[ti];
		xg=X[ti+1];
		yg=Y[ti+1];
		D[xs][ys]=0;
		ans+=solve();
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 10;

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

typedef struct {
	int x, y;
} node;
node makeNode(int x, int y) {
	node n = {x, y};
	return n;
}

int w, h, n;
// 入力されたフィールド (W * H)
char field[MAX_H][MAX_W + 1];
// 各地点までのステップ数，未到達なら -1
int step[MAX_H][MAX_W + 1];

int bfs(int startX, int startY, int goalX, int goalY) {
	queue<node> que;
	que.push(makeNode(startX, startY));
	step[startY][startX] = 0;
	while (!que.empty()) {
		node current = que.front();
		que.pop();
		if (current.x == goalX && current.y == goalY) {
			return step[current.y][current.x];
		}
		for (int i = 0; i < 4; i++) {
			int nextX = current.x + dx[i];
			int nextY = current.y + dy[i];
			if (0 <= nextX && nextX < w
					&& 0 <= nextY && nextY < h
					&& field[nextY][nextX] != 'X'
					&& step[nextY][nextX] == -1) {
				que.push(makeNode(nextX, nextY));
				step[nextY][nextX] = step[current.y][current.x] + 1;
			}
		}
	}
	return -1;
}

int main () {
	int startX[10], startY[10];
	cin >> h >> w >> n;
	// 入力しながら各地点の座標を探す
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cin >> field[i][j];
			if (field[i][j] == 'S') {
				startX[0] = j;
				startY[0] = i;
			}
			else if (isdigit(field[i][j])) {
				startX[field[i][j] - '0'] = j;
				startY[field[i][j] - '0'] = i;
			}
		}
	}
	
	// 工場を順番に回って合計ステップを計算する
	int ans = 0;
	for (int i = 0; i < n; i++) {
		memset(step, -1, sizeof(step));
		ans += bfs(startX[i], startY[i], startX[i + 1], startY[i + 1]);
	}
	cout << ans << endl;
	return 0;
}