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>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll = long long;
const ll INF = 1e9;

ll H, W, N;
vector<vector<ll>> F(10);
string M[1001];

ll dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

ll bfs(ll sx, ll sy, ll gx, ll gy) {
    ll d[1001][1001];
    queue<vector<ll>> que;
    for(ll i = 0; i < H; i++) for (ll j = 0; j < W; j++) d[i][j] = INF;

    que.push({sx, sy});
    d[sy][sx] = 0;

    while(que.size()) {
        vector<ll> p = que.front(); que.pop();
        if (p[0] == gx && p[1] == gy) break;

        for (ll i = 0; i < 4; i++) {
            ll nx = p[0] + dx[i], ny = p[1] + dy[i];
            if (M[ny][nx] != 'X' && nx >= 0 && nx < W && ny >= 0 && ny < H && d[ny][nx] == INF) {
                que.push({nx, ny});
                d[ny][nx] = d[p[1]][p[0]] + 1;
            }
        }
    }

    return d[gy][gx];
}

int main() {
    ll ans = 0;
    cin >> H >> W >> N;
    for (ll i = 0; i < H; i++) {
        cin >> M[i];
        for (ll k = 0; k < W; k++) {
            if (M[i][k] == 'S') {
                F[0] = {k, i};
            }
            if (isdigit(M[i][k])) {
                F[M[i][k] - '0'] = {k, i};
            }
        }
    }

    for (ll i = 0; i < N; i++) {
        ans += bfs(F[i][0], F[i][1], F[i+1][0], F[i+1][1]);
    }

    cout << ans << endl;

    return 0;
}

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

int h,w,n;
int en;//energy indicates which cheese to eat next
const int MAX_N=1000;
char map[MAX_N][MAX_N];
struct point{
    int x,y;
    int step;
    point(){
        x=0; y=0; step=0;
    }
    point(int xx, int yy, int ss){
        x=xx;
        y=yy;
        step=ss;
    }
};
struct point p[10];
int dx[4]={-1,0,1,0}, dy[4]={0,1,0,-1};

int ok(int x, int y){
    if(x>=0 && x<h && y>=0 && y<w) return 1;
    return 0;
}

int bfs(int next){
    int nextcheese = next +1; int res=0;
    
    queue<point> q;
    point cur(p[next].x, p[next].y,0);
    q.push(cur);
    while(q.size()){
        cur = q.front(); q.pop(); 
        int x=cur.x, y=cur.y;
        int nx, ny;
        for(int i=0; i<4; i++){
            nx = x+dx[i]; ny=y+dy[i];
            if(ok(nx, ny)){
                if((map[nx][ny]-'0')==nextcheese) return ++cur.step;
                if(map[nx][ny]=='.'){
                    point temp(nx, ny, cur.step+1);
                    q.push(temp);
                }
            }
        }
    }
}

void solve(){
    int sx, sy;
    scanf("%d%d%d", &h, &w, &n);
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            char ch;
            cin>>ch; map[i][j]=ch;
            switch(ch){
                case '.': break;
                case 'X': break;
                case 'S': p[0].x = i; p[0].y = j; break;
                default:
                int t=ch-'0';
                p[t].x = i; p[t].y = j;
            }
        }
    }
    int step=0;
    for(int i=0; i<n; i++){
        step+=bfs(i);
    }
    printf("%d\n", step);
}


int main(){
    solve();
}

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

using namespace std;

#define llong long long 
typedef struct data{
	int h;
	int w;
	int t;
}data;
int H, W, N;
int HP = 1;
int cost = 0;
int area[1000][1000];
bool cannnot[1000][1000];
bool check[1000][1000];
int nh[4] = {1, 0, -1, 0};
int nw[4] = {0, 1, 0, -1};
pair<int, int> start;
queue<data> q;

void solve(pair<int, int> p){
	data fd;
	fd.h = p.first;
	fd.w = p.second;
	fd.t = 0;

	q.push(fd);

	while(HP <= N){
		data sd = q.front(); q.pop();
		int nowh = sd.h;
		int noww = sd.w;
		if(area[nowh][noww] == HP){
			cost += sd.t;
			while(!q.empty()){
				q.pop();
			}
			HP++;
			data now;
			now.h = sd.h;
			now.w = sd.w;
			now.t = 0;
			q.push(now);
			fill_n(*check, 1000 * 1000, false);
		}else{
			for(int i = 0; i < 4; i++){
				data next;
				next.h = nowh + nh[i];
				next.w = noww + nw[i];
				next.t = sd.t + 1;
				if(next.h < 0 || next.w < 0 || next.h >= H || next.w >= W) continue;
				if(cannnot[next.h][next.w] || check[next.h][next.w]) continue;

				check[next.h][next.w] = true;
				q.push(next);
			}
		}
	}

	return;
}

int main(){
	fill_n(*area, 1000 * 1000, 0);
	fill_n(*check, 1000 * 1000, false);
	fill_n(*cannnot, 1000 * 1000, false);
	cin >> H >> W >> N;
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			char in;
			cin >> in;
			if(in == 'S') start = make_pair(i, j);
			if(1 <= (int)in - 48 && (int)in - 48 <= 9) area[i][j] = (int)in - 48;
			if(in == 'X') cannnot[i][j] = true;
		}
	}

	solve(start);
	cout << cost << endl;
	return 0;
}