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 <bits/stdc++.h>

using namespace std;

typedef struct xyt{
	int xx;
	int yy;
	int tt;
}xyt;

int H, W, N, minute = 0;
char area[1001][1001] = {{0}};
bool areab[1001][1001] = {};
bool visit[1001][1001] = {};
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};


void dfs(int x, int y){
	int X, Y, time;
	int HP =1;
	xyt start = {x, y, 0};
	queue <xyt> Q;
	Q.push (start);
	for(int i = 1; i<=1000;i++){
		for(int j = 1; j<= 1000 ;j++){
			visit[j][i]=0;
		}
	}

	while(HP <= N){
		X = Q.front().xx;
		Y = Q.front().yy;
		time = Q.front().tt;
		Q.pop();
		
		//cout << HP << endl;
		for(int i = 0; i <= 4 ;i++){
			int nx = X + dx[i];
			int ny = Y + dy[i];
			//cout << nx <<" " << ny << " " << area[nx][ny] << endl;
			xyt now;
			if(X + dx[i] <= 0 || Y + dy[i] <= 0 || X + dx[i] > W || Y + dy[i] > H)continue;
			if(visit[nx][ny]==1)continue;

			if(areab[X + dx[i]][Y + dy[i]] == 0){
				if(area[X + dx[i]][Y + dy[i]] == HP + '0'){
					minute += time + 1;
					while(!Q.empty()){
						Q.pop();
					}
					now.xx = X + dx[i];
					now.yy = Y + dy[i];
					now.tt = 0;
					Q.push (now);
					HP++;
					for(int k = 1; k<=1000;k++){
						for(int l = 1; l<= 1000 ;l++){
							visit[k][l]=0;
						}
					}
					break;
				}else{
					now.xx = X + dx[i];
					now.yy = Y + dy[i];
					now.tt = time + 1;
					Q.push (now);
					visit[X+dx[i]][Y+dy[i]]=1;
				}
			}
		}
	}
	return;
}


int main(){
	H=W=N=0;
	cin >> H >> W >> N;
	for(int i=0;i<=1000;i++){
		for(int j = 0; j <= 1000 ; j++){
			area[i][j]=0;
			areab[i][j]=0;
			
		}
	}
	for(int i = 1; i <= H ;i++){
		for(int j = 1; j <= W ;j++){
			cin >> area[j][i];
			if(area[j][i] == 'X'){
				areab[j][i] = 1;
			}
		}
	}
	for(int i = 1; i <= H ;i++){
		for(int  j = 1; j <= W ;j++){
			if(area[j][i] == 'S'){
				dfs(j, i);
			}
		}
	}
	cout << minute << endl;
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<queue>
using namespace std ;
struct ST
{
	int x ;
	int y ;
	int cnt = 0 ;
} ;
bool came[ 1000 ][ 1000 ] = {{ 0 }} ;
char map[ 1000 ][ 1000 ] ;
int main()
{
	ST m ,s ;
	queue< ST > que ;
	int n ;
	int w ,h ;
	int kyori = 0 ;
	int mx[ 4 ] = { 0 ,0 ,1 ,-1 } ;
	int my[ 4 ] = { 1 ,-1 ,0 ,0 } ;
	cin >> h >> w >> n ;
	char c = '0' ;
	bool a = true ;	
	for( int i = 0 ; i < h ; i++ )
	{
		cin >> map[ i ] ;
		for( int j = 0 ; j < w ; j++ && a )
		{
			if( map[ i ][ j ] == 'S' )
			{
				m.x = j ;
				m.y = i ;
				a = false ;
			}
		}
	}
	que.push( m ) ;
	while( n )
	{
		n-- ;
		c++ ;
		while( !que.empty() )
		{
			m = que.front() ;
			que.pop() ;
			came[ m.y ][ m.x ] = 1 ;
			if( map[ m.y ][ m.x ] == c )
			{
				kyori += m.cnt ;
				while( ! que.empty() )
				{
					que.pop() ;
				}
				break ;
			}
			for( int i = 0 ; i < 4 ; i++ )
			{
				if( m.x + mx[ i ] > -1 && m.x + mx[ i ] < w && m.y +  my[ i ] > -1 && m.y + my[ i ] < h )
				{
					s = m ;
					s.x += mx[ i ] ;
					s.y += my[ i ] ;
					s.cnt++ ;
					if( map[ s.y ][ s.x ] != 'X' && came[ s.y ][ s.x ] == 0 )
					{
						que.push( s ) ;
					}
				}
			}
		}
		memset( came ,0 ,sizeof( came ) ) ;
		m.cnt = 0 ;
		que.push( m ) ;
	}
	cout << kyori << endl ;
	return 0 ;
}

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

#define INF (1e+9)
#define LLINF (1e+18)
#define LL long long

using namespace std;

typedef struct info{
	int nx;
	int ny;
	int cost;
	int target;
	info(int a,int b,int c,int d){
		nx = a;
		ny = b;
		cost = c;
		target = d;
	}
}info;

bool isNumChar(char c){
	if((0 <= c-48)&&(c-48 <= 9))return true;
	return false;
}

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

int W,H;
int N;

int sx[16] = {0};
int sy[16] = {0};

char field[1024][1024] = {""};
int record[1024][1024][16] = {0};
bool flag[16] = {false};

queue<info> que;


int ans;



int recordReset(){
	for(int i = 0;i < 1024;i++){
		for(int j = 0;j < 1024;j++){
			for(int k = 0;k < 16;k++){
				record[i][j][k] = INF;
			}
		}
	}
}

int bfs(){
	info now(0,0,0,0);
	info next(0,0,0,0);
	for(int i = 0;i < N;i++){
		next = info(sx[i],sy[i],0,i+1);
		que.push(next);
	}

	recordReset();
	while(!que.empty()){
		now = que.front();
		que.pop();
		if(!flag[now.target]){
			//printf("###(%d,%d):::cost<%d>:::target<%d>\n",now.nx,now.ny,now.cost,now.target);
			if(record[now.nx][now.ny][now.target] > now.cost){
				record[now.nx][now.ny][now.target] = now.cost;
				if((sx[now.target] == now.nx)&&(sy[now.target] == now.ny)){
					flag[now.target] = true;
					//printf("###target<%d> completed\n",now.target);
					ans += record[now.nx][now.ny][now.target];
				}
				else{
					switch(field[now.nx][now.ny]){
					case 'X':
						break;
					default:
						for(int i = 0;i < 4;i++){
							next = info(now.nx + dx[i],now.ny + dy[i],now.cost + 1,now.target);
							que.push(next);
						}
						break;
					}
				}
			}
			else{

			}
		}
	}
}


int main (void){
	//while(true){
		scanf("%d%d",&H,&W);
		scanf("%d",&N);
		//if((W == 0)&&(H == 0))break;

		ans = 0;

		//Reset Field
		for(int i = 0;i < 1024;i++){
			for(int j = 0;j < 1024;j++){
				field[j][i] = 'X';
			}
		}

		//Reset Flag
		for(int i = 0;i < 16;i++){
			flag[i] = false;
		}

		//Reading Data
		for(int i = 1;i <= H;i++){
			scanf(" ");
			for(int j = 1;j <= W;j++){
				scanf("%c",&field[j][i]);
				if(isNumChar(field[j][i])){
					sx[field[j][i]-48] = j;
					sy[field[j][i]-48] = i;
					//printf("###<%d>(%d,%d)\n",field[j][i] - 48,sx[field[j][i] - 48],sy[field[j][i] - 48]);

				}
				else if(field[j][i] == 'S'){
					sx[0] = j;
					sy[0] = i;
					//printf("###<S>(%d,%d)\n",j,i);
				}
			}
		}

		//DBG
		/*for(int i = 0;i < N;i++){
			printf("<%d>(%d,%d)\n",i,sx[i],sy[i]);
		}*/

		bfs();

		printf("%d\n",ans);
	//}

	return 0;
}