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<stdio.h>
#include<stack>
#include<iostream>
using namespace std;
int main(void)
{
	char p[2];
	stack<pair<int,int> > q;
	pair<int,int> g,s,w;
	int x,y,n,m,a=0,f[1002][1002],l=1,l2;
	scanf("%d %d %d",&x,&y,&l2);
	for(n=0;n<=1002;n++){
		for(m=0;m<=1002;m++){
			if(n==0||n>x||m==0||m>y){
				f[n][m]=10;
			}
			else{
				scanf("%c",&p);
				if(p[1]=='S'){
					f[n][m]=0;
					s.first=n;
					s.second=m;
					q.push(s);
				}
				else if(p[1]=='X'){
					f[n][m]=10;
				}
				else if(p[1]=='.'){
					f[n][m]=0;
				}
				else{
					f[n][m]=p[1];
				}
			}
		}
	}
	while(!q.empty()){
		s=q.top();
		q.pop();
		if(f[s.first+1][s.second]<=l){
			w.first=s.first+1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second+1]<=l){
			w.first=s.first;
			w.second=s.second+1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first-1][s.second]<=l){
			w.first=s.first-1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second-1<=l]){
			w.first=s.first;
			w.second=s.second-1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				printf("%d\n",a);
				return 0;
			}
			else{
				q.push(w);
			}
		}
	}

	return 0;
}

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

using namespace std;

int h, w;
char n;
char N[1002][1002];
bool p_flag[1002][1002] = { 0 };
int depth[1002][1002] = { 0 };

int main(){
	char next;
	int x, y, time = 0;
	int sx, sy;
	const int dx[4] = { 1, 0, -1, 0 };
	const int dy[4] = { 0, 1, 0, -1 };

	ifstream fin("input.txt");
	ofstream fout("output.txt");

	cin >> h >> w >> n;

	for (int i = 0; i < 1002; i++){
		for (int j = 0; j < 1002; j++){
			N[j][i] = 'X';
		}
	}
	for (int i = 1; i <= h; i++){
		for (int j = 1; j <= w; j++){
			cin >> N[j][i];
			if (N[j][i] == 'S'){
				sx = j;
				sy = i;
			}
		}
	}

	queue <pair<int, int>> q;
	q.push(pair<int, int>(sx, sy));
	next = '1';
	bool b_flag = 0;
	p_flag[sx][sy] = 1;
	while (true){
		if (b_flag) break;
		x = q.front().first;
		y = q.front().second;
		q.pop();
		cout << x << " " << y << endl;
		for (int d = 0; d < 4; d++){
			if (N[x + dx[d]][y + dy[d]] != 'X'){
				if (depth[x + dx[d]][y + dy[d]] == 0) depth[x + dx[d]][y + dy[d]] = depth[x][y] + 1;
				if (p_flag[x + dx[d]][y + dy[d]] == 0){
					if (N[x + dx[d]][y + dy[d]] == next){
						if (next == n){
							cout << depth[x + dx[d]][y + dy[d]] << endl;
							b_flag = 1;
							break;
						}
						else{
							next = next + 1;
							while (!q.empty()) q.pop();
							for (int i = 0; i < 1002; i++){
								for (int j = 0; j < 1002; j++){
									p_flag[j][i] = false;
								}
							}
							q.push(pair<int, int>(x + dx[d], y + dy[d]));
							p_flag[x + dx[d]][y + dy[d]] = 1;
							int dep = depth[x + dx[d]][y + dy[d]];
							for (int i = 0; i < 1002; i++){
								for (int j = 0; j < 1002; j++){
									depth[j][i] = 0;
								}
							}
							depth[x + dx[d]][y + dy[d]] = dep;
							break;
						}
					}
					else {
						q.push(pair<int, int>(x + dx[d], y + dy[d]));
						p_flag[x + dx[d]][y + dy[d]] = 1;
					}
				}
			}
		}
	}

	for (int i = 1; i <= h; i++){
	for (int j = 1; j <= w; j++){
			cout << depth[j][i] << " ";
		}
		cout << endl;
	};
}

C++: 

#include <iostream>
#include <utility>
#include <queue>
using namespace std;

typedef pair<int, int> Pair;
const int INF = 100000000;

int H, W, N;

const int MAX_H = 1000, MAX_W = 1000;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int d[MAX_H][MAX_W];
int mytime;

void bfs() {
	queue<Pair> que;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			d[i][j] = INF;
		}
	}
	char muscle = '1';
	que.push(Pair(sx, sy));
	d[sx][sy] = 0;
	while (que.size()) {
		Pair p = que.front(); que.pop();
		if (int(muscle) - 48 - 1 == N) break;
		for (int i = 0; i < 4; i++) {
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if (0 <= nx && nx < H && 0 <= ny && ny < W && plant[nx][ny] != 'X' && d[nx][ny] == INF) {
				que.push(Pair(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
			if (muscle == plant[nx][ny]) {
				while (que.size()) { que.pop(); }
				que.push(Pair(nx, ny));
				muscle = muscle + 1;
				mytime += d[nx][ny];
				for (int i = 0; i < H; i++) {
					for (int j = 0; j < W; j++) {
						d[i][j] = INF;
					}
				}
				d[nx][ny] = 0;
				break;
			}
		}
	}
}

void solve() {
	mytime = 0;
	bfs();
	//printf("%d\n", mytime);
	 cout << mytime << endl;

}

int main() {
	 cin >> H >> W >> N;
	//scanf("%d%d%d", &H, &W, &N);
	for (int i = 0; i < H; i++) {
		//scanf("%s", plant[i]);
		for (int j = 0; j < W; j++) {
			   cin >> plant[i][j];
			if (plant[i][j] == 'S') { sx = i; sy = j; }
		}
	}
	solve();
}