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 <vector>
#include <iostream>
#include <set>
#include <cstdio>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <cmath>
#include <complex>
#include <algorithm>
#include <tuple>
#include <algorithm>
#include <limits>
#include <map>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

P tiz[100];
char g[1100][1100];
int w, h, k;
int d[4][2] = {
    {1, 0},
    {-1, 0},
    {0, 1},
    {0, -1}
};
int used[1100][1100];
int solve(P s, P e) {
    fill_n(used[0], 1100*1100, false);
    queue<T> q;
    q.push(T(s.first, s.second, 0));
    while (!q.empty()) {
        T p = q.front(); q.pop();
        int x, y, t;
        tie(x, y, t) = p;
        if (P(x, y) == e) return t;
        if (x < 0 || w <= x || y < 0 || h <= y) continue;
        if (g[x][y] == 'X') continue;
        if (used[x][y]) continue;
        used[x][y] = true;
        for (int i = 0; i < 4; i++) {
            q.push(T(x+d[i][0], y+d[i][1], t+1));
        }
    }
    return -1;
}

int main() {
    cin >> w >> h >> k;
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            char c;
            cin >> c;
            g[i][j] = c;
            if (c == 'S') {
                tiz[0] = P(i, j);
            }
            if (isdigit(c)) {
                tiz[c-'0'] = P(i, j);
            }
        }
    }
    int r = 0;
    for (int i = 0; i < k; i++) {
        r += solve(tiz[i], tiz[i+1]);
    }
    cout << r << endl;
    return 0;
}

C++: #include <cstdio>
#include <queue>
using namespace std;

int H, M, N;
int go[4][2];
bool K[1002][1002];
bool flg[1002][1002];
int score[1002][1002];
int C[11][2];
int ans = 0;

struct Tops{
  int tx, ty, co;
  
  Tops(int x, int y, int c){
    tx = x;
    ty = y;
    co = c;
  }
};

queue<Tops> que;

int cal(int x, int y){
  while(!que.empty()){
    Tops t = que.front();
    que.pop();
    //printf("(%d, %d)", t.tx, t.ty);
    flg[t.tx][t.ty] = false;
    if(t.tx == x && t.ty == y){
      return t.co;
    }
    for(int i = 0; i < 4; i++){
      //printf("fee");
      if(flg[t.tx + go[i][0]][t.ty + go[i][1]]){
        //printf("push");
        que.push(Tops(t.tx + go[i][0], t.ty + go[i][1], t.co + 1));
      }
    }
  }
  return 2147483647;
}

int main(){
  
  go[0][0] = 1;
  go[0][1] = 0;
  go[1][0] = 0;
  go[1][1] = -1;
  go[2][0] = -1;
  go[2][1] = 0;
  go[3][0] = 0;
  go[3][1] = 1;
  
  for(int i = 0; i <= H + 1; i++){
    for(int j = 0; j <= M + 1; j++){
      score[i][j] = 2147483640;
      flg[i][j] = false;
      K[i][j] = false;
    }
  }
  
  scanf("%d%d%d", &H, &M, &N);
  
  for(int i = 0; i < H; i++){
    char str[M + 1];
    scanf("%s", str);
    for(int j = 0; j < M; j++){
      if(str[j] == '.'){
        K[i + 1][j + 1] = true;
      }else if(str[j] == 'X'){
        K[i + 1][j + 1] = false;
      }else if(str[j] == 'S'){
        C[0][0] = i + 1;
        C[0][1] = j + 1;
        K[i + 1][j + 1] = true;
      }else{
        C[str[j] - '0'][0] = i + 1;
        C[str[j] - '0'][1] = j + 1;
        K[i + 1][j + 1] = true;
      }
    }
  }
  
  for(int i = 0; i < N; i++){
    for(int j = 0; j <= H + 1; j++){
      for(int k = 0; k <= M + 1; k++){
        flg[j][k] = K[j][k];
      }
    }
    while(!que.empty()){
      que.pop();
    }
    que.push(Tops(C[i + 1][0], C[i + 1][1], 0));
    int aa = cal(C[i][0], C[i][1]);
    //printf("%d\n", aa);
    ans += aa;
  }
  
  printf("%d\n", ans);
  
  return 0;
}

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

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() {
  while (1)
  {
    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);
    memset(timer, 0, sizeof(timer[0][0]) * H * W);
    strength = 1;
  }
  return 0;
}