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 <queue>
#include <vector>
using namespace std;

int h, w, n;
int gy[10], gx[10];
char field[1000][1001];

//????????????
const int dy[4] = {-1, 0, 1, 0};	//?????°
const int dx[4] = {0, 1, 0, -1};	//?????°

//???????????°????±???????
int bfs( int starty, int startx, int goaly, int goalx ){
	//static(??????)???????????¨????¨???¶??????????¨???¶????????¶????????°?????????????????°??¨?????????????????§
	//????????????????????°????????????????????????bfs??¢??°?????§??????????????????????????°???????????\???
	static int minDist[1000][1000];		//minDist[y][x] = y(>=0)???x(>=0)???????????§??????????????°
	static queue<pair<int, int> > que;	//?????°, ??´???
	int i, j;
	
	for( i = 0; i < h; i++ )
		for( j = 0; j < w; j++ )
			minDist[i][j] = 100000000;	//???????????§??????????????\????????????
			
	que.push(pair<int, int>(0, starty * w + startx) );
	while( !que.empty() ){
		int cost = que.front().first;
		int nowy = que.front().second / w;
		int nowx = que.front().second % w;
		que.pop();
		
		if( minDist[nowy][nowx] <= cost )	//??´???????????????????????°????°???????????????????
			continue;
		minDist[nowy][nowx] = cost;			//???????????°?????´??°
		
		for( i = 0; i < 4; i++ ){
			int nexty = nowy + dy[i];
			int nextx = nowx + dx[i];
			if( 0 <= nexty && nexty < h && 0 <= nextx && nextx < w ){	//?????????????£?????????????
				if( field[nexty][nextx] != 'X' ){						//???????£???§???????????°
					que.push( pair<int, int>(cost + 1, nexty * w + nextx) );	//?§??????????
				}
			}
		}
	}
	
	return minDist[goaly][goalx];
}

int main()
{
	int i, j;
	cin >> h >> w >> n;
	for( i = 0; i < h; i++ ){
		cin >> field[i];
		for( j = 0; j < w; j++ ){
			if( field[i][j] == 'S' ){
				gy[0] = i;
				gx[0] = j;
			}
			else if( '1' <= field[i][j] && field[i][j] <= '9' ){
				gy[field[i][j] - '0'] = i;
				gx[field[i][j] - '0'] = j;
			}
		}
	}
	
	int ans = 0;
	for( i = 0; i < n; i++ ){
		//???????????\??´i(i = 0?????????S)?????????????????\??´i + 1?????????????????°????¶????
		ans += bfs(gy[i], gx[i], gy[i+1], gx[i+1]);
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

typedef pair<int, int> P;
typedef struct cheese {
    P p;
    int hd;
    cheese(P p, int hd) : p(p), hd(hd) {}
} cheese;

const int INF = 1e8;
const int MAX_N = 10;
int m, n, total;
char map[MAX_N][MAX_N];
int tm[MAX_N][MAX_N];
vector<cheese> che;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int sx, sy;

int bfs(int sx, int sy, int ex, int ey) {
    queue<P> que;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            tm[i][j] = INF;
    que.push(P(sx, sy));
    tm[sx][sy] = 0;
    
    while (que.size() > 0) {
        P p = que.front();
        que.pop();
        if (p.first == ex && p.second == ey) return tm[ex][ey];
        
        for (int i = 0; i < 4; i++) {
            int nx = p.first+dx[i], ny = p.second+dy[i];
            if (nx >= 0 && nx < m && ny >=0 && ny < n && map[nx][ny] != 'X' && tm[nx][ny] == INF) {
                que.push(P(nx, ny));
                tm[nx][ny] = tm[p.first][p.second]+1;
            }
        }
    }
    return -1;
}

bool cmp(cheese a, cheese b) {
    return a.hd < b.hd;
}

int main() {
    cin >> m >> n >> total;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            cin >> map[i][j];
            if (map[i][j] == 'S') {
                sx = i;
                sy = j;
            }
            if (map[i][j] > '0' && map[i][j] <= '9')
                che.push_back(cheese(P(i, j), map[i][j]-'0'));
        }
    sort(che.begin(), che.end(), cmp);
    int sum = bfs(sx, sy, che[0].p.first, che[0].p.second);
    for (vector<cheese>::iterator it = che.begin(); it < che.end()-1; it++) {
        int sx = it->p.first, sy = it->p.second;
        int ex = (it+1)->p.first, ey = (it+1)->p.second;
        sum += bfs(sx, sy, ex, ey);
    }
    cout << sum << endl;
    return 0;
}

C++: 

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#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();
}