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 <cassert>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
using namespace std;

const int MAX_H = 1001;
const int MAX_W = 1001;

int H, W, N;
string F[MAX_H];
void Input() {
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) cin >> F[i];
}

const int dy[] = {-1, 0, 1, 0},
          dx[] = {0, 1, 0, -1};

struct S {
    int y, x, c;
    S(int y = 0, int x = 0, int c = 0) : y(y), x(x), c(c) {}
};
int bfs(int sy, int sx, int gy, int gx) {
    queue<S> Q;
    static bool U[MAX_H][MAX_W];
    memset(U, 0, sizeof(U));
    Q.push(S(sy, sx, 0));
    U[sy][sx] = true;
    while (!Q.empty()) {
        S c = Q.front(); Q.pop();
        if (c.y == gy && c.x == gx) return c.c;
        for (int i = 0; i < 4; i++) {
            int ny = c.y + dy[i],
                nx = c.x + dx[i];
            if (ny < 0 || ny >= H) continue;
            if (nx < 0 || nx >= W) continue;
            if (F[ny][nx] == 'X') continue;
            if (U[ny][nx]) continue;
            Q.push(S(ny, nx, c.c + 1));
            U[ny][nx] = true;
        }
    }
    assert(0);
}

pair<int, int> Find(char c) {
    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            if (c == F[y][x]) return make_pair(y, x);
        }
    }
    assert(0);
}

void Solve() {
    string s = "S";
    for (int i = 0; i < N; i++) {
        s.push_back(i + 1 + '0');
    }
    int Ans = 0;
    for (int i = 0; i < N; i++) {
        pair<int, int> from = Find(s[i]),
                         to = Find(s[i + 1]);
        Ans += bfs(from.first, from.second, to.first, to.second);
    }
    cout << Ans << endl;
}

int main() {
    Input(); Solve();
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;

struct OZAWA
{
	int x;
	int y;
	int c;
};
int Flag[1001][1001];
char MAP[1001][1001];
queue<OZAWA> Q;
OZAWA ba,v;
int main(void)
{
	int H,W,N,Nc=0,X[10],Y[10];
	int mv[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
	char Cheese[10] ={'1','2','3','4','5','6','7','8','9'};
	memset(MAP,'X',sizeof(MAP));
	memset(Flag,0,sizeof(Flag));
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			//printf("%d %d ",i,j);
			cin >> MAP[i][j];
			//printf("Q");
			if(MAP[i][j] == 'S'){
				ba.x = j;
				ba.y = i;
				ba.c = 0;
				Flag[i][j] = 1;
				Q.push(ba);
				//printf("Sin\n");
			}
			for(int ii=0;ii<9;ii++){
				if(MAP[i][j] == Cheese[ii]){

					X[ii] = j;
					Y[ii] = i;
					//printf("\n@@@\n");
				}
			}
		}
	}
	//printf("S %d %d\n",ba.x,ba.y);
	//for(int ii=0;ii<2;ii++)
	//printf("%d %d\n",X[0],Y[0]);
	while(!Q.empty()){
		//printf("QA\n");
		ba = Q.front();
		Q.pop();
		if(ba.x==X[Nc] && ba.y==Y[Nc]){
			//printf("OZA");
			Nc++;
			memset(Flag,0,sizeof(Flag));
			while(!Q.empty()){
				Q.pop();
			}
			if(Nc == N) break;
			Q.push(ba);
			Flag[ba.y][ba.x] = 1;
		}
		//printf("aaa\n");
		//if(Nc == N) break;
		for(int i=0;i<4;i++){
			if(MAP[ba.y+mv[i][1]][ba.x+mv[i][0]]!='X' && Flag[ba.y+mv[i][1]][ba.x+mv[i][0]]==0 && (ba.x+mv[i][0])>=0&&(ba.x+mv[i][0])<W&&(ba.y+mv[i][1])>=0&&(ba.y+mv[i][1])<H){
				v.x=ba.x+mv[i][0];
				v.y=ba.y+mv[i][1];
				v.c=ba.c+1;
				Q.push(v);
				Flag[v.y][v.x] = 1;
				//printf("%d %d\n",v.x,v.y);
			}
		}
	}
	cout <<ba.c<<endl;
	return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1002][1002],C[11]={"0123456789"};
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);
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
				A[S.y][S.x]=0;
		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 <cstring>
#include <queue>
#include <utility>

#define fir first
#define sec second

using namespace std;

typedef pair<int,int> mp;
typedef pair<int,mp> mmp;


typedef struct{
	int x,y;
}pos;

int h,w,n;
char map[1005][1005]={};
int gone[1005][1005];

pos dir[4]={ {0,1},{1,0},{0,-1},{-1,0} };

int main(void){
	scanf("%d%d%d", &h, &w, &n);
	pos ms;
	char read[1005];
	for(int y=0;y<h;y++){
		scanf("%s",read);
		for(int x=0;x<w;x++){

			map[x][y]=read[x];

			if(map[x][y]=='S'){
				ms.x=x; ms.y=y;
			}
		}
	}

	int r=0;
	for(int k=1;k<=n;k++){

		memset(gone,0,sizeof(gone));
		queue<mmp> que;
		
		que.push(make_pair( 0,make_pair( ms.x,ms.y )));

		while(true){
			mmp ma=que.front();
			que.pop();

			if( map[ma.sec.fir][ma.sec.sec] == ('0'+k) ){
				ms.x=ma.sec.fir;
				ms.y=ma.sec.sec;
				r+=ma.fir;
				break;
			}
			for(int i=0;i<4;i++){
				int nx = ma.sec.fir + dir[i].x;
				int ny = ma.sec.sec + dir[i].y;

				if(!(nx<0 || nx>=w || ny<0 || ny>=h || map[nx][ny]=='X' || gone[nx][ny]==1)){
					que.push( make_pair(ma.fir+1, make_pair(nx, ny)) );
					gone[nx][ny]=1;
				}
			}
		}
	}
	printf("%d\n",r);
	return 0;
}