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 <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
struct T{
	ll first,second,third;
};
typedef double db;
string c[1001];
const ll INF = 0xffffffffff;
P s[10],g[10];
ll vx[4]={1,0,-1,0};
ll vy[4]={0,1,0,-1};
ll n,h,w;
bool visited[1001][1001];

ll bfs(ll sx,ll sy,ll gx,ll gy){
	queue<T> q;
	while(!q.empty()) q.pop();
	q.push((T){sx,sy,0});
	visited[sy][sx]=1;
	while(1){
		T t=q.front();
		q.pop();
		if(t.first==gx&&t.second==gy) return t.third;
		for(ll i=0;i<4;i++){
			ll nx=t.first+vx[i];
			ll ny=t.second+vy[i];
			if(0<=nx&&nx<w&&0<=ny&&ny<h&&c[ny][nx]=='.'&&!visited[ny][nx]){
				q.push((T){nx,ny,t.third+1});
				visited[ny][nx]=1;
			}
		}
	}
}


int main()
{
	scanf("%lld%lld%lld",&h,&w,&n);
	for(ll i=0;i<h;i++) cin>>c[i];
	for(ll i=0;i<h;i++) for(ll j=0;j<w;j++){
		if(c[i][j]=='S'){
			s[0]=P(i,j);// y x
			c[i][j]='.';
		}
		if('1'<=c[i][j]&&c[i][j]<='9'){
			s[c[i][j]-'0']=P(i,j);
			g[c[i][j]-'0'-1]=P(i,j);
			c[i][j]='.';
		}
	}
	ll ans=0;
	for(ll i=0;i<n;i++){
		fill(visited[0],visited[1000],0);
		ans+=bfs(s[i].second,s[i].first,g[i].second,g[i].first);
	}
	printf("%lld\n",ans);
}

C++: // AOJ0558.cpp : ??????????????? ??¢????????±????????§????????¨????????? ?????????????????????????????????

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
//Global
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 9;
const int INF = 100000000;
char maze[MAX_H][MAX_W + 1];
int d[MAX_H][MAX_W];
int H, W, N;
int sx, sy;
int gx, gy;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
P dot[MAX_N + 1];

//solve
int solve() {
	//S(0)-->1 1-->2 ....N-1-->N??????????????¢????±?????????????
	int sumDistance = 0;
	for (int i = 0; i < N; i++) {
		sx = dot[i].first;
		sy = dot[i].second;
		gx = dot[i + 1].first;
		gy = dot[i + 1].second;

		//??¨??????????????????????????????
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				d[i][j] = INF;
			}
		}

		queue<P> que;
		que.push(P(sx, sy));
		d[sy][sx] = 0;//??????????????°??????0?????????

		//?????\???????????????????????§?????????
		while (que.size()) {
			//?????\???????????????????????????
			P p = que.front(); que.pop();
			//??????????????????????????\????????´??????????????°???????????????
			if (p.first == gx && p.second == gy)break;
			//?§????4??????
			for (int i = 0; i < 4; i++) {
				int nx = p.first + dx[i];
				int ny = p.second + dy[i];
				if (0 <= nx && nx < W && 0 <= ny && ny < H && maze[ny][nx] != 'X' && d[ny][nx] == INF) {
					que.push(P(nx, ny));
					d[ny][nx] = d[p.second][p.first] + 1;
				}
			}
		}
		//cout << i <<"-->" << i+1 << "????????¢" << d[gy][gx] << endl;
		sumDistance += d[gy][gx];
	}
	return sumDistance;
}
	
	
	
//main
int main()
{
	//input
	cin >> H >> W >> N;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> maze[i][j];
			if (maze[i][j] == 'S') {
				dot[0] = P(j, i);
			}
			else {
				switch (maze[i][j]) {
				case '1':dot[1] = P(j, i); break;
				case '2':dot[2] = P(j, i); break;
				case '3':dot[3] = P(j, i); break;
				case '4':dot[4] = P(j, i); break;
				case '5':dot[5] = P(j, i); break;
				case '6':dot[6] = P(j, i); break;
				case '7':dot[7] = P(j, i); break;
				case '8':dot[8] = P(j, i); break;
				case '9':dot[9] = P(j, i); break;
				}
			}
		}
	}


	//cout << "========================" << endl;
	/*
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cout << maze[i][j];
		}
		cout << endl;
	}
	*/

	/*
	for (int i = 0; i <=  N; i++) {
		cout << "dot[" << i << "]=" << dot[i].first << "," << dot[i].second << endl;
	}
	cout << "===================================" << endl;
	*/
	cout << solve() << endl;
	
	return 0;
}

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(){
    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;
        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<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/4+2;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;
}