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 <vector>
#include <queue>

using namespace std;

typedef struct {
	int x;
	int y;
}location;

int h, w, n;
location cheese[10] = {};
vector<vector<char> > map(1000, vector<char>(1000));
int dirx[] = { -1,0,0,1 }, diry[] = { 0,-1,1,0 };
int mint = 0;

void bfs(location now, location goal) {
	queue<location> que;
	que.push(now);
	vector<vector<int> > dist(h, vector<int>(w, -1));
	dist[now.x][now.y] = 0;
	while (!que.empty()) {
		now = que.front(); que.pop();
		if (now.x == goal.x && now.y == goal.y) {
			mint += dist[now.x][now.y];
			break;
		}
		for (int i = 0; i < 4; ++i) {
			location cnext; cnext.x = now.x + dirx[i], cnext.y = now.y + diry[i];
			if (cnext.x >= 0 && cnext.y >= 0 && cnext.x < h && cnext.y < w && map[cnext.x][cnext.y] != 'X' && dist[cnext.x][cnext.y] == -1) {
						dist[cnext.x][cnext.y] = dist[now.x][now.y] + 1;
						que.push(cnext);
			}
		}
	}
}

int main() {
	cin >> h >> w >> n;

	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			cin >> map[i][j];
			if (map[i][j] == 'S') {
				cheese[0].x = i, cheese[0].y = j;
			}
			if ('1' <= map[i][j] && map[i][j] <= '9') {
				cheese[map[i][j]-'0'].x = i;
				cheese[map[i][j]-'0'].y = j;
			}
		}
	}

	for (int cp = 0; cp < n; ++cp)
		bfs(cheese[cp], cheese[cp + 1]);

	cout << mint << endl;

	return 0;
}

C++: #include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cstdio>

#define INF 1 << 29

using namespace std;

int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};
char f[1003][1003];
int visited[1003][1003][10];

class S{
public:
    int x;
    int y;
    int lvl;
    int step;

    S(int x, int y, int lvl, int step):x(x),y(y),lvl(lvl),step(step){}
};

int main(){
    int h,w,lvl,sx,sy,gx,gy;
    cin >> h >> w >> lvl;
    fill((char *)f, (char *)f+1003*1003, 'X');
    fill((int *)visited, (int *)visited+1003*1003*10, INF);

    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin >> f[i][j];
            if(f[i][j] == 'S'){
                sx = j;
                sy = i;
            }else if(f[i][j] == lvl + '0'){
                gx = j;
                gy = i;
            }
        }
    }
    deque<S> q;
    q.push_back(S(sx,sy,1,0));
    visited[sy][sx][1] = 0;
    int px,py,tmplvl,tmpstep;
    while(!q.empty()){
        px = q.front().x;
        py = q.front().y;
        tmplvl = q.front().lvl;
        tmpstep = q.front().step;
        q.pop_front();

        //printf("current:(y = %d, x = %d, lvl = %d, step = %d)\n", py,px,tmplvl,tmpstep);

        if(py == gy && px == gx && tmplvl == lvl+1){
            break;
        }
        visited[py][px][tmplvl] = tmpstep;
        for(int i=0;i<4;i++){
            int nx = px + dx[i];
            int ny = py + dy[i];

            if(f[ny][nx] != 'X'){
                if(f[ny][nx] == tmplvl +'0'){
                    q.push_back(S(nx,ny,tmplvl+1,tmpstep+1));
                }else{
                    if(tmpstep + 1 < visited[ny][nx][tmplvl]){
                        q.push_back(S(nx,ny,tmplvl,tmpstep+1));
                        visited[ny][nx][tmplvl] = tmpstep+1;
                    }
                }
            }
        }
    }
    cout << tmpstep << endl;
}

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

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

int H, W;
char N;
const int MAX_H = 1010, MAX_W = 1010;
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 (muscle-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();
    cout << mytime << endl;
}

int main () {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    // IO
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> plant[i][j];
            if(plant[i][j] == 'S') {sx = i; sy = j;}
        }
    }
    solve();
}


//错误版本突然又正确了，这是什么情况啊？？？？？？？！！！！！！！！

C++: #include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;

#define MP make_pair

const int INF = (1<<30);

char mp[1000][1000];

int h, w, n;

int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
P coords[10];

P s, g;
int memo[10][1000][1000];

int f(char c)
{
	if (c == 'S') return 0;
	if (c == '1') return 1;
	if (c == '2') return 2;
	if (c == '3') return 3;
	if (c == '4') return 4;
	if (c == '5') return 5;
	if (c == '6') return 6;
	if (c == '7') return 7;
	if (c == '8') return 8;
	if (c == '9') return 9;
}

int bfs(int goal) // arihon ha saikou
{
	queue<P> que;
	que.push(s);

	memo[goal][s.first][s.second] = 0;

	while (que.size()) {
		P p = que.front(); que.pop();
		if (p.first == g.first && p.second == g.second) break;

		for (int i = 0; i < 4; ++i) {
			int ny = p.first + dy[i], nx = p.second + dx[i];
			if (0 <= ny && ny < h && 0 <= nx && nx < w && mp[ny][nx] != 'X' && memo[goal][ny][nx] == INF) {
				que.push(MP(ny, nx));
				memo[goal][ny][nx] = memo[goal][p.first][p.second] + 1;
			}
		}
	}

	return memo[goal][g.first][g.second];
}

int main()
{
	cin >> h >> w >> n;

	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			cin >> mp[i][j];
			if (('1' <= mp[i][j] && mp[i][j] <= '9') || mp[i][j] == 'S') {
				int idx = f(mp[i][j]);
				coords[idx] = MP(i, j);
			}
		}
	}

	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < h; ++j) {
			for (int k = 0; k < w; ++k) {
				memo[i][j][k] = INF;
			}
		}
	}

	int ans = 0;

	for (int i = 1; i <= n; ++i) {
		s = coords[i - 1], g = coords[i];
		ans += bfs(i);
	}

	printf("%d\n", ans);
}