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<vector>
#include<algorithm>
#include<string>
#include<queue>

#define mp make_pair
#define f first
#define s second

using namespace std;

typedef pair<int,int> P;
typedef pair<int,P> T;

const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int h,w;
string grid[1000];

int bfs(P S,P G){

  queue<T>que;
  que.push(T(0,S));

  while(!que.empty()){
    T now=que.front();
    if(now.s.f==G.f && now.s.s==G.s)return now.f;
    grid[now.s.f][now.s.s]='X';
    
    que.pop();
    for(int i=0;i<4;i++){
      if(0<=now.s.f+dx[i] && 0<=now.s.s+dy[i] &&
	 now.s.f+dx[i]<h && now.s.s+dy[i]<w &&
	 grid[now.s.f+dx[i]][now.s.s+dy[i]]!='X'){
	que.push(T(now.f+1,P(now.s.f+dx[i],now.s.s+dy[i])));
      }
    }
  }
}

int main(void){
  int n;
  string in[1000];
  P v[1001];

  cin >> h >> w >> n;

  for(int i=0;i<h;i++)cin >> in[i];

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(in[i][j]=='S')v[0]=mp(i,j);
      if(in[i][j]!='X'&&in[i][j]!='.')
	v[in[i][j]-'0']=mp(i,j);
    }
  }

  int ans=0;
  for(int i=0;i<n;i++){
    for(int j=0;j<h;j++)grid[j]=in[j];
    ans+=bfs(v[i],v[i+1]);
  }

  cout << ans << endl;

  return 0;
}

C++: #define REP(i,n) for (int i=0;i<(n);i++)
#include <queue>
#include <iostream>
using namespace std;
const int MAX = 1000;
const int INF = 10000;
typedef pair<int,int> P; //??§?¨??????????

int N,M,c_num;
int sx,sy;
int gx,gy;
P C[9];
char field[MAX][MAX+1]; //field
int d[MAX][MAX+1]; //distance

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


void init(){
    // cout << "init ..." << endl;
	REP(i,N){
		REP(j,M){
			if (! (i == sx && j == sy)) d[i][j] = INF;
		}
	}
	// cout << "start: " << d[sx][sy] << endl;
}


void bfs(){
    
    init();

	queue <P> que;
	que.push(P(sx,sy));

	while(que.size()){
		P p = que.front(); que.pop(); 

		if (p.first == gx && p.second == gy) break; 

		// ??¨???4??????????????????
		for(int r = 0; r < 4; r++) {
	    	int nx = p.first + dx[r], ny = p.second + dy[r]; 
	    	if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != 'X' &&d[nx][ny] == INF){
	    		d[nx][ny] = d[p.first][p.second] + 1;
	    		que.push(P(nx,ny));
	    	}
		}
	}
	return ;
}


void solve(){
	REP(i,c_num){
		gx = C[i].first, gy = C[i].second;
		//cout <<???'goal :'<< gx << gy << endl;
		bfs();
		sx = gx, sy = gy;
	}
	cout << d[gx][gy] << endl;
}


int main(){
	cin >> N >> M >> c_num;
	REP(i,N){
		REP(j,M){
			cin >> field[i][j];
			if (field[i][j] == 'S') sx = i, sy = j;
			else if ('1' <= field[i][j] && field[i][j] <= '9'){
			    C [field[i][j]-'1'] = P(i,j);
			}
		}
	}
	solve();
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <cstdio>
#include <string.h>
#include <cstring>

using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)

typedef long long ll;
//各地点の行と列をPairで持たせる
typedef pair<int,int> P;
int y,x,n,nx,ny,nextx,nexty;
int ans = 0;
int counter = 0;
int d[1100][1100];
char c[1100][1100];
int d1[] = {0,0,-1,1};
int d2[] = {1,-1,0,0};
int dc = 0;
char key[] = "S123456789";
P st[10];

int bfs(P,P);

int main(){
    cin >> y >> x >> n;
    REP(i,y){
        cin >> c[i];
    }
    REP(i,y){
        REP(j,x){
            REP(k,n+1) {
                if(c[i][j] == key[k]) st[k] = P(i,j); 
            }
        }
    }
   
    REP(i,n){
        memset(d,-1,sizeof(d));
      
        ans += bfs(st[i],st[i+1]);
      
    }
    cout << ans << endl;
    return 0;
}

int bfs(P s, P g){
    queue<P> que;
    que.push(s);
    d[s.first][s.second] = 0;
    while(que.size()){
        P p = que.front(); que.pop();
        ny = p.first;
        nx = p.second;
        dc = d[ny][nx];
       
        if(p == g) break;
 
        
        REP(i,4){
            nextx = nx + d1[i];
            nexty = ny + d2[i];

            if(nexty < 0 || nexty >= y || nextx < 0 || nextx >= x) continue;
           
            if(c[nexty][nextx] == 'X' || d[nexty][nextx] != -1) continue;
	
            que.push(P(nexty,nextx));
     
            d[nexty][nextx] = dc + 1;
        }
        
    }
    return d[g.first][g.second];

}

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

char map[1000][1000];
int check[1000][1000];
int cx[10];
int cy[10];

int main(){
	int i,j,k,l,h,w,ch,pd=0;
	int tex,tey,ted;
	std::queue<int> qx;
	std::queue<int> qy;
	std::queue<int> qd;

	scanf("%d %d %d",&h,&w,&ch);
	scanf("%c",&map[0][0]);
	for(i=0;i<h;i++){
		for(j=0;j<=w;j++){
			scanf("%c",&map[i][j]);
			if(map[i][j]=='S'){cy[0]=i; cx[0]=j;}
			if(map[i][j]=='1'){cy[1]=i; cx[1]=j;}
			if(map[i][j]=='2'){cy[2]=i; cx[2]=j;}
			if(map[i][j]=='3'){cy[3]=i; cx[3]=j;}
			if(map[i][j]=='4'){cy[4]=i; cx[4]=j;}
			if(map[i][j]=='5'){cy[5]=i; cx[5]=j;}
			if(map[i][j]=='6'){cy[6]=i; cx[6]=j;}
			if(map[i][j]=='7'){cy[7]=i; cx[7]=j;}
			if(map[i][j]=='8'){cy[8]=i; cx[8]=j;}
			if(map[i][j]=='9'){cy[9]=i; cx[9]=j;}
		}
	}
	for(i=0;i<h;i++){
		for(j=0;j<=w;j++)check[i][j]=0;
	}

	i=0;
	while(i<ch){
		qx.push(cx[i]); qy.push(cy[i]); qd.push(0);
		++i;
		while(!qx.empty()){
			tex=qx.front(); tey=qy.front(); ted=qd.front();
			qx.pop(); qy.pop(); qd.pop();
			check[tey][tex]=1;
			if(tey==cy[i] && tex==cx[i]){
				pd+=ted;
				while(!qx.empty())qx.pop();
				while(!qy.empty())qy.pop();
				while(!qd.empty())qd.pop();
				for(k=0;k<h;k++){
					for(l=0;l<=w;l++)check[k][l]=0;
				}
				break;
			}
			++ted;
			if(tex+1<w && map[tey][tex+1]!='X' && check[tey][tex+1]==0){
				check[tey][tex+1]=1;
				qx.push(tex+1); qy.push(tey); qd.push(ted);
			}
			if(tex-1>=0 && map[tey][tex-1]!='X' && check[tey][tex-1]==0){
				check[tey][tex-1]=1;
				qx.push(tex-1); qy.push(tey); qd.push(ted);
			}
			if(tey+1<h && map[tey+1][tex]!='X' && check[tey+1][tex]==0){
				check[tey+1][tex]=1;
				qx.push(tex); qy.push(tey+1); qd.push(ted);
			}
			if(tey-1>=0 && map[tey-1][tex]!='X' && check[tey-1][tex]==0){
				check[tey-1][tex]=1;
				qx.push(tex); qy.push(tey-1); qd.push(ted);
			}
		}
	}

	printf("%d\n",pd);
	return 0;
}