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 <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int h, w, n;
char maze[1001][1001];
int x[10];
int y[10];

char Map[1001][1001];
int d[1001][1001];
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int bfs(int sx,int sy,int gx,int gy) {
	queue<pair<int, int>> que;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			d[i][j] = INF;
		}
	}
	que.push(pair<int, int>(sx, sy));
	d[sy][sx] = 0;
	while (que.size()) {
		pair<int, int> p = que.front();
		que.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&&Map[ny][nx] != 'X'&&d[ny][nx] == INF) {
				que.push(pair<int, int>(nx, ny));
				d[ny][nx] = d[p.second][p.first] + 1;
			}
		}
	}
	return d[gy][gx];
}

void copy() {
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			Map[i][j] = maze[i][j];
		}
	}
}

int main() {
	cin >> h >> w >> n;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cin >> maze[i][j];
			if (maze[i][j] == 'S')x[0] = j, y[0] = i;
			if (maze[i][j] == '1')x[1] = j, y[1] = i;
			if (maze[i][j] == '2')x[2] = j, y[2] = i;
			if (maze[i][j] == '3')x[3] = j, y[3] = i;
			if (maze[i][j] == '4')x[4] = j, y[4] = i;
			if (maze[i][j] == '5')x[5] = j, y[5] = i;
			if (maze[i][j] == '6')x[6] = j, y[6] = i;
			if (maze[i][j] == '7')x[7] = j, y[7] = i;
			if (maze[i][j] == '8')x[8] = j, y[8] = i;
			if (maze[i][j] == '9')x[9] = j, y[9] = i;
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		copy();
		ans += bfs(x[i], y[i], x[i + 1], y[i + 1]);
	}
	cout << ans << endl;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long  cheese[1001][1001];
long long  check[1001][1001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
		}
		else{
			for(i=0;i<4;i++){
				if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&check[y.a+q[i]][y.b+p[i]]!=1&&cheese[y.a][y.b]!=-1){
					x.a=y.a+q[i];
					x.b=y.b+p[i];
					x.c=y.c+1;
					x.d=y.d;
					Q.push(x);
				}
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>

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

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

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

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

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

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

        for (int i = 0; i < 4; i++) {
            int 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 (int i = 0; i < H; i++) {
        cin >> M[i];
        for (int 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 (int 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 <cstdio>
#include <map>
#include <queue>

using namespace std;

int h, w, n;

char field[1010][1010];

using pos = pair<int, int>;

pos pass[10];

int distance(pos start, pos goal) {
    bool visited[1010][1010];
    for (int i = 0; i < 1010; i++) {
        for (int j = 0; j < 1010; j++) {
            visited[i][j] = false;
        }
    }
    queue<pair<int, pos>> q;
    q.push(make_pair(0, start));

    while (!q.empty()) {
        auto s = q.front();
        q.pop();
        if (s.second.first < 0 || s.second.second < 0) { continue; }
        if (field[s.second.first][s.second.second] == 'X') { continue; }
        if (visited[s.second.first][s.second.second]) { continue; }
        if (s.second == goal) {
            return s.first;
        }
        visited[s.second.first][s.second.second] = true;
        q.push(make_pair(s.first + 1, make_pair(s.second.first, s.second.second + 1)));
        q.push(make_pair(s.first + 1, make_pair(s.second.first, s.second.second - 1)));
        q.push(make_pair(s.first + 1, make_pair(s.second.first + 1, s.second.second)));
        q.push(make_pair(s.first + 1, make_pair(s.second.first - 1, s.second.second)));
    }
}

int main() {
    scanf("%d", &h);
    scanf("%d", &w);
    scanf("%d", &n);
    for (int i = 0; i < h; i++) {
        scanf("%s", field[i]);
        for (int j = 0; j < w; j++) {
            if (field[i][j] == 'S') {
                pass[0] = make_pair(i, j);
            } else if (field[i][j] >= '1' && field[i][j] <= '9') {
                pass[field[i][j] - '0'] = make_pair(i, j);
            }
        }
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += distance(pass[i], pass[i + 1]);
    }
    printf("%d\n", sum);
}