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 pair<int, int> P;
typedef pair<P, int> PP;

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

int INF = 1 << 10;

vector<string> stage;
vector<vector<int> > visited;

template <typename T> void display(T area, int h, int w){
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cout << area[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}

P getStart(int h, int w, char c){
  bool flag = false;
  P res(INF, INF);
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(stage[i][j] == c){
        res.first = i;
        res.second = j;
        flag = true;
        break;
      }
    }
    if(flag) break;
  }

  return res;
}

void init(int h, int w){
  visited.resize(h);
  for(int i = 0; i < h; i++){
    visited[i].resize(w);
    for(int j = 0; j < w; j++){
      visited[i][j] = INF;
    }
  }
}


int main(){
  int h, w, n;
  cin >> h >> w >> n;
  for(int i = 0; i < h; i++){
    string tmp;
    cin >> tmp;
    stage.push_back(tmp);
  }
  
  P s = getStart(h, w, 'S');

  int ans = 0, power = 0;

  while(1){
    //cout << s.first << " " << s.second << endl;
    if(power == n) break;

    queue<PP > que;
    que.push(PP(s, 1));

    vector<string> tmp(h);
    tmp = stage;

    init(h, w);
    visited[s.first][s.second] = 0;
    
    while(que.size()){
      PP p = que.front(); que.pop();
      P pos = p.first;
      bool goal = false;
      
      //display(tmp, h, w);
      //display(visited, h, w);
      
      for(int i = 0; i < 4; i++){
        int nx = pos.second + dx[i], ny = pos.first + dy[i];
        if(0 <= nx && nx < w && 0 <= ny && ny < h && tmp[ny][nx] != 'X' && visited[ny][nx] == INF){
          if(tmp[ny][nx] == char('0' + power + 1)){
            goal = true;
            s = P(ny, nx);
            break;
          }
          else{
            tmp[ny][nx] = '&';
            visited[ny][nx] = p.second;
            que.push(PP(P(ny, nx), p.second + 1));
          }
        }
      }
      
      if(goal){
        ans += p.second;
        power++;
        break;
      }
    }

    
  }

  cout << ans << endl;
  
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>

using namespace std;

int W, H, N;
typedef pair<int, int> P;
queue<P> que;
char maze[1003][1003];
int timer[1003][1003];
int pos_x, pos_y;
int sh_x[4] = {0, 0, -1, 1};
int sh_y[4] = {1, -1, 0, 0};
int res = 0;
int strength = 1;

void solve() {
  que.push(P(pos_x, pos_y));

  while(que.size()) {
    P pos = que.front(); que.pop();
    if (maze[pos.second][pos.first] - '0' == strength) {
      //printf("[%d] [%d]\n", pos.second, pos.first);
      while(que.size()) que.pop();
      strength++;
      if (strength == N+1) {
        res = timer[pos.second][pos.first];
        break;
      }

      for (int i=0;i<H;i++) {
        for (int j=0;j<W;j++) {
          if (maze[i][j] == '#') maze[i][j] = '.';
        }
      }

      maze[pos.second][pos.first] = '#';
      que.push(pos);
      continue;
    }
    for (int i=0;i<4;i++) {
      int new_x = pos.first + sh_x[i];
      int new_y = pos.second + sh_y[i];
      if (new_x >= 0 && new_x < W && new_y >=0 && new_y < H && maze[new_y][new_x] != '#' && maze[new_y][new_x] != 'X') {
        //printf("Go to [%d] [%d]\n", new_y, new_x);
        que.push(P(new_x, new_y));
        if (maze[new_y][new_x] == '.') {
          maze[new_y][new_x] = '#';
        }
        timer[new_y][new_x] = timer[pos.second][pos.first] + 1;
      }
    }
  }
}

int main() {
    scanf("%d %d %d", &H, &W, &N);
    getchar();
    if (!W && !H) {
      break;
    }
    
    for (int i = 0; i < H; i++)
    {
      fgets(maze[i], 1002, stdin);
      maze[i][W] = '#';
    }

    for (int i=0;i<H;i++) {
      for (int j=0;j<W;j++) {
        if (maze[i][j] == 'S') {
          pos_x = j;
          pos_y = i;
        }
        if (i==H-1) {
          maze[H][j] = '#';
        }
      }
    }

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

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int MAX_HW = 1000;
int n, h, w, rat, s[2], g[2], flag[MAX_HW][MAX_HW][2];
int town[MAX_HW][MAX_HW];
char town2[MAX_HW][MAX_HW];
queue <P> que;
P p;
void bfs(int sy, int sx){
	que.push(P(sy, sx));
	flag[sy][sx][0]=rat;
	while (que.size()){
	RE:
		p = que.front();
		que.pop();
		for (int i = -1; i <= 1; i++){
			for (int j = -1; j <= 1; j++){
				if (i == 0 || j == 0){
					if (town2[p.first + i][p.second + j] != 'X'&&flag[p.first + i][p.second + j][0] != rat&&p.first + i < h&&p.first + i >= 0 && p.second + j < w&&p.second + j >= 0) {
							flag[p.first+i][p.second+j][0] = rat;
							flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
							if (town[p.first + i][p.second + j] == rat){
							if (rat == n) return;
							rat++;
							flag[p.first+i][p.second+j][0] = rat;
							while (que.size()){
								que.pop();
							}
							que.push(P(p.first + i, p.second + j));
							goto RE;
						}
						else{
						que.push(P(p.first + i, p.second + j));
						}
					}
				}
			}
		}
	}
}
int main(){
	cin >> h >> w >> n;
	rat = 1;
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			cin >> town2[i][j];
			town[i][j] = town2[i][j] - 48;
			if (town2[i][j] == 'S'){
				s[0] = i;
				s[1] = j;
			}
			if (town[i][j] == n){
				g[0] = i;
				g[1] = j;
			}
		}
	}
	bfs(s[0], s[1]);
	cout << flag[g[0]][g[1]][1] << endl;
	return 0;
}