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 <stdlib.h>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>

using namespace std;

using Point = pair<int, int>;

const int MAXN = 1000;
const int INF = 0x3f3f3f3f;
const int STEP_NUM = 4;
const int STEP[STEP_NUM][STEP_NUM] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

int h, w, n;
char field[MAXN + 1][MAXN + 1];

int BFS(Point start, Point goal) {
    int start_x = start.first;
    int start_y = start.second;
    int goal_x = goal.first;
    int goal_y = goal.second;
    int dist[MAXN][MAXN];
    for (size_t i = 0; i < h; i++) {
        for (size_t j = 0; j < w; j++) {
            dist[i][j] = INF;
        }
    }
    queue<Point> queue;
    queue.emplace(start_x, start_y);
    dist[start_x][start_y] = 0;

    while (queue.size()) {
        auto point = queue.front();
        queue.pop();
        if (point == make_pair(goal_x, goal_y)) { break; }
        for (size_t i = 0; i < STEP_NUM; i++) {   
            int new_x = point.first + STEP[i][0];
            int new_y = point.second + STEP[i][1];
            if (0 <= new_x && new_x < h && 0 <= new_y && new_y < w
                && field[new_x][new_y] != 'X' && dist[new_x][new_y] == INF) {
                queue.emplace(new_x, new_y);
                dist[new_x][new_y] = dist[point.first][point.second] + 1;
            }
        }
    }

    return dist[goal_x][goal_y];
}

int main() {
    scanf("%d %d %d\n", &h, &w, &n);
    Point start;
    vector<Point> goals(n + 1);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            scanf("%c", &field[i][j]);
            switch (field[i][j]) {
                case 'S':
                    goals[0] = make_pair(i, j);
                    break;
                case '.':
                case 'X':
                    break;
                default:
                    goals[field[i][j] - '0'] = make_pair(i, j);
            }
        }
        getchar();
    }

    int best_time = 0;
    for (int i = 0; i < n; i++) {
        best_time += BFS(goals[i], goals[i + 1]);
    }
    printf("%d\n", best_time);

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

typedef struct{
  short x,y;
  int cost;
  short ch;
}mouse;

short maxch[1000][1000];
char ku[1000][1000];
mouse t;
short cx,cy,nx,ny,ch,nch,h,w,n,cost;

main(){
  scanf("%d%d%d",&h,&w,&n);
  
  rep(i,h)scanf(" %s ",ku[i]);


  rep(i,h)rep(j,w){
    if(ku[i][j]=='S')cx=i,cy=j;
    maxch[i][j]=-1;
  }

  queue<mouse> Q;

  Q.push((mouse){cx,cy,0,0});
  maxch[cx][cy]=0;

  while(!Q.empty()){
    //    cout<<Q.size()<<endl;
    t=Q.front();Q.pop();
    cx=t.x,cy=t.y,cost=t.cost;
    ch=t.ch;

    rep(i,4){
      nx=cx+dx[i],ny=cy+dy[i];
      if(nx<0 || nx>=h || ny<0 || ny>=w)continue;
	 if(ku[nx][ny]=='X')continue;
	 nch=ch;
	 if(ku[nx][ny]<='9' && ku[nx][ny]>='1' && ku[nx][ny]-'0'==t.ch+1){
	   nch=ch+1;
	 }
	 if(nch==n){
	   printf("%d\n",cost+1);
	   return 0;
	 }
	 if(maxch[nx][ny]>=nch)continue;
	 maxch[nx][ny]=nch;
	 Q.push((mouse){nx,ny,cost+1,nch});
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
typedef pair<int, int> p;
int main(){
	queue<p> q;
	queue<p> q1;
	p s;
	p temp;
	char tizu[100][100], mozi[11] = "123456789";
	int W, H, count = 0, bre = 0, tizu1[100][100] = { 0 }, nami = 0, kazu = 0;
	cin >> H >> W;
	for (int i = 0; i < H; i++){
		for (int l = 0; l < W; l++){
			cin >> tizu[i][l];
			if (tizu[i][l] != '.'){
				for (int m = 0; m < 10; m++){
					if (tizu[i][l] == mozi[m]) tizu1[i][l] = m + 1;
				}
				kazu++;
				s = make_pair(i, l);
				q.push(s);
			}
		}
	}
	while (!q.empty()){
		nami++;
		for (int l = 0; l < kazu; l++){
			s = q.front();
			q.pop();
			int dx1[8] = { 1, -1, 0, 0, -1, -1, 1, 1 }, dx2[8] = { 0, 0, 1, -1, -1, 1, -1, 1 };
			for (int i = 0; i < 8; i++){
				if (tizu[s.first + dx1[i]][s.second + dx2[i]] == '.'){
					count++;
				}
			}if (count >= tizu1[s.first][s.second]) {
				q1.push(s);
				bre++;
			}
			else q.push(s);
			count = 0;
		}cout << bre;
		for (int i = 0; i < bre; i++){
			temp = q1.front();
			q1.pop();
			tizu[temp.first][temp.second] = '.';
		}
		for (int i = 0; i < H; i++){
			for (int l = 0; l < W; l++){
				cout << tizu[i][l];
			}cout << endl;
		}
		if (bre == 0) {
			cout << nami-1 << endl;
			return 0;
		}
		kazu = kazu - bre;
		bre = 0;
	}
	return 0;
}