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>
using namespace std;
#include<stdio.h>
#include<queue>
int main(){
int h,w,n,i,j,k,time=0;
int m[102][102]={0};
pair<int,int> ht,s;
char c[2001];
queue<pair<int,int> > q,st;
scanf("%d%d%d",&h,&w,&n);
for(i=0;i<=h+1;i++){
	for(j=0;j<=w+1;j++){
		if(i==0||i==h+1||j==0||j==w+1){
		m[i][j]=10;
		}
	}
}
for(i=1;i<=h;i++){
	scanf("%s",&c);
	for(j=0;j<w;j++){
		if(c[j]=='S'){
			s.first=i;
			s.second=j+1;
			q.push(s);
			m[i][j+1]=11;
		}
		else if(c[j]=='X'){
			m[i][j+1]=10;
		}
		else if(c[j]=='1')
			m[i][j+1]=1;
		else if(c[j]=='2')
			m[i][j+1]=2;
		else if(c[j]=='3')
			m[i][j+1]=3;
		else if(c[j]=='4')
			m[i][j+1]=4;
		else if(c[j]=='5')
			m[i][j+1]=5;
		else if(c[j]=='6')
			m[i][j+1]=6;
		else if(c[j]=='7')
			m[i][j+1]=7;
		else if(c[j]=='8')
			m[i][j+1]=8;
		else if(c[j]=='9')
			m[i][j+1]=9;
	}
}
ht.first=1;
ht.second=0;
st.push(ht);
while(!q.empty()){
	s=q.front();
	ht=st.front();
	q.pop();
	st.pop();
	ht.second++;
	if(ht.first==m[s.first][s.second]){
		ht.first++;
	}
	if(m[s.first+1][s.second]!=10){
		s.first++;
		q.push(s);
		s.first--;
		st.push(ht);
	}
	if(m[s.first-1][s.second]!=10){
		s.first--;
		q.push(s);
		s.first++;
		st.push(ht);
	}
	if(m[s.first][s.second+1]!=10){
		s.second++;
		q.push(s);
		s.second--;
		st.push(ht);
	}
	if(m[s.first][s.second-1]!=10){
		s.second--;
		q.push(s);
		s.second++;
		st.push(ht);
	}
	ht.second--;
	if(ht.first==n+1){
			time=ht.second;
			break;
	}

}
printf("%d\n",time);
return 0;
}

C++: #include<bits/stdc++.h>
#include<ctype.h>
#define int long long int
#define rep(a,b) for(int a=0;a<b;a++)
#define INF 1000006
using namespace std;

int tx[] = {1,0,-1,0};
int ty[] = {0,1,0,-1};
int w,h,che,sx,sy;
char bases;
int base[1003][1003],base2[1003][1003],memo[1003][1003];

int chee(int x,int y,int n){
    int ans=INF;
    if(memo[x][y]>0&&(memo[x][y]!=INF||base[x][y]<0))return memo[x][y];
    else if(base[x][y]==n)return 0;
    else if(base[x][y]>=0){
        base[x][y]=-1;
        for(int i=0;i<4;i++){
            if(x+tx[i]>=0&&x+tx[i]<w&&y+ty[i]>=0&&y+ty[i]<h){
                ans = min(ans,chee(x+tx[i],y+ty[i],n)+1);
            }
        }base[x][y]=0;
    }
    memo[x][y]=ans;
    return ans;
}
void sho(int b[1003][1003],int a[1003][1003]){
    rep(i,1003){
        rep(j,1003){
            b[i][j]=a[i][j];
        }
    }
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int zx[20],zy[20];
    cin >> h >> w >> che;
    rep(f,h){
        rep(s,w){
            cin >> bases;
            switch(bases){
                case 'S':sx=s;sy=f;
                case '.':base[s][f]=0;
                break;
                case 'X':base[s][f]=-1;
                break;
                default:{base[s][f]=bases - '0';
                        zx[base[s][f]]=s;
                        zy[base[s][f]]=f;
                        }

            }
        }
    }
    sho(base2,base);
    int ans=0;
    ans+=chee(sx,sy,1);
    for(int i=2;i<=che;i++){
        sho(base,base2);
        fill((int*)memo,(int*)(memo+1003),0);
        ans+=chee(zx[i-1],zy[i-1],i);
    }
    cout << ans << endl;
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() ;
		came[ s. y ][ s.x ] = s.cnt ;
		if( map[ s.y ][ s.x ] == c )
		{
			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<algorithm>
#include<string>
#include<cassert>
#include<map>
using namespace std;
string field[1001];
int H, W, N;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int cache[11][1001][1001];
bool dls(int depth, int y, int x, int point){
  if(depth<0){
    return false;
  }
  if(y < 0 || H <= y || x < 0 || W <= x || field[y][x] == 'X'){
    return false;
  }
  if(point == N){
    return true;
  }
  if(cache[point][y][x] >= depth){
    return false;
  }
  cache[point][y][x] = depth;
  if(field[y][x] == point+1 + '0'){ //
    return dls(depth, y, x, point+1);
  }
  for(int i = 0; i < 4; ++i){
    if(dls(depth-1, y+dy[i], x+dx[i], point)){
      return true;
    }
  }
  return false;
}
int iddfs(int x, int y){
  for(int i = 0; i < N; ++i){
    for(int j = 0; j < H; ++j){
      for(int k = 0; k < W; ++k){
        cache[i][j][k] = -1;
      }
    }
  }
  for(int depth = 0; depth < H * W; ++depth){
    if(dls(depth, x, y, 0)){
      return depth;
    }
  }
  assert(false);
}
int main(){
  cin >> H >> W >> N;
  for(int i = 0; i < H; ++i){
    cin >> field[i];    
  }
  for(int i = 0; i < H; ++i){
    for(int j = 0; j < W; ++j){
      if(field[i][j] == 'S'){
        cout << iddfs(i, j) << endl;
      }
    }
  }
  return 0;
}