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<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#define int long long
#define INF 1000000
using namespace std;
struct data{
    int x,y;
};
char fld[1001][1001];
int H,W,N,cou;
int ans[1001][1001];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
signed main(){
    cin>>H>>W>>N;
    data s;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin>>fld[i][j];
            if(fld[i][j]=='S'){
                s.x=i,s.y=j;
            }
        }
    }
    for(int l=1;l<=N;l++){
        queue<data> q;
        data g;
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                ans[i][j]=INF;
                if(fld[i][j]=='0'+l)g.x=i,g.y=j;
            }
        }
        q.push(s);
        ans[s.x][s.y]=0;
        while(!q.empty()){
            data now;
            now=q.front();
            q.pop();
            if(now.x==g.x&&now.y==g.y)break;
            for(int i=0;i<4;i++){
                if(now.x+dx[i]>=0&&now.x+dx[i]<H&&now.y+dy[i]>=0&&now.y+dy[i]<W&&ans[now.x+dx[i]][now.y+dy[i]]==INF&&fld[now.x+dx[i]][now.y+dy[i]]!='X'){
                    data next;
                    next.x=now.x+dx[i],next.y=now.y+dy[i];
                    q.push(next);
                    ans[now.x+dx[i]][now.y+dy[i]]=ans[now.x][now.y]+1;
                }
            }
        }
        cou+=ans[g.x][g.y];
        s=g;
    }
    cout<<cou<<endl;
    return 0;
}

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<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;

typedef pair<int, int> pi;
pair <int, int>p;
pair <int, int> po;
int n, m,c;
char ma[1001][1001];
int ix, iy,x,y;
int te[1001][1001];
int xw[4] = { 1, 0, -1, 0 };
int yw[4] = { 0, 1, 0, -1 };
char name[10] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int nat = 0;
int na;
int op = 10000;
int main()
{	queue<pi> q;
	cin >> n >> m >> na;
	for (int i = 0; i < n; i++){
		for (int i2 = 0; i2 < m; i2++){
			cin >> ma[i][i2];
			te[i][i2] = op;
			if (ma[i][i2] == 'S'){
				p.first = i;
				p.second = i2;
			}
		}
	}
	
	int count;

	count = 0;
	q.push(p);
	
	te[p.first][p.second] = 0;
	while (!q.empty()){
		p = q.front();
		po = p;
		q.pop();
		
		for (int i = 0; i < 4; i++){
			x = p.first + xw[i];
			y = p.second + yw[i];
			if (0<=x && x<=n&&0<=y && y<=m&&ma[x][y] != 'X'&&te[x][y] == op){

				if (ma[x][y] == name[nat]){
					po.first = x;
					po.second = y;
					while (!q.empty()){ q.pop(); }
					q.push(po);
					po = p;
					count += te[p.first][p.second] + 1;
					nat++; 
					if (nat == na){ cout << count << endl; return 0; }
					//memset(te, op, sizeof(te));
					for (int i2 = 0; i2 <= n; i2++){
						for (int i3 = 0; i3 <= m; i3++){
							te[i2][i3] = op;
						}
					}
					te[x][y] = 0;
					break;
				}

				else{
					po.first = x;
					po.second = y;
					q.push(po);
					po = p;
					te[x][y] = te[p.first][p.second] + 1;
				}
			}
		}
	}
}

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

using namespace std;

struct P{
  int s;
  int t;
  int c;
};

int ds[4] = {0, 1, 0, -1}, dt[4] = {1, 0, -1, 0};

int bfs(char ch[1001][1001], P st, P go, int w, int h)
{
  bool fa[1001][1001];
  fill(&fa[0][0], &fa[1000][1000],  true);
  P p, q;
  queue<P> que;
  que.push(st);
  int c = 0;
  while(!que.empty()){
    p = que.front();
    que.pop();

    if(p.s == go.s && p.t == go.t){
      c = p.c;
      break;
    }

    for(int i = 0; i < 4; i++){
      q.s = p.s + ds[i];
      q.t = p.t + dt[i];
      if(q.s >= 0 && q.s < h && q.t >= 0 && q.t < w){
	if(fa[q.s][q.t] && ch[q.s][q.t] != 'X'){
	  fa[q.s][q.t] = false;
	  q.c = p.c + 1;
	  que.push(q);
	}
      }
    }
  }

  return c;
}

int main()
{
  int w, h, n, c;
  char ch[1001][1001];
  P pa[10];

  while(cin>>h>>w>>n && h && w && n){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin >> ch[i][j];
	if(ch[i][j] == 'S'){
	  pa[0].s = i;
	  pa[0].t = j;
	  pa[0].c = 0;
	} else if(ch[i][j] != '.' && ch[i][j] != 'X'){
	  pa[(int)(ch[i][j]-'0')].s = i;
	  pa[(int)(ch[i][j]-'0')].t = j;
	  pa[(int)(ch[i][j]-'0')].c = 0;
	}
      }
    }
    c = 0;

    for(int i = 0; i < n; i++){
      c += bfs(ch, pa[i], pa[i+1], w, h);
    }

    cout << c << endl;
  }

  return 0;
}

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;
}