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 <queue>
using namespace std;

char mem[1005][1005];

int main()
{
    int h,w,n,sx,sy;
    typedef pair<int,int> P;
    queue<P> que,que2;
    int dx[4]={-1,0,1,0};
    int dy[4]={0,1,0,-1};
    while(scanf("%d%d%d",&h,&w,&n)!=EOF&&h&&w)
    {
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
            {
                scanf("%c",&mem[i][j]);
                if(mem[i][j]==' '||mem[i][j]=='\n') j--;
                if(mem[i][j]=='S') { sx=i; sy=j; }
            }
        while(que.size()) { que.pop();que2.pop(); }
        que.push(P(sx,sy));
        que2.push(P(0,1));
        bool is_find=false;
        while(!is_find)
        {
            P xy=que.front();
            que.pop();
            int x=xy.first;
            int y=xy.second;
            P date=que2.front();
            que2.pop();
            for(int i=0;i<4;i++)
            {
                if((x+dx[i]>=0&&y+dy[i]>=0)&&(x+dx[i]<h&&y+dy[i]<w))
                {
                    if(mem[x+dx[i]][y+dy[i]]=='X')
                        continue;
                    else
                    {
                        if(date.second==(mem[x+dx[i]][y+dy[i]]-'0'))
                        {
                            if(date.second==n)
                            {
                                is_find=true;
                                printf("%d\n",date.first+1);
                            }
                            else
                            {
                                que.push(P(x+dx[i],y+dy[i]));
                                que2.push(P(date.first+1,date.second+1));
                            }
                            break;
                        }
                        else
                        {
                            que.push(P(x+dx[i],y+dy[i]));
                            que2.push(P(date.first+1,date.second));
                        }
                    }
                }
            }
        }
    }
    return 0;
}

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

using namespace std ;

int mx[ 4 ] = { 0 ,0 ,1 ,-1 } ;
int my[ 4 ] = { 1, -1 ,0 ,0 } ;
char map[ 1000 ][ 1000 ] ;
char c = '1' ;

struct M
{
	int x ;
	int y ;
	int cnt ;
} ;

M s ,v ;

int run( int h ,int w )
{
	int came[ 1000 ][ 1000 ] = { 0 } ;
	queue< M > que ;
	s.cnt = 0 ;
	que.push( s ) ;
	while( 1 )
	{
		s = que.front() ;
		que.pop() ;
		if( came[ s.x ][ s. y ] == 1 )
		{
			continue ;
		}
		came[ s. y ][ s.x ] = 1 ;
		if( map[ s.y ][ s.x ] == c )
		{
			while( ! que.empty() )
			{
				que.pop() ;
			}
			c++ ;
			return s.cnt ;
		}
		for( int i = 0 ; i < 4 ; i++ )
		{
			if( s.x + mx[ i ] > -1 && s.x + mx[ i ] < w && s.y +  my[ i ] > -1 && s.y + my[ i ] < h )
			{
				v = s ;
				v.x += mx[ i ] ;
				v.y += my[ i ] ;
				v.cnt++ ;
				if( map[ v.y ][ v.x ] != 'X' && came[ v.y ][ v.x ] == 0 )
				{
					que.push( v ) ;
				}
			}
		}
	}
}

int main()
{
	s.x = -1 ;
	int h ,w ,n ;
	cin >> h >> w >> n ;
	for( int i = 0 ; i < h ; i++ )
	{
		cin >> map[ i ] ;
		for( int j = 0 ; j < w && s.x < 0 ; j++ )
		{
			if( map[ i ][ j ] == 'S' )
			{
				s.x = j ;
				s.y = i ;
				break ;
			}
		}
	}
	int ans = 0 ;
	while( n )
	{
		n-- ;
		ans += run( h ,w ) ;
	}
	cout << ans << endl ;
	return 0 ;
}

C++: #include <iostream>
#include <queue>
#include <climits>
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;

typedef pair<int,int> pii;

int main()
{
  int H, W, N;
  const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  cin >> H >> W >> N;
  char map[H][W];
  int d[H][W], hp = 1, ans = 0;
  fill(d[0],d[H],INT_MAX);
  queue<pii> q;
  REP(i,H){
    REP(j,W){
      cin >> map[i][j];
      if(map[i][j] == 'S'){
        q.push({i,j});
        d[i][j] = 0;
      }
    }
  }
  while(1){
    pii p = q.front();
    q.pop();
    if(hp == map[p.first][p.second]-'0'){
      ans += d[p.first][p.second];
      hp++;
      fill(d[0],d[H],INT_MAX);
      d[p.first][p.second] = 0;
      while(!q.empty()) q.pop();
    }
    if(hp == N+1){
      cout << ans << endl;
      return 0;
    }
    REP(i,4){
      pii next_p = {p.first+dx[i],p.second+dy[i]};
      if(next_p.first < 0 || next_p.first >= H || next_p.second < 0 || next_p.second >= W || map[next_p.first][next_p.second] == 'X') continue;
      if(d[next_p.first][next_p.second] > d[p.first][p.second]+1){
        d[next_p.first][next_p.second] = d[p.first][p.second]+1;
        q.push(next_p);
      }
    }
  }
}