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 <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
typedef pair<P, int> T;
char table[1000][1000];
bool done[1000][1000];
int H,W,N;
vector<P> cur;
vector<P> nex;
T search(T s, char goal)
{
	memset(done,0,sizeof(done));
	cur.clear();
	int dist=s.second;
	cur.push_back(s.first);
	int dx[4]={0,1,0,-1};
	int dy[4]={1,0,-1,0};
	while(true){
		nex.clear();
		dist++;
		for(int i=0;i<cur.size();i++){
			int x=cur[i].first;
			int y=cur[i].second;
			for(int j=0;j<4;j++){
				int xx=x+dx[j];
				int yy=y+dy[j];
				if(0<=xx&&xx<H&&
				   0<=yy&&yy<H&&
				   !done[xx][yy]){
					done[xx][yy]=true;
					if(table[xx][yy]=='X')
					{
					}
					else if(table[xx][yy]==goal){
						return T(P(xx,yy), dist);
					}
					else
					{
						nex.push_back(P(xx,yy));
					}
				}
			}
		}
		nex.swap(cur);
	}
}
int main() {
	cin >> H >> W >> N;
	T s;
	for(int i=0;i<H;i++){
		scanf("%s", table[i]);
		for(int j=0;j<W;j++){
			if(table[i][j]=='S'){
				s=T(P(i,j),0);
			}
		}
	}
	for(int i=0;i<N;i++){
		s=search(s,'0'+i+1);
	}
	cout << s.second << endl;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1002][1002],C[11]={"0123456789"};
int D[1002][1002];
int A[1002][1002],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
	memset(A,0,sizeof(A));
   memset(D,0,sizeof(D));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			A[S.y][S.x]=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();

		S.x+=x[l];
		S.y+=y[l];
		S.Z++;

		if(A[S.y][S.x]==1||s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
		continue;
		}
				A[S.y][S.x]=1;
		if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			memset(A,0,sizeof(A));
			l=-1;

		} 
		P.push(S);
	}
	printf("%d\n",S.Z);
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
/*(^(^(^(^(^o^)^)^)^)^) &#9602;&#9605;&#9607;&#9608;&#9619;&#9618;&#9617;('ω')&#9617;&#9618;&#9619;&#9608;&#9607;&#9605;&#9602; 初のBFS*/
typedef pair<int,int> p;
char x[1001][1001];
int c[10]={},d[10],y[1001][1001],a[10],b[10];
int h,q,n,i,j,now=1,sum=0,pro[10];
int bfs(){
	int dx[]={1,0,-1,0};
	int dy[]={0,1,0,-1};
	queue<p>orz;
	for(i=1;i<=h;i++){
	for(j=1;j<=q;j++){
		y[i][j]=10000000;
		if(x[i][j]=='S'){
			a[0]=i;
			b[0]=j;
			y[i][j]=0;
		}
		if(x[i][j]-'0'>0 && x[i][j]-'0'<10){
			a[x[i][j]-'0']=i;
			b[x[i][j]-'0']=j;
		}}}
	for(i=1;i<=n;i++){
		printf("%d %d\n",a[i],b[i]);
	}
	for(int u=1;u<=n;u++){
		while(orz.size()){
			orz.pop();
		}
	for(i=1;i<=h;i++){
	for(j=1;j<=q;j++){
		y[i][j]=10000000;
		}}
	while(1){
		orz.push(p(a[u-1],b[u-1]));
		p w=orz.front();
		orz.pop();
		if(w.first==a[u]&&w.second==b[u]){
			printf("%d %d b\n",a[u],b[u]);
			pro[u]=y[a[u]][b[u]]-10000000;
			break;
		}
		for(i=0;i<4;i++){
			int ax=w.first+dx[i];
			int ay=w.second+dy[i];
			if(ax>=1&&ax<=h&&ay>=1&&ay<=q&&y[ax][ay]==10000000&&x[ax][ay]!='X'){
				orz.push(p(ax,ay));
				y[ax][ay]=y[w.first][w.second]+1;
				printf("%d %d %d\n",ax,ay,y[ax][ay]);
			}
		}
	}
	}
for(i=1;i<=n;i++){
		sum+=pro[i];
	}
return sum;
}
int main(){
scanf("%d %d %d",&h,&q,&n);
for(i=1;i<=h;i++){
scanf("%s",&x[i]);
}
for(i=1;i<=h;i++){
for(j=q-1;j>=0;j--){
	x[i][j+1]=x[i][j];
}}
int ans=bfs();
printf("%d\n",ans);
scanf("%d",&i);
return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>

using namespace std;

struct node
{
    int s;
    int x,y;
};

int dir[][2] = {0,1,1,0,0,-1,-1,0};
int vis[1010][1010];
int n,m,k,sx[20],sy[20];
char mp[1010][1010];

int bfs(int x,int y,int ex,int ey)
{
    queue<node> q;
    node a,b,d;
    a.x = x;
    a.y = y;
    a.s = 0;
    vis[x][y] = 1;
    q.push(a);
    while(!q.empty())
    {
        b = q.front();
        q.pop();
        if(b.x == ex && b.y == ey)
        {
            return b.s;
        }
        for(int i = 0; i < 4; i++)
        {
            d.x = b.x + dir[i][0];
            d.y = b.y + dir[i][1];
            if(d.x >= 0 && d.x < n && d.y >= 0 && d.y < m && vis[d.x][d.y] == 0)
            {
                if(mp[d.x][d.y] >= '1' && mp[d.x][d.y] <= '9' || mp[d.x][d.y] == '.')
                {
                    vis[d.x][d.y] = 1;
                    d.s = b.s + 1;
                    q.push(d);
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        int i,j,t;

        for(i = 0; i < n; i++)
        {
            scanf("%s",mp[i]);
            for(j = 0; j < m; j++)
                if(mp[i][j] == 'S')
                {
                    sx[0] = i;
                    sy[0] = j;
                    mp[i][j] = '.';
                }
        }
        for(i = 0; i < n; i++)
            for(j = 0; j < m; j++)
                if(mp[i][j] >= '1' && mp[i][j] <= '9')
                {
                    sx[mp[i][j]-'0'] = i;
                    sy[mp[i][j]-'0'] = j;
                }
        int ans = 0;
        for(i = 1; i <= k; i++)
        {
            memset(vis,0,sizeof(vis));
            ans += bfs(sx[i-1],sy[i-1],sx[i],sy[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}