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<queue>
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 1000;
const int MAX_M = 1000;
const int MAX_T = 9;
const int INF = 100000000;
int n, m, t;
int sx, sy;
int ans = 0;
char field[MAX_N][MAX_M];
int d[MAX_N][MAX_M];
P factory[MAX_T];
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};

//从当前起点&#24320;始，&#23547;找第num个&#22902;酪 
int bfs(int x, int y, int num){
	int gx = factory[num].first;
	int gy = factory[num].second;
	queue<P> que;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			d[i][j] = INF;
		}
	}
	d[x][y] = 0;
	que.push(P(x,y));
	
	while(que.size()){
		P p = que.front();
		que.pop();
		int x = p.first;
		int y = p.second;
		if(x == gx && y == gy){
			break;
		}
		for(int i = 0; i < 4; i++){
			int nx = x + dx[i], ny = y + dy[i];
			if(nx >= 0 && nx < n && ny >= 0 && ny < m && field[nx][ny] != 'X' && d[nx][ny] == INF){
				d[nx][ny] = d[x][y] + 1;
				que.push(P(nx, ny));
			}
		}
	}
	
	return d[gx][gy];
}

void solve(){
	cin >> n >> m >> t;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			cin >> field[i][j];
			if(field[i][j] == 'S'){
				factory[0] = P(i, j);
			}
			if(field[i][j] >= '1' && field[i][j] <= '9'){
				int index = field[i][j] - '0';
				factory[index] = P(i, j);
			}
		}
	}

	int ans = 0;
	
	for(int i = 0; i < t; i++){
		int x = factory[i].first;
		int y = factory[i].second;
		ans += bfs(x, y, i + 1);
	}
	cout << ans << "\n";
}

int main(){
	solve();
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)

using namespace std;


struct P{
    int x;
    int y;
};

int w,h,n;
const int INF = 1e+8;
char feld[1010][1010];
int d[1010][1010];
int sx,sy,gx,gy;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans=0;
P che[15];


int bfs(){
    for(int i=0;i<n;i++){
        P sp=che[i];
        sx=che[i].x;
        sy=che[i].y;
        gx=che[i+1].x;
        gy=che[i+1].y;
        queue<P> que;
        for(int y=0;y<h;y++)for(int x=0;x<w;x++)d[y][x]=INF;
        que.push(sp);
        d[sy][sx]=0;
        while(!que.empty()){
            P p=que.front();
            que.pop();
            int x=p.x;
            int y=p.y;
            if(x==gx&&y==gy){
                ans+=d[gy][gx];
                break;
            }
            for(int i=0;i<4;i++){
                int nx=x+dx[i];
                int ny=y+dy[i];
                if(nx<0||ny<0&&nx>=w&&ny>=h)continue;
                if(feld[ny][nx]!='X'&&d[ny][nx]==INF){
                    P next;
                    next.x=nx;
                    next.y=ny;
                    que.push(next);
                    d[ny][nx]=d[y][x]+1;
                }
            }
        }
    }
    return 0;
}

int main(){
    cin>>h>>w>>n;
    for(int y=0;y<h;y++){
        for(int x=0;x<w;x++){
            scanf(" %c",&feld[y][x]);
            if(feld[y][x]=='S'){
                che[0].x=x;
                che[0].y=y;
            }else if(feld[y][x]>='1'&&feld[y][x]<='9'){
                char c=feld[y][x];
                che[c-'0'].x=x;
                che[c-'0'].y=y;
            }
        }
    }
    bfs();
    cout<<ans<<endl;
    return 0;
}

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

using namespace std;

struct Position {
    int h, w, time, strength;
    Position (int h, int w, int t, int s): h(h), w(w), time(t), strength(s) {}
};

const int max_h = 1005;
const int max_w = 1005;
int g_h, g_w, g_max_strength, si, sj;
char mat[max_h][max_w];
char mat2[max_h][max_w];
int dh[] = {0, 1, 0, -1};
int dw[] = {1, 0, -1, 0};

int main() {
    scanf("%d%d%d", &g_h, &g_w, &g_max_strength);
    for (int i = 0; i < g_h; i++) {
        getchar();
        for (int j = 0; j < g_w; j++) {
            mat[i][j] = getchar();
            if (mat[i][j] == 'S') {
                si = i;
                sj = j;
            }
            if (mat[i][j] == 'X') {
                mat2[i][j] = 'X';
            } else {
                mat2[i][j] = '.';
            }
        }
    }
    queue<Position> q;
    q.push(Position(si, sj, 0, '0'));
    int result = -1;
    while (!q.empty()) {
        Position p = q.front();
        q.pop();
        char c = mat[p.h][p.w];
        if (isdigit(c)) {
            if (p.strength+1 == c) {
                if (p.strength+1 == g_max_strength+'0') {
                    result = p.time;
                    break;
                }
                p.strength++;
            }
        }
        mat2[p.h][p.w] = p.strength;
        for (int i = 0; i < 4; i++) {
            int w = p.w + dw[i];
            int h = p.h + dh[i];
            if (w >= 0 && w < g_w && h >= 0 && h < g_h &&
                    (mat2[h][w] == '.' || (isdigit(mat2[h][w]) && p.strength > mat2[h][w]))) {
                q.push(Position(h, w, p.time+1, p.strength));
            }
        }
    }
    printf("%d\n", result);
    return 0;
}

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

typedef pair<int,int> pii;
int mp[1001][1001];
int turn[1001][1001];
pii target[10];

int main() {
    int h,w,n,x,y;
    string s;
    cin>>h>>w>>n;

    for(int i=0; i<h; ++i) {
        cin>>s;
        for(int j=0; j<w; ++j) {
            if(s[j] == 'X') mp[i][j] = -1;
            else if('1' <= s[j] && s[j] <= '9') {
                mp[i][j] = s[j] - '0';
                target[s[j]-'0'] = pii(j,i);
            } else if(s[j] == 'S') y = i,x = j;
            else mp[i][j] = 0;
        }
    }

    int dx[4] = {0,1,0,-1};
    int dy[4] = {1,0,-1,0};
    int ans = 0;
    for(int i=0; i<n; ++i) {
        memset(turn, -1, sizeof(turn));
        turn[y][x] = 0;
        pii tar = target[i+1];
        queue<pii> q;
        q.push(pii(x,y));

        while(!q.empty()) {
            pii now = q.front(); q.pop();
            if(now.first == tar.first && now.second == tar.second) {
                ans += turn[now.second][now.first];
                x = now.first, y = now.second;
                break;
            }

            for(int k=0; k<4; ++k) {
                int nx = now.first + dx[k], ny = now.second + dy[k];
                if(nx < 0 || nx > w-1 || ny < 0 || ny > h-1) continue;
                if(mp[ny][nx] == -1) continue;
                if(turn[ny][nx] != -1) continue;
                turn[ny][nx] = turn[now.second][now.first]+1;
                q.push(pii(nx,ny));
            }
        }
    }

    cout<<ans<<endl;
}