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

typedef pair<int, int> pi;
pair <int, int>p;
int n, m, c;
char ma[1001][1001];
int 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();
		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]){
					while (!q.empty()){ q.pop(); }
					q.push(make_pair(x, y));
					count += te[p.first][p.second] + 1;
					nat++;
					if (nat == na){ cout << count << endl; return 0; }
					for (int i2 = 0; i2 <= n; i2++){
						for (int i3 = 0; i3 <= m; i3++){
							te[i2][i3] = op;
						}
					}
					te[x][y] = 0;
					break;
				}
				else{
					q.push(make_pair(x, y));
					te[x][y] = te[p.first][p.second] + 1;
				}
			}
		}
	}
}

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[1000][1000],C[11]={"0123456789"};
int D[1000][1000];
int A[1000][1000],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;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		//printf("a");
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();
		A[S.y][S.x]=1;
		S.x+=x[l];
		S.y+=y[l];
		S.Z++;
		//printf("%d\n",A[S.y][S.x]);
		if(A[S.y][S.x]==1) continue;
		//printf("b");
		if(s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
			S.x-=x[l];
			S.y-=y[l];
		}
		else if(s[S.y][S.x]=='.'){}
		else{
			if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				printf("%d",S.Z);
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			memset(A,0,sizeof(A));
			l=-1;
			}

		} 
		P.push(S);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
typedef pair<int, int> pii;

int H, W, N;
pii P[10];
string T[1000];
int D[1000][1000];

int MinDist(pii S, pii G)
{
    for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
            D[h][w] = -1;
        }
    }

    queue<pii> Q;
    Q.push(S);
    D[S.first][S.second] = 0;
    while (!Q.empty()) {
        pii Tmp = Q.front(); Q.pop();
        int dif[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

        for (int i = 0; i < 4; ++i) {
            int h = Tmp.first + dif[i][0];
            int w = Tmp.second + dif[i][1];
            if (h < 0 || h >= H) continue;
            if (w < 0 || w >= W) continue;
            if (T[h][w] == 'X') continue;
            if (D[h][w] >= 0) continue;
            D[h][w] = D[Tmp.first][Tmp.second] + 1;
            Q.push(make_pair(h, w));
        }
    }

    return D[G.first][G.second];
}

int main()
{
    cin >> H >> W >> N;
    for (int h = 0; h < H; ++h) {
        cin >> T[h];
    }
    for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
            if (T[h][w] == 'S') P[0] = make_pair(h, w);
            if (T[h][w] >= '1' && T[h][w] <= '9') P[T[h][w]-'0'] = make_pair(h, w);
        }
    }
    int Res = 0;
    for (int n = 0; n < N; ++n) {
        Res += MinDist(P[n], P[n+1]);
    }
    cout << Res << endl;

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;

int main()
{
	queue < pair<int, int>> q;
	pair<int, int> p;

	char **m;
	char c[10] = { "123456789" };
	int **f;
	int n, k, i, i1, c1, i2, sum = 0;

	cin >> n;
	cin >> k;
	cin >> c1;

	m = new char *[n + 2];
	f = new int *[n + 2];

	for (i = 0; i < n + 2; i++){
		m[i] = new char[k + 2];
		f[i] = new int[k + 2];
	}

	for (i = 1; i <= n; i++){
		for (i1 = 1; i1 <= k; i1++){
			cin >> m[i][i1];
		}
	}

	for (i = 0; i < n + 2; i++){
		for (i1 = 0; i1 < k + 2; i1++){
			if (i == 0 || i == n + 1){
				m[i][i1] = 'X';
			}
			else if (i1 == 0 || i1 == k + 1){
				m[i][i1] = 'X';
			}
		}
	}

	for (i = 1; i <= n; i++){
		for (i1 = 1; m[i][i1] != '\0'; i1++){
			if (m[i][i1] == 'S'){
				q.push(make_pair(i, i1));
			}
		}
	}



	pair<int, int> temp;

	temp = q.front();

	for (i = 0; i < c1; i++){

		for (i1 = 0; i1 < n + 2; i1++){
			for (i2 = 0; i2 < k + 2; i2++){
				if (i1 == 0 || i1 == n + 1){
					f[i1][i2] = 1;
				}
				else if (i2 == 0 || i2 == k + 1){
					f[i1][i2] = 1;
				}
				else
					f[i1][i2] = -1;
			}
		}

		f[temp.first][temp.second] = 0;

		while (!q.empty()){
			p = q.front();
			q.pop();

			//上への探索
			if (m[p.first - 1][p.second] != 'X' && f[p.first - 1][p.second] == -1){
				q.push(make_pair(p.first - 1, p.second));
				f[p.first - 1][p.second] = f[p.first][p.second] + 1;
			}

			//下への探索
			if (m[p.first + 1][p.second] != 'X' && f[p.first + 1][p.second] == -1){
				q.push(make_pair(p.first + 1, p.second));
				f[p.first + 1][p.second] = f[p.first][p.second] + 1;
			}

			//左への探索
			if (m[p.first][p.second - 1] != 'X' && f[p.first][p.second - 1] == -1){
				q.push(make_pair(p.first, p.second - 1));
				f[p.first][p.second - 1] = f[p.first][p.second] + 1;
			}

			//右への探索
			if (m[p.first][p.second + 1] != 'X' && f[p.first][p.second + 1] == -1){
				q.push(make_pair(p.first, p.second + 1));
				f[p.first][p.second + 1] = f[p.first][p.second] + 1;
			}

			//ゴールかどうか？
			if ( m[p.first][p.second] == c[i]){
				sum += f[p.first][p.second];
				temp = p;
			}

		}
	}
	
	cout << sum << endl;
	
return 0;
}