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 <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <array>
#include <stack>
#include <queue>
#include <chrono>
#include <numeric>
 
#pragma warning(disable:4996)
#define REP(i,n) for(int i = 0 ; i < n ; ++i)
typedef long long Int;
 
using namespace std;
 
struct point{
    Int j, i,step;
    point(Int i_, Int j_,Int s) :i(i_), j(j_),step(s){}
};
 
int dx[4] = { 0, -1, 0, 1 };
int dy[4] = { 1, 0, -1, 0 };
 
queue<point> que;
char tile[1000][1000];
int H, W;
bool ed[1000][1000];
 
point solve(char target)
{
    while (!que.empty()){
        point now(que.front());
        //ed[now.i][now.j] = true;
        que.pop();
        REP(i, 4){
            int x = now.j + dx[i];
            int y = now.i + dy[i];
 
            if (x < 0 || y < 0 || x > W || y > H || ed[y][x])
                continue;
 
            if (tile[y][x] == target){
                return point(y, x,now.step+1);
            }
 
            if (tile[y][x] != 'X'){
                que.push(point(y, x, now.step + 1));
                ed[y][x] = true;
            }
        }
    }
}
 
int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    //fstream fs("input.txt");
    ////////////ここから開始///////////
    int n(0);
    cin >> H >> W >> n;
 
    REP(i, 1000)REP(j, 1000)tile[i][j] = 'X';
 
    REP(i, H)REP(j, W)cin >> tile[i][j];
 
    REP(i, H)REP(j, W){ if (tile[i][j] == 'S')que.emplace(i, j, 0); }
    Int ans(0);
    for(int i = 0 ; i < n ; ++i){
        REP(i_1, H+1)REP(j_1, W+1){ ed[i_1][j_1] = false; }
        point t = solve('1' + i);
        que = {};
        que.emplace(t);
        ans = t.step;
    }
     
    cout << ans << 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);
	//memset(A,0,sizeof(A));
		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;
			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(s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
		continue;
		}
		if(A[S.y][S.x]==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<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include <climits>
#include <map>
#include <set>
const int mod = 1e9 + 7;
const int INF = 1 << 20;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

char field[1000][1000];
int n,m;
int sx,sy;
int gx,gy;
int d[1000][1000];

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

int bfs(int s)
{
    queue<P> que;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            d[i][j] = INF;
        }
    }
    que.push(P(sx,sy));
    d[sx][sy] = 0;

    while(que.size()){
        P p = que.front(); que.pop();
        if(field[p.first][p.second] == '0' + (s+1)){
            sx = p.first;
            sy = p.second;
            return d[p.first][p.second];
        }

        for(int i = 0; i < 4; i++){
            int nx = p.first + dx[i] ,ny = p.second + dy[i];
            if(0 <= nx && nx < n && 0 <= ny && ny < m && field[nx][ny] != 'X' && d[nx][ny] == INF){
                que.push(P(nx,ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }
    return 0;
}

int main()
{
    cin >> n >> m;
    ll ans = 0;
    int num; cin >> num;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> field[i][j];
            if(field[i][j] == 'S'){
                sx = i; sy = j;
            }
        }
    }
    for(int i = 0; i < num; i++){
        ans += bfs(i);
    }
    cout << ans << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef pair<pi, pi> pipi;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int H, W, N;
char g[1024][1024];
int dy[] = {0,-1,0,1}, dx[] = {-1,0,1,0};

int main(){
  scanf("%d%d%d", &H, &W, &N);
  rep(i, H) scanf("%s", g[i]);

  int y, x;
  rep(i, H) rep(j, W) if(g[i][j] == 'S') y = i, x = j;

  queue<pipi>q;
  q.push(MP(MP(y, x), MP(1, 0))); // y, x, hp, time
  map<pi, int>m;
  m[MP(y, x)] = 1;

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

    int hp = p.S.F;
    if(g[p.F.F][p.F.S] - '0' == hp){
      while(!q.empty()) q.pop();
      m.clear();
      hp++;
    }

    if(hp > N){
      printf("%d\n", p.S.S);
      break;
    }

    rep(d, 4){
      int ny = p.F.F+dy[d], nx = p.F.S+dx[d];
      if(ny < 0 || ny >= H || nx < 0 || nx >= W || g[ny][nx] == 'X' || m.find(MP(ny, nx)) != m.end()) continue;
      q.push(MP(MP(ny, nx), MP(hp, p.S.S+1)));
      m[MP(ny, nx)] = 1;
    }
  }
  return 0;
}