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<queue>
#include<cctype>
#include<cstdio>
 
#define mp          make_pair
#define rep(i,n)    for(int i=0;i<n;i++)
 
using namespace std;
 
typedef pair<int,int> pii;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
 
int h,w;
char grid[1000][1001];
 
int shortestDistance(int sx,int sy,int gx,int gy){
    static bool visited[1000][1000];
    rep(i,h)rep(j,w)    visited[i][j]=false;
    visited[sy][sx]=true;
 
    queue< pair<int,pii> > qu;  qu.push(mp(0,mp(sy,sx)));
    while(1){
        pair<int,pii> a=qu.front();   qu.pop();
        int d=a.first,x=a.second.second,y=a.second.first;
        if(x==gx && y==gy)  return d;
 
        rep(i,4){
            int xx=x+dx[i],yy=y+dy[i];
            if(0<=xx && xx<w && 0<=yy && yy<h && grid[yy][xx]!='X' && !visited[yy][xx]){
                visited[yy][xx]=true;
                qu.push(mp(d+1,mp(yy,xx)));
            }
        }
    }
}
 
int main(){
    int n;  scanf("%d%d%d",&h,&w,&n);
    int cx[10],cy[10];
    rep(i,h){
        scanf("%s",grid[i]);
        rep(j,w){
            if(grid[i][j]=='S')     cx[0]=j,cy[0]=i;
            if(isdigit(grid[i][j])) cx[grid[i][j]-'0']=j,cy[grid[i][j]-'0']=i;
        }
    }
 
    int cost=0;
    rep(i,n)    cost+=shortestDistance(cx[i],cy[i],cx[i+1],cy[i+1]);
    printf("%d\n",cost);
 
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n,m,k;
int food[15];
int a[1005000];
queue<int> q;

int run(int i,int i0){
	queue<int> empty;
	swap(q,empty);
	for(int i1=0;i1<n*m;i1++){
		if(a[i1]>0)a[i1]=0;
	}
	q.push(i);
	a[i]=1;
	while(!q.empty()){
		i = q.front();
		q.pop();
//		printf("from:%d %d %d\n",i/m,i%m,a[i]);
		if(i==i0)return a[i]-1;
		for(int di=-1;di<=1;di++){
			for(int dj=-1;dj<=1;dj++){
				if(((di^dj)&1)==0)continue;
				if(a[i+di*m+dj]!=0)continue;
				a[i+di*m+dj] = a[i] + 1;
//				printf("\tto:%d %d %d\n",i/m+di,i%m+dj,a[i+di*m+dj]);
				q.push(i+di*m+dj);
			}
		}
	}
	return -1;
}

int main(){
	cin>>n>>m>>k; 
	string s;
	m+=2;n+=2;
	for(int i=0;i<n;i++){
		if(i%(n-1)!=0)cin>>s;
		for(int j=0;j<m;j++){
			if(i%(n-1)==0||j%(m-1)==0)a[i*m+j]=-1;
			else{
				a[i*m+j] = s[j-1]=='X'?-1:0;
				if(s[j-1]>'0'&&s[j-1]<='9'){
					food[s[j-1]-'0']=i*m+j;
				}else if(s[j-1]=='S'){
					food[0]=i*m+j;
				}
			}
		}
	}
	
	int end = 0;
	for(int i=1;i<=k;i++){
		end += run(food[i-1],food[i]);
//		printf("%d   step:%d\n",i,run(food[i-1],food[i]));
//		for(int x=0;x<n;x++){
//			for(int y=0;y<m;y++){
//				printf("%d\t",a[x*m+y]);
//			}
//			printf("\n");
//		} 
	}
	cout<<end<<endl;
	return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999


int h,w,n;
string data[1100];

//void dfs(int y,int x,int fie[1100][1100],bool used[1100][1100],int dir){
//    fie[y][x]=dir;
//    int dx[]={0,1,0,-1};
//    int dy[]={1,0,1,-1};
//    rep(i,4){
//        int ddy=y+dy[i];
//        int ddx=x+dx[i];
//        if(ddy>=0 && ddy<h && ddx>=0 && ddx<w && fie[ddy][ddx]!='X' && !used[ddy][ddx]){
//            used[ddy][ddx]=true;
//            dfs(ddy,ddx,fie,used,dir+1);
//        }
//    }
//}

int main(){
    while(cin>>h>>w>>n){
        rep(i,h)cin>>data[i];
        
        pii ce[10];
        rep(i,h)rep(j,w){
            if(data[i][j]=='S'){
                ce[0]=pii(i,j);
            }
            if(isdigit(data[i][j])){
                ce[data[i][j]-'0']=pii(i,j);
            }
        }
        int sum=0;
        rep(i,n){
            int fie[1100][1100];
            rep(i,1100)rep(j,1100)fie[i][j]=INF;
            bool used[1100][1100]={};

            fie[ce[i].first][ce[i].second]=0;
            used[ce[i].first][ce[i].second]=true;
            queue<pii> que;
            que.push(ce[i]);
            while(que.size()){
                int y=que.front().first;
                int x=que.front().second;
                que.pop();
                int dx[]={0,1,0,-1};
                int dy[]={1,0,-1,0};
                rep(i,4){
                    int ddy=y+dy[i];
                    int ddx=x+dx[i];
                    if(ddy>=0 && ddy<h && ddx>=0 && ddx<w && data[ddy][ddx]!='X' && !used[ddy][ddx]){
                        used[ddy][ddx]=true;
                        fie[ddy][ddx]=fie[y][x]+1;
                        que.push(pii(ddy,ddx));
                    }
                }
            }
            
            
            sum+=fie[ce[i+1].first][ce[i+1].second];
        }
        cout<<sum<<endl;
    }
}