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 <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <functional>


using namespace std;

#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()

#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vii;


const int INF = 100000000;
const double EPS = 1e-10;
int mod=1000000007;

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int h, w, n;
vector<string> s;

bool outOfRange(int y, int x){
	if(x < 0 || x >= w || y < 0 || y >= h) return true;
	return false;	
}

int calc(int y, int x, int c, int num){
	//cout<<y<<x<<" "<<c<<" "<<num<<endl;
	if(num == n) return c;
	queue<pair<int, pii> > q;
	vector<vi> v(h, vi(w, -1));
	v[y][x] = 0;
	pair<int, pii> p(c, mp(y, x));
	q.push(p);
	while(!q.empty()){
		//cout<<"  "<<q.size()<<endl;
		p = q.front(); q.pop();
		int tx = p.se.se, ty = p.se.fi, tc = p.fi;
		for(int i = 0; i < 4; i++){
			int nx = tx + dx[i], ny = ty + dy[i];
			if(outOfRange(ny, nx) || s[ny][nx] == 'X' || v[ny][nx] != -1) continue;
			//cout<<ny<<" "<<nx<<endl;
			if(s[ny][nx] == '1' + num){
				//q = queue<pair<int, pii> >;
				//cout<<"AAA"<<endl;
				return calc(ny, nx, tc + 1, num + 1);
			}
			//cout<<" "<<nx<<" "<<ny<<endl;
			v[ny][nx] = tc + 1;
			q.push(mp(tc + 1, mp(ny, nx)));
		}
	}
	return -1;
}

int main(){
	cin>>h>>w>>n;
	//cout<<h<<" "<<w<<" "<<n<<endl;
	s = vector<string>(h);
	rep(i, h) cin>>s[i];
	//rep(i, h) cout<<s[i]<<endl;
	//cout<<s[2][2]<<endl;
	int sx, sy;
	rep(i, h) rep(j, w){
		if(s[i][j] == 'S'){
			sy = i; sx = j;
		}
	}
	//cout<<sy<<" "<<sx<<endl;
	cout<<calc(sy, sx, 0, 0)<<endl;
}

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

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

int H, W, N, SY, SX;
char board[MAX_W][MAX_W + 1];
int dist[MAX_W][MAX_W + 1];


void init() {
	scanf("%d%d%d ", &H, &W, &N);
	for (int i = 0; i < H; ++i) {
		scanf(" %s ", board[i]);
	}
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			if (board[i][j] == 'S') {
				SY = i;
				SX = j;
			}
		}
	}
}

int solve() {
	int ans = 0;
	for (int i = 0; i < N; ++i) {
		char tar = '1' + i;
		memset(dist, -1, sizeof(dist));
		queue<pair<int,int> > que;
		que.push(make_pair(SY, SX));
		dist[SY][SX] = 0;
		for (;!que.empty();) {
			int y = que.front().first, x = que.front().second;
			if (board[y][x] == tar) {
				ans += dist[y][x];
				SY = y; SX = x;
				break;
			}
			que.pop();
			int nd = dist[y][x] + 1;
			for (int k = 0; k < 4; ++k) {
				const int ny = y + dy[k], nx = x + dx[k];
				if ( 0 <= ny && ny < H && 0 <= nx && nx < W && board[ny][nx] != 'X' && dist[ny][nx] == -1) {
					dist[ny][nx] = nd;
					que.push( make_pair(ny, nx) );
				}
			}
		}
	}
	return ans;
}

int main() {
	init();
	printf("%d\n", solve());

	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+1][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;
}