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<queue>
#include<algorithm>
using namespace std;
struct k{
	int x,y,cnt;
};
int i,i2,xx,yy,X,Y,map[1001][1001],b[1001][1001],mi,st[11][3],t,cntsum=0;
int main(){
	k ii;
	queue<k> Q;
	char d,z[1001][1001];
	cin>>Y>>X>>t;
	for(i=0;i<Y;i++){
		for(i2=0;i2<X;i2++){
			map[i2][i]=0;
			cin>>d;
			switch (d){
				case 'S':
					st[0][1]=i2;
					st[0][2]=i;
					break;
				case '.':
					break;
				case 'X':
					map[i2][i]=-1;
					break;
				case '1':
					st[1][1]=i2;
					st[1][2]=i;
					break;
				case '2':
					st[2][1]=i2;
					st[2][2]=i;
					break;
				case '3':
					st[3][1]=i2;
					st[3][2]=i;
					break;
				case '4':
					st[4][1]=i2;
					st[4][2]=i;
					break;
				case '5':
					st[5][1]=i2;
					st[5][2]=i;
					break;
				case '6':
					st[6][1]=i2;
					st[6][2]=i;
					break;
				case '7':
					st[7][1]=i2;
					st[7][2]=i;
					break;
				case '8':
					st[8][1]=i2;
					st[8][2]=i;
					break;
				case '9':
					st[9][1]=i2;
					st[9][2]=i;
					break;
			}
		}
	}
	/*for(i=0;i<Y;i++){
		for(i2=0;i2<X;i2++){
			cout<<map[i2][i]<<' ';
		}
	cout<<endl;
	}
	cout<<endl;*/
	int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
	for(int w=0;w<t;w++){
		for(int ww=0;ww<Y;ww++)for(int www=0;www<X;www++)b[www][ww]=0;
		mi=999999999;
		ii.x=st[w][1];
		ii.y=st[w][2];
		ii.cnt=0;
		Q.push(ii);
		b[st[w][1]][st[w][2]]=1;
		map[st[w+1][1]][st[w+1][2]]=2;

		while(!Q.empty()){
			for(int I=0;I<4;I++){
				ii=Q.front();
				xx=ii.x+dx[I];
				yy=ii.y+dy[I];
				if(xx>=0&&yy>=0&&xx<X&&yy<Y&&map[xx][yy]!=-1&&b[xx][yy]!=1){
					ii.x=xx;
					ii.y=yy;
					ii.cnt++;
					Q.push(ii);
					b[xx][yy]=1;
		
					/*for(i=0;i<Y;i++){
						for(i2=0;i2<X;i2++){cout<<b[i2][i]<<' ';
						}
						cout<<endl;
					}
					cout<<"x="<<ii.x<<' '<<"y="<<ii.y<<' '<<"cnt="<<ii.cnt<<endl;*/
		
				}
				if(map[ii.x][ii.y]==2){
					mi=min(mi,ii.cnt);
					//cout<<"x="<<ii.x<<' '<<"y="<<ii.y<<' '<<"cnt="<<ii.cnt<<endl;
				}
			}
			Q.pop();
		}

		map[st[w+1][1]][st[w+1][2]]=0;
		cntsum+=mi;
	}
	cout<<cntsum<<endl;
}

C++: #include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <vector>
#include <set>
#include <queue>

using namespace std;

int h,w,n;
char s[1252];
int mp[1252][1252];
int ys[11];
int xs[11];
bool used[1252][1252];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

int bfs(int sy,int sx,int gy,int gx){
	queue<int> qy,qx,qdist;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++)
		used[i][j]=false;
	qy.push(sy);
	qx.push(sx);
	qdist.push(0);
	used[sy][sx] = true;
	while(!qy.empty()){
		int y,x,dist;
		y = qy.front(); qy.pop();
		x = qx.front(); qx.pop();
		dist = qdist.front(); qdist.pop();
		if(gy==y && gx==x)return dist;
		for(int i=0;i<4;i++){
			int ny,nx,ndist;
			ny = y+dy[i];
			nx = x+dx[i];
			ndist = dist+1;
			if(ny<0 || ny>=h || nx<0 || nx>=w)continue;
			if(used[ny][nx])continue;
			if(mp[ny][nx]==1)continue;
			used[ny][nx] = true;
			qy.push(ny);
			qx.push(nx);
			qdist.push(ndist);
		}
	}
	return -1;
}

int main(){
	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<h;i++){
		scanf("%s",s);
		for(int j=0;j<w;j++){
			mp[i][j] = (s[j]=='X'?1:0);
			if(s[j]!='X' && s[j]!='.'){
				if(s[j]=='S'){
					ys[0]=i;
					xs[0]=j;
				}else{
					ys[s[j]-'0']=i;
					xs[s[j]-'0']=j;
				}
			}
		}
	}
	int ans = 0;
	for(int i=0;i<n;i++){
		ans += bfs(ys[i],xs[i],ys[i+1],xs[i+1]);
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include <iostream>
#include <string>
#include <queue>
int a[1005][1005];
bool tf[1005][1005];
int p[10][2];
int dirc[4][2] = {{-1 , 0} , {1 , 0} , {0 , -1} , {0 , 1}};
int w , h , n;
struct node{
    int x , y , step;
};
int bfs(int st_y , int st_x , int en_y , int en_x){
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            tf[i][j] = false;
        }
    }
    tf[st_y][st_x] = true;    
    std::queue <node> que;
    node node0;
    node0.x = st_x , node0.y = st_y , node0.step = 0;
    que.push(node0);
    while(!que.empty()){
        node0 = que.front();
        que.pop();
        if(node0.x == en_x && node0.y == en_y){
            return node0.step;
        }
        for(int i = 0;i < 4;i++){
            node node1;
            node1.y = node0.y + dirc[i][0] , node1.x = node0.x + dirc[i][1] , node1.step = node0.step + 1;
            if(node1.x >= 0 && node1.x < w && node1.y >= 0 && node1.y < h && a[node1.y][node1.x] >= 0 && tf[node1.y][node1.x] == false){
                tf[node1.y][node1.x] = true;
                que.push(node1);
            }
        }
    }
    return 0;
}
int main(){
    std::cin >> h >> w >> n;
    for(int i = 0;i < h;i++){
        std::string s;
        std::cin >> s;
        for(int j = 0;j < w;j++){
            switch(s[j]){
                case 'S':
                    a[i][j] = 0 , p[0][0] = i , p[0][1] = j;
                    break;
                case '.':
                    a[i][j] = 0;
                    break;
                case '1':
                    a[i][j] = 1; p[1][0] = i , p[1][1] = j;
                    break;
                case '2':
                    a[i][j] = 2; p[2][0] = i , p[2][1] = j;
                    break;
                case '3':
                    a[i][j] = 3; p[3][0] = i , p[3][1] = j;
                    break;
                case '4':
                    a[i][j] = 4; p[4][0] = i , p[4][1] = j;
                    break;
                case '5':
                    a[i][j] = 5; p[5][0] = i , p[5][1] = j;
                    break;
                case '6':
                    a[i][j] = 6; p[6][0] = i , p[6][1] = j;
                    break;
                case '7':
                    a[i][j] = 7; p[7][0] = i , p[7][1] = j;
                    break;
                case '8':
                    a[i][j] = 8; p[8][0] = i , p[8][1] = j;
                    break;
                case '9':
                    a[i][j] = 9; p[9][0] = i , p[9][1] = j;
                    break;
                default:
                    a[i][j] = -1;
                    break;
            }
        }
    }

    int ans = 0;
    for(int i = 1;i <= n;i++){
        ans += bfs(p[i - 1][0] , p[i - 1][1] , p[i][0] , p[i][1]);
    }
    std::cout << ans << "\n";
    return 0;
}