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>
using namespace std;
int w, h, n;
char map[1024][1024];
// ??????????
int d[1024][1024];
const int direction[4][2] = {
	{ -1, 0 },
	{ 1, 0 },
	{ 0, -1 },
	{ 0, 1 },
};
 
int factory[16][2];
typedef pair<int, int> P;
 
 
 
//************************************
// Method:    bfs
// FullName:  bfs
// Access:    public 
// Returns:   int
// Qualifier:
// Parameter: const int & sx ??x
// Parameter: const int & sy ??y
// Parameter: const int & gx ??x
// Parameter: const int & gy ??y
//************************************
int bfs(const int& sx, const int& sy, const int& gx, const int& gy)
{
	//memset(d, -1, sizeof(d));
	for (int i = 0; i < h; ++i)
	{
		for (int j = 0; j < w; ++j)
		{
			d[j][i] = -1;
		}
	}
	queue<P> que;
	que.push(P(sx, sy));
	d[sx][sy] = 0;
	while (que.size())
	{
		const P p = que.front(); que.pop();
		// ????????
		if (p.first == gx && p.second == gy)
		{
			break;
		}
 
		// ?????
		for (int i = 0; i < 4; ++i)
		{
			int nx = p.first + direction[i][0];
			int ny = p.second + direction[i][1];
			// ??????,?????????
			if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx][ny] != 'X' && d[nx][ny] == -1)
			{
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
 
	return d[gx][gy];
}
 
///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
 
	cin >> h >> w >> n;
	for (int i = 0; i < h; ++i)
	{
		for (int j = 0; j < w; ++j)
		{
			cin >> map[j][i];
		}
	}
 
	for (int i = 0; i < h; ++i)
	{
		for (int j = 0; j < w; ++j)
		{
			if (map[j][i] == 'S')
			{
				factory[0][0] = j;
				factory[0][1] = i;
				map[j][i] = '.';
			}
			else if (isdigit(map[j][i]))
			{
				int index = map[j][i] - '0';
				factory[index][0] = j;
				factory[index][1] = i;
			}
		}
	}
 
	int step = 0;
	for (int i = 0; i < n; ++i)
	{
		// ???????
		step += bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);
	}
	
	cout << step << endl;
 
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <map>
#include <string>

using namespace std;

typedef struct P{
    int x;
    int y;
}P;


int h,w,n;

const int INF = 100000000;

char feld[1010][1010];
P ch[15];
int d[1010][1010];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int sx,sy;
int gx,gy;
int ans=0;

int bfs(){
    for(int i=0;i<n;i++){
        P s_xy=ch[i];
        sx=ch[i].x;
        sy=ch[i].y;
        gx=ch[i+1].x;
        gy=ch[i+1].y;
        for(int y=0;y<h;y++)
            for(int x=0;x<w;x++)
                d[y][x]=INF;
            queue<P> que;
            que.push(s_xy);
            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;
                }
                for(int i=0;i<4;i++){
                    int nx=x+dx[i];
                    int ny=y+dy[i];
                    if(nx<0||nx>=w||ny<0||ny>=h)continue;
                    if(feld[ny][nx]!='X'&&d[ny][nx]==INF){
                        P n;
                        n.x=nx;
                        n.y=ny;
                        d[ny][nx]=d[y][x]+1;
                        que.push(n);
                    }
                }
            }
        }
        return 0;
}

int main(){
    cin>>h>>w>>n;
    char c;
    for(int y=0;y<h;y++){
        for(int x=0;x<w;x++){
            scanf(" %c",&feld[y][x]);
            c=feld[y][x];
            if(feld[y][x]=='S'){
                ch[0].x=x;
                ch[0].y=y;

            }
            if(c>='1'&&c<='9'){
                ch[c-'0'].x=x;
                ch[c-'0'].y=y;

            }
        }
    }
    bfs();
    cout<<ans<<endl;
    return 0;
}

C++: #include<iostream>
#include<vector>

using namespace std;

void recur(int i,int j,vector<vector<char> > &rect,vector<vector<int> > &dp){
	try{
		if (rect.at(i - 1).at(j) != 'X' and dp[i-1][j]>dp[i][j]+1){
			dp[i - 1][j] = dp[i][j]+1;
			recur(i-1,j,rect,dp);
		}
	}
	catch (...){}
	try{
		if (rect.at(i + 1).at(j) != 'X' and dp[i+1][j]>dp[i][j]+1){
			dp[i + 1][j] = dp[i][j]+1;
			recur(i+1,j,rect,dp);
		}
	}
	catch (...){}
	try{
		if (rect.at(i).at(j-1) != 'X' and dp[i][j-1]>dp[i][j]+1){
			dp[i][j-1] = dp[i][j]+1;
			recur(i,j-1,rect,dp);
		}
	}
	catch (...){}
	try{
		if (rect.at(i).at(j+1) != 'X' and dp[i][j+1]>dp[i][j]+1){
			dp[i][j+1] = dp[i][j]+1;
			recur(i,j+1,rect,dp);
		}
	}
	catch (...){}
	return;
}

int trvrs(int sx,int sy,int gx,int gy,vector<vector<char> > &R,vector<vector<int> > dp){
	dp[sx][sy]=0;
	recur(sx,sy,R,dp);
	return dp[gx][gy];
}

int main(){
	int W, H ,N, count=0,idx[10][2];
	//Input
	cin >> H >> W >> N;
	vector<vector<char> > rect(H, vector<char>(W));
	vector<vector<int> > dp(H, vector<int>(W));
	for (int i = 0; i<H; i++){
		for (int j = 0; j<W; j++){
			cin >> rect[i][j];
			dp[i][j] =1000000;
			if('1' <= rect[i][j] and '9' >= rect[i][j]){idx[rect[i][j]-'0'][0]=i; idx[rect[i][j]-'0'][1]=j;} 
			if(rect[i][j]=='S'){idx[0][0]=i; idx[0][1]=j;}
		}
	}
	//Processing
	for(int i=0;i<N;i++){
		count+=trvrs(idx[i][0],idx[i][1],idx[i+1][0],idx[i+1][1],rect,dp);
	}
	//Output
	cout << count << endl;
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1010;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
struct node{
	int x,y;
	int dep;
};
node q[maxn*maxn];
int a[maxn][maxn],vis[maxn][maxn];
int x[10],y[10];
int h,w,n;
int bfs(int s,int t){
	memcpy(vis,a,sizeof(a));//vis=a;
	q[0]=(node){x[s],y[s],0};
	vis[x[s]][y[s]]=1;
	int head=0,tail=1;
	while(head<tail){
		node cur=q[head++];
		if(cur.x==x[t]&&cur.y==y[t]) return cur.dep;
		for(int i=0;i<4;i++){
			int xx=cur.x+dx[i],yy=cur.y+dy[i];
			if(xx>=0&&xx<h&&yy>=0&&yy<w&&!vis[xx][yy]){
				q[tail++]=(node){xx,yy,cur.dep+1};
				vis[xx][yy]=1;
			}
		}
	}
}
int main(){
	cin>>h>>w>>n;
	memset(a,0,sizeof(a));
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++){
			char ch;
			cin>>ch;
			if(ch=='S') {x[0]=i;y[0]=j;}
			if(ch=='X') a[i][j]=1;
			if(ch>='1'&&ch<='9'){x[ch-48]=i;y[ch-48]=j;}
		}
	int ans=0,s;
	for(int i=0;i<n;i++) ans+=bfs(i,i+1);
	cout<<ans<<endl;
	return 0;
}