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<cstdio>
#include<queue>

using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define INF 1e+8

struct P{
    int x;
    int y;
};

int w,h,n;
char feld[1000][1000];
int d[1100][1100];
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(){
    REP(i,n){
        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;
        REP(y,h)REP(x,w)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;
            }
            REP(i,4){
                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;
    REP(y,h){
        REP(x,w){
            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<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
int map[1002][1002]={0},r[1002][1002]={0};
struct A{
int t,h,x,y;
};
int main(){
int h,w,n,i,j,m=0,g[2],b;
g[0]=1;
g[1]=-1;
char s[1001];
A a;
queue<A> q;
cin>>h>>w>>n;
for(i=0;i<=h+1;i++){
map[i][0]=-1;
map[i][w+1]=-1;
}
for(i=0;i<=w+1;i++){
map[0][i]=-1;
map[h+1][i]=-1;
}
for(i=1;i<=h;i++){
	scanf("%s",s);
	for(j=0;j<=w;j++){
		b=s[j];
		if(b>48&&b<58)
			map[i][j+1]=b-48;
		else if(s[j]=='S'){
			a.x=i;
			a.y=j+1;
			a.t=0;
			a.h=1;
			q.push(a);
		}
		else if(s[j]=='X')
			map[i][j+1]=-1;
	}
}
while(!q.empty()){
	a=q.front();
	q.pop();
	if(map[a.x][a.y]==a.h){
		a.h++;
		while(!q.empty()){
			q.pop();
		}
	}
	r[a.x][a.y]=a.h;
	if(a.h==n+1){
		m=a.t;
		break;
	}
	a.t++;
	for(i=0;i<2;i++){
		if(map[a.x+g[i]][a.y]!=-1&&r[a.x+g[i]][a.y]!=a.h){
			a.x+=g[i];
			q.push(a);
			a.x-=g[i];
		}
		if(map[a.x][a.y+g[i]]!=-1&&r[a.x][a.y+g[i]]!=a.h){
			a.y+=g[i];
			q.push(a);
			a.y-=g[i];
		}
	}
}
cout<<m<<endl;
return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <utility>
#include <functional>

using namespace std;

typedef pair<int, int> P;

int h, w, n;
char f[1000][1000];

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

struct Ele {
	Ele(int a, double b, P c) : g(a), h(b), p(c) {}
	bool operator<(const Ele& right) const {
		return g + h > right.g + right.h;
	}
	int g;
	double h;
	P p;
};

double euclidean(P s, P g) {
	int y = s.first - g.first;
	int x = s.second - g.second;
	return sqrt(x*x + y*y);
}

int Astar(P s, P g) {
	bool used[1000][1000] = { false };
	priority_queue<Ele> que;
	que.push(Ele(0, euclidean(s, g), s));

	while (!que.empty()) {
		Ele t = que.top();
		que.pop();

		if (t.p == g)
			return t.g;
		if (used[t.p.first][t.p.second])
			continue;
		used[t.p.first][t.p.second] = true;

		for (int i = 0; i < 4; ++i) {
			int ny = t.p.first + dy[i];
			int nx = t.p.second + dx[i];
			if (ny < 0 || ny >= h || nx < 0 || nx >= w || f[ny][nx] == 'X')
				continue;

			P np(ny, nx);
			que.push(Ele(t.g + 1, euclidean(np, g), np));
		}
	}

	return 0;
}

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

	P fac[10];
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			cin >> f[i][j];
			if ('1' <= f[i][j] && f[i][j] <= '9')
				fac[f[i][j] - '0'] = P(i, j);
			else if (f[i][j] == 'S')
				fac[0] = P(i, j);
		}
	}

	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans += Astar(fac[i], fac[i + 1]);
	}

	cout << ans << endl;

	return 0;
}