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 <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
    os<<'[';
    rep(i,a.size()) os<<(i?" ":"")<<a[i];
    return os<<']';
}

typedef pair<int, pii> State;

int H, W, N;
char m[1010][1010];

int bfs(pii s, int dest, pii &ns) {
	queue<pair<int, pii> > q;
	q.push(mp(0, s));
	bool visited[1010][1010];
	memset(visited, false, sizeof(visited));
	while(!q.empty()) {
		pair<int, pii> p = q.front();
		q.pop();
		int x = p.snd.fst;
		int y = p.snd.snd;
		if (visited[y][x]) continue;
		visited[y][x] = true;
		
		if (m[y][x] == dest + '0') {
			ns = p.snd;
			return p.fst;
		}
		
		static const int dx[] = {1, 0, -1, 0};
		static const int dy[] = {0, 1, 0, -1};
		rep(d, 4) {
			int tx = x + dx[d];
			int ty = y + dy[d];
			if (tx < 0 || tx >= W || ty < 0 || ty >= H) continue;
			if (m[ty][tx] == 'X') continue;
			q.push(mp(p.fst + 1, mp(tx, ty)));
		}
	}
}

int main() {
	cin >> H >> W >> N;
	pii s;
	rep(i, H) rep(j, W) {
		cin >> m[i][j];
		if (m[i][j] == 'S') {
			s.fst = j;
			s.snd = i;
		}
	}
	
	int ans = 0;
	rep(i, N) ans += bfs(s, i + 1, s);
	
	cout << ans << endl;
    
    return 0;
}

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

typedef pair<int, int> P;
int bfs();
char stage[1010][1010];
int d[1010][1010];
int h,w;
int sx,sy,gx,gy;

int main(){
	int n;
	scanf("%d%d%d",&h,&w,&n);

	int x_d[10],y_d[10];

	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			scanf(" %c",&stage[j][i]);
			if(stage[j][i] == 'S'){
				sx = j;
				sy = i;
			}
			if(48 < stage[j][i] && stage[j][i] < 58){
				x_d[stage[j][i]-48] = j;
				y_d[stage[j][i]-48] = i;
			}
		}
	}

	int ans = 0;
	gx = x_d[1];
	gy = y_d[1];
	for(int i = 0; i < n; i++){
		ans += bfs();
		sx = x_d[i+1],sy = y_d[i+1];
		gx = x_d[i+2],gy = y_d[i+2];
	}
	printf("%d\n",ans);
	return 0;
}

int bfs(){
	queue<P> Q;
	int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
	memset(d,-1,sizeof(d));
	Q.push(P(sx,sy));
	d[sx][sy] = 0;

	while(Q.size()){
		P p = Q.front();
		Q.pop();
		if(p.first == gx && p.second == gy){
			break;
		}

		for(int i = 0; i < 4; i++){
			int nx = p.first + dx[i] , ny = p.second + dy[i];

			if(0 <= nx && nx < w && 0 <= ny && ny < h){
				if(stage[nx][ny] != 'X' && d[nx][ny] == -1){
					Q.push(P(nx,ny));
					d[nx][ny] = d[p.first][p.second] + 1;
				}
			}
		}
	}
	return d[gx][gy];
}

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

struct node {
	int i;
	int j;
	char val;
};

int H, W, N;//H(1 <= H <= 1000)、W(1 <= W <=1000)、N(1 <= N <= 9)
char m[1001][1001];
queue<node> q;

bool flag = 0;
int t1 = 0;
char flag_m[1001][1001];
void search(int i, int j, char val)
{
	if (flag_m[i][j]<val) {
		if (m[i][j] == 'X')
		{
			return;
		}
		if (m[i][j] == '.' || m[i][j] != val)
		{
			q.push({ i,j,val });
			//flag_m[i][j] = true;
			t1++;
		}
		if (m[i][j] == val)
		{
			if (val == ('0' + N))
			{
				flag = 1;
			}
			else
			{
				q.push({ i,j,val + 1 });
				//flag_m[i][j] = true;
				m[i][j] = '.';
				t1++;
			}
		}
	}
	return;
}
int main()
{
	int si, sj;
	scanf("%d %d %d",&H,&W,&N);
	for (int i = 1;i <= H; i++)
	{
		getchar();
		for (int j = 1; j <= W; j++)
		{
			scanf("%c",&(m[i][j]));
			if (m[i][j] == 'S')
			{
				si = i;
				sj = j;
			}
		}
	}

	//
	
	q.push({si,sj,'1'});
	memset(flag_m,'0',sizeof(flag_m));
	int t = 1;
	int time=0;
	
	while (!q.empty() &&(!flag))
	{
		time++;
		t1 = 0;
		while (t-- &&(!flag))
		{
			node n1 = q.front(); q.pop();
			si = n1.i; sj = n1.j;
			flag_m[si][sj] = n1.val;
			if (si > 1)
			{
				search(si-1,sj,n1.val);
			}
			if (si < H)
			{
				search(si + 1, sj, n1.val);
			}
			if (sj > 1)
			{
				search(si, sj-1, n1.val);
			}
			if (sj < W)
			{
				search(si, sj+1, n1.val);
			}

		}
		t = t1;
	}
	printf("%d\n",time);
	return 1;
}