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

#define MAX 1000
int y[MAX][MAX];
int x[MAX][MAX];
int px[10];
int py[10];
int main(){
	memset(y,127,sizeof(y));
	char T[12]={'.','1','2','3','4','5','6','7','8','9','S','X'};
	int n,m,c,d,sum;
	string S;
	sum=0;
	cin>>n>>m>>c;
	for(int i=0;i<n;i++){
		cin>>S;
		for(int j=0;j<m;j++){
			for(int k=0;k<12;k++){
				if(S[j]==T[k]){
					x[i][j]=k;
					if(k==10){
						px[0]=i;
						py[0]=j;
					}
					if(k>=1 && k<=9){
						px[k]=i;
						py[k]=j;
					}
				}
			}
		}
	}
	for(int i=1;i<=c;i++){
		memset(y,127,sizeof(y));
		y[px[i-1]][py[i-1]]=0;
		for(int j=0;j<n*m;j++){
			for(int k=0;k<n;k++){
				for(int l=0;l<m;l++){
					if(k>=1){
						if(x[k-1][l]!=11){
							y[k-1][l]=min(y[k-1][l],y[k][l]+1);
						}
					}
					if(l>=1){
						if(x[k][l-1]!=11){
							y[k][l-1]=min(y[k][l-1],y[k][l]+1);
						}
					}
					if(x[k+1][l]!=11){
						y[k+1][l]=min(y[k+1][l],y[k][l]+1);
					}
					if(x[k][l+1]!=11){
						y[k][l+1]=min(y[k][l+1],y[k][l]+1);
					}
				}
			}
		}
		sum += y[px[i]][py[i]];
	}
	cout<<sum<<endl;
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int Max_n=1100;

int n,m,d;
struct Node{
    int x,y,d;
    Node(int xx,int yy,int dd):x(xx),y(yy),d(dd){}
};
char s[Max_n][Max_n];
bool vis[Max_n][Max_n];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

Node bfs(int x1,int y1,int k){
    memset(vis,0,sizeof(vis));
    queue<Node>que;
    que.push(Node(x1,y1,0));
    vis[x1][y1]=1;
    while(!que.empty()){
        Node node=que.front();que.pop();
        int x=node.x,y=node.y;
        if(k==s[x][y]-'0')return node;
        int d=node.d;
        for(int i=0;i<4;i++){
            int X=x+dx[i],Y=y+dy[i];
            if(!vis[X][Y]&&(s[X][Y]=='S'||s[X][Y]=='.'||(s[X][Y]>='1'&&s[X][Y]<='9'))){
                vis[X][Y]=1;
                que.push(Node(X,Y,d+1));
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&d)){
        memset(s,0,sizeof(s));
        for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
        int x,y;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++)
            if(s[i][j]=='S'){x=i;y=j;}
        }
        int sum=0;
        for(int i=1;i<=d;i++){
            Node node=bfs(x,y,i);
            sum+=node.d;
            x=node.x,y=node.y;
        }
        printf("%d\n",sum);
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstring>
using namespace std;
#define MAXN 1001
char A[MAXN][MAXN];
int w,h,n;
int s[2];

void input()
{
    cin>>h>>w>>n;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>A[i][j];
            if(A[i][j]=='S'){
                s[0]=i;
                s[1]=j;
            }
        }
    }
}

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
typedef pair<pair<int,int>,int> P;

int known[MAXN][MAXN];

int bfs(int& x, int& y, char k)
{
    queue<P>que;
    que.push(P(make_pair(x, y), 0));
    while(!que.empty()){
        P cur = que.front();
        // cout<<"*"<<cur.first.first<<" "<<cur.first.second<<endl;
        que.pop();
        if(A[cur.first.first][cur.first.second]==k){
            x = cur.first.first;
            y = cur.first.second;
            return cur.second;
        }
        for(int i=0;i<4;i++){
            int nx = cur.first.first+dx[i];
            int ny = cur.first.second+dy[i];
            if(0<=nx&&nx<h && 0<=ny&&ny<w && A[nx][ny]!='X' && !known[nx][ny]){
                que.push(P(make_pair(nx, ny), cur.second+1));
                known[nx][ny]=1;
            }
        }
    }
    return 0x3fffffff;
}

int main()
{
    input();
    int res = 0;
    int x, y;
    x = s[0];
    y = s[1];

    for(int i=1;i<=n;i++){
        memset(known, 0, sizeof(int)*MAXN*MAXN);
        int tmp = bfs(x, y, '0'+i);
        res += tmp;
    }
    cout<<res<<endl;
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll = long long;
const ll INF = 1e9;

typedef pair<int, int> P;
int H, W, N;
vector<P> F(10);
string M[1001];

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

ll bfs(P ps, P pg) {
    ll d[1001][1001];
    queue<P> que;
    for(int i = 0; i < H; i++) for (int j = 0; j < W; j++) d[i][j] = INF;

    que.push(ps);
    d[ps.second][ps.first] = 0;

    while(que.size()) {
        P p = que.front(); que.pop();
        if (p.first == pg.first && p.second == pg.second) break;

        for (int i = 0; i < 4; i++) {
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if (M[ny][nx] != 'X' && nx >= 0 && nx < W && ny >= 0 && ny < H && d[ny][nx] == INF) {
                que.push(P(nx, ny));
                d[ny][nx] = d[p.second][p.first] + 1;
            }
        }
    }

    return d[pg.second][pg.first];
}

int main() {
    ll ans = 0;
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        cin >> M[i];
        for (int k = 0; k < W; k++) {
            if (M[i][k] == 'S') {
                F[0] = P(k, i);
            }
            if (isdigit(M[i][k])) {
                F[M[i][k] - '0'] = P(k, i);
            }
        }
    }

    for (int i = 0; i < N; i++) {
        ans += bfs(F[i], F[i+1]);
    }

    cout << ans << endl;

    return 0;
}