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;
typedef pair<int, int> P;

int h, w, n;
P goal[10];
char map[1000][1000];
int dis[1000][1000];
const int vx[4] = { 1,0,-1,0 };
const int vy[4] = { 0,1,0,-1 };

int bfs(P start, P goal) {
	int ans = 0;
	queue<P> q;
	q.push(start);
	while (q.size()) {
		P tmp = q.front();
		q.pop();
		if (tmp == goal) {
			ans = dis[tmp.first][tmp.second];
			break;
		}
		for (int i = 0; i < 4; i++) {
			int fx = tmp.first + vx[i], fy = tmp.second + vy[i];
			if (fx < 0 || fy < 0 || fx >= h || fy >= w)continue;
			if (!dis[fx][fy] && start != make_pair(fx, fy) && map[fx][fy] != 'X') {
				dis[fx][fy] = dis[tmp.first][tmp.second] + 1;
				q.push(make_pair(fx, fy));
			}
		}
	}
	for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)dis[i][j] = 0;
	return ans;
}

int main() {
	cin >> h >> w >> n;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cin >> map[i][j];
			if (map[i][j] == 'S')goal[0] = make_pair(i, j);
			if (map[i][j] > '0' & map[i][j] <= '9') {
				goal[map[i][j] - '0'] = make_pair(i, j);
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		ans += bfs(goal[i], goal[i + 1]);
	}
	cout << ans << endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<cctype>


using namespace std;

int cnt;
char field[1002][1002];
int cost[1002][1002];
int factory[9][2];
int x[4] = {1, 0, -1, 0};
int y[4] = {0, 1, 0, -1};

int bfs(int sx, int sy, int gx, int gy) {
    queue< pair<int,int> > que;

    for(int i = 0; i < 1002; i++) {
        for (int j = 0; j < 1002; j++) {
            cost[i][j] = -1;
        }
    }

    que.push(make_pair(sx, sy));
    cost[sx][sy] = 0;

    while(!que.empty()) {
        pair<int, int> p;
        p = que.front();
        que.pop();
        
        if(p.first == gx && p.second == gy) {
            break;
        }

        for(int i = 0; i < 4; i++) {
            int tx = p.first + x[i]; int ty = p.second + y[i];
            if(field[tx][ty] != 'X' && cost[tx][ty] == -1) {
                que.push(pair<int, int>(tx, ty));
                cost[tx][ty] = cost[p.first][p.second] + 1;
            }
        }
    }
    return cost[gx][gy];
}

int main() {
    int w, h, n, sx, sy;

    cin >> h >> w >> n;

    for (int y = 1; y <= h; y++) {
        for (int x = 1; x <= w; x++) {
            cin >> field[x][y];
            if (field[x][y] == 'S') {
                sx = x;
                sy = y;
            } else if(isdigit(field[x][y])) {
                int tmp = field[x][y] - '0';
                factory[tmp][0] = x;
                factory[tmp][1] = y;
            }
        }
        field[0][y] = 'X';
        field[w + 1][y] = 'X';
    }

    for (int x = 0; x <= w + 1; x++) {
        field[x][0] = 'X';
        field[x][h + 1] = 'X';
    }

    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        // 次の目的地へ行くで
        cnt += bfs(sx, sy, factory[i][0], factory[i][1]);
        sx = factory[i][0];
        sy = factory[i][1];
    }

    cout << cnt << endl;
}

C++: #include<iostream>
#include<queue>
#define fi first
#define se second
using namespace std;
int dy[4]={0,0,-1,1},dx[4]={1,-1,0,0};
int X,Y,N;
typedef pair<int,int> P;
char field[1000][1001];
P time[1000][1001];
struct point{
    int y,x,t,n;
    point(int y_,int x_,int t_,int n_){
        y=y_;x=x_;t=t_;n=n_;
    }
};
int bfs(int,int);
int main(){
    cin>>Y>>X>>N;
    int rep;
    for(int i=0;i<Y;i++)cin>>field[i];
    for(int i=0;i<Y;i++){
        for(int j=0;j<X;j++){
            time[i][j].fi=time[i][j].se=-1;
        }
    }
    for(int i=0;i<Y;i++){
        bool flag=false;
        for(int j=0;j<X;j++){
            if(field[i][j]=='S'){
                rep=bfs(i,j);
                flag=true;
                break;
            }
        }
        if(flag)break;
    }
    cout<<rep<<endl;
    return 0;

}

int bfs(int y,int x){
    point start=point(y,x,0,0);
    queue<point>Q;
    Q.push(start);
    while(Q.size()){

        point a=Q.front();
        Q.pop();
        if(a.y<0 || a.y>=Y ||a.x<0||a.x>=X)continue;

        if(field[a.y][a.x]=='X')continue;

        if(a.n+1==field[a.y][a.x]-'0')a.n++;

        if(a.n>time[a.y][a.x].se){
            time[a.y][a.x].fi=a.t;
            time[a.y][a.x].se=a.n;
        }else if(a.n==time[a.y][a.x].se&&a.t<time[a.y][a.x].fi){
            time[a.y][a.x].fi=a.t;
        }else continue;

        if(a.n==N){
            return a.t;
        }


        for(int i=0;i<4;i++){
            point next=point(a.y+dy[i],a.x+dx[i],a.t+1,a.n);
            Q.push(next);
        }


    }



}

C++: // No.3

#include<iostream>
#include<map>
#include<queue>

#define INF 1000000
using namespace std;

typedef pair<int ,int> P;

char map_data[1001][1001];
int data[1001][1001];
int W,H;

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int bfs (P s,P g){
	fill((int*) data,(int*)(data +1001),INF);
	data[s.first][s.second]=0;
	queue<P> que;

	que.push(s);
	while(que.size()){
		P p=que.front();que.pop();
		
		for(int i=0;i<4;i++){
			int nx=p.second+dx[i],ny=p.first+dy[i];
			if(0<=nx && nx<W && 0<=ny && ny<H && map_data[ny][nx]!='X' && data[p.first][p.second]+1<data[ny][nx]){
				que.push(P(ny,nx));
				data[ny][nx]=data[p.first][p.second]+1;
				
			}

		}

	}
return data[g.first][g.second];	
}

int main(){
int n;
cin>>H>>W>>n;

P ar[10];

for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	cin>>map_data[i][j];
	if(map_data[i][j]!='.' && map_data[i][j]!='X'){
		if(map_data[i][j]=='S')ar[0]=P(i,j);
		else{
		ar[map_data[i][j]-'0']=P(i,j);
		}
	}
	}
}
int ans=0;
for(int i=0;i<n;i++){
ans+=bfs(ar[i],ar[i+1]);
}

cout<<ans<<endl;
}