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;

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

const Point DIRE4[4] = {
	{0, -1},
	{-1, 0},
	{0, 1},
	{1, 0}
};

int H, W, N;
char field[1000][1000];
int dist[1000][1000];

int seek(Point &S, Point &G)
{
	
	for(int y = 0;y < H;y++)
	{
		for(int x = 0;x < W;x++)
		{
			dist[y][x] = -1;
		}
	}
	
	dist[S.y][S.x] = 0;
	
	queue<Point> pointQ;
	Point start = {S.x, S.y};
	pointQ.push(start);
	
	while(!pointQ.empty())
	{
		Point p = pointQ.front();
		pointQ.pop();
		
		if(p.x == G.x && p.y == G.y) break;
		
		for(int i = 0;i < 4;i++)
		{
			int tx = p.x + DIRE4[i].x;
			int ty = p.y + DIRE4[i].y;
			
			if(0 <= tx && tx < W && 0 <= ty && ty < H && field[ty][tx] != 'X')
			{
				if(dist[ty][tx] == -1 || dist[p.y][p.x] + 1 < dist[ty][tx])
				{
					dist[ty][tx] = dist[p.y][p.x] + 1;
					
					Point set = {tx, ty};
					pointQ.push(set);
				}
			}
		}
	}
	
	return dist[G.y][G.x];
}

int main()
{
	int ans = 0;
	Point P[1 + 9]; //start + goal
	
	cin >> H >> W >> N;
	
	for(int y = 0;y < H;y++)
	{
		for(int x = 0;x < W;x++)
		{
			char in;
			cin >> in;
			if(in == 'S'){ P[0].x = x; P[0].y = y; }
			else if('1' <= in && in <= '9')
			{
				int index = in - '1';
				P[index + 1].x = x;
				P[index + 1].y = y;
			}
			
			field[y][x] = in;
		}
	}
	
	for(int i = 0;i < N;i++)
	{
		ans += seek(P[i], P[i + 1]);
	}
	
	cout << ans << endl;
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
struct P{
	int y;
	int x;
	int ans;

};

void print(const P & p){
	cout<<p.y<<","<<p.x<<","<<p.ans<<endl;
}

queue<P> que;
P sg[10]={};
int def(char tebl[][1000],int nezu,int h,int w,int n){
	while(que.size()){
		P p=que.front();
		
		//printf("%d %d %d %d %d\n",nezu, p.y, p.x, p.ans, que.size());
		
		que.pop();
		if(p.y>0&&tebl[p.y-1][p.x]=='.'){//up
			que.push(P{p.y-1,p.x,p.ans+1});
			tebl[p.y-1][p.x]='$';
		}
		if(p.y+1<h&&tebl[p.y+1][p.x]=='.'){//down
			que.push(P{p.y+1,p.x,p.ans+1});
			tebl[p.y+1][p.x]='$';
		}
		if(p.x>0&&tebl[p.y][p.x-1]=='.'){//left
			que.push( P{p.y,p.x-1,p.ans+1});
			tebl[p.y][p.x-1]='$';
		}
		if(p.x+1<w&&tebl[p.y][p.x+1]=='.'){//right
			que.push(P{p.y,p.x+1,p.ans+1});
			tebl[p.y][p.x+1]='$';
		}
		if(p.y==sg[nezu].y&&p.x==sg[nezu].x){//リセット
			if(n==nezu){
				return p.ans;
			}
			nezu++;
			for(int l=0;l<h;l++){
				for(int m=0;m<w;m++){
					if(tebl[l][m]=='$'){
						tebl[l][m]='.';
					}
				}
			}
			while(que.size()){
				que.pop();
			}
			que.push(P{p.y,p.x,p.ans});
			tebl[p.y][p.x]='$';
		}
	}
	return 0;
}





int main(){
	int n,h,w,k;
	static char tebl[1000][1000]={};
	cin>>h>>w>>n;
	for(int p=0;p<h;p++){
		for(int m=0;m<w;m++){
			cin>>tebl[p][m];
			if(tebl[p][m]=='S'){
				que.push(P{p,m,0});
				tebl[p][m]='$';
			}
			if(tebl[p][m]>='1'&&tebl[p][m]<='9'){
				sg[tebl[p][m]-'1'+1]=(P{p,m,0});
				tebl[p][m]='.';
			}
		}
	}
	cout<<def(tebl,1,h,w,n)<<endl;
}

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

using namespace std;

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


int h,w;

const int INF = 100000000;

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

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
}

int main(){
    cin>>w>>h>>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;

int nowS = 0;
int x, y, n;
int mp[1000][1000] = {};
int k[4] = {1, -1, 0, 0};
int h[4] = {0, 0, 1, -1};
int cx[9] = {};
int cy[9] = {};
vector<int> bufX, bufY, bufD;

bool search(int xx, int yy, int dd) {
	bufX.erase(bufX.begin());
	bufY.erase(bufY.begin());
	bufD.erase(bufD.begin());


	mp[yy][xx] = nowS + 1;
	for (int i = 0; i < 4; i++) {
		if (xx + k[i] < 0 || xx + k[i] >= x || yy + h[i] < 0 || yy + h[i] >= y)
			continue;
		if (mp[yy + h[i]][xx + k[i]] == -1) {
			continue;
		}
		if (cx[nowS] == xx + k[i] && cy[nowS] == yy + h[i]) {
			nowS++;
			bufX.clear();
			bufY.clear();
			bufD.clear();
			if (nowS == n) {
				cout << dd + 1 << endl;
				return true;
			}
			else {
				bufX.push_back(xx + k[i]);
				bufY.push_back(yy + h[i]);
				bufD.push_back(dd + 1);
			}
			return false;
		}
		if(mp[yy + h[i]][xx + k[i]] >= 0 && mp[yy + h[i]][xx + k[i]] < nowS + 1){
			bufX.push_back(xx + k[i]);
			bufY.push_back(yy + h[i]);
			bufD.push_back(dd + 1);
		}
	}
	return false;
}

int main() {
	int ch = 0;
	cin >> y >> x >> n;
	for (int i = 0; i < y; i++) {
		char c[1001];
		cin >> c;
		for (int j = 0; j < x; j++) {
			if(c[j] == 'X')
				mp[i][j] = -1;
			else if(c[j] == 'S'){
				bufX.push_back(j);
				bufY.push_back(i);
				bufD.push_back(0);
				mp[i][j] = 0;
			}	
			else {
				mp[i][j] = 0;
			}
			if (c[j] >= 48 && c[j] <= 57) {
				int m;
				m = c[j] - 49;
				cx[m] = j;
				cy[m] = i;		
				ch++;
			}
		}
	}
	

	while(!search(bufX[0], bufY[0], bufD[0])){}

	return 0;
}