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 <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

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

struct OBJ{
public:
	OBJ(char a, bool b):kind(a), mark(b){}
	char kind;
	bool mark;
};

int loop;
void dfs(char target, pair<int, int> &pos, vector<vector<OBJ> > &field, int count){
	if(loop <= 0) return;
	if(field[pos.second][pos.first].kind == target){
		mincount = mincount > count ? count : mincount;
		loop--;
		return;
	}
	field[pos.second][pos.first].mark = true;
	for(int i = 0; i < 4; i++){
		if((pos.first+dx[i]<0||pos.first+dx[i]>=field[0].size())||(pos.second+dy[i]<0||pos.second+dy[i]>=field.size())) continue;
		if(field[pos.second+dy[i]][pos.first+dx[i]].kind != 'X' && !field[pos.second+dy[i]][pos.first+dx[i]].mark){
			pair<int, int> tmp(pos.first+dx[i], pos.second+dy[i]);
			dfs(target, tmp, field, count + 1);
		}
	}
	field[pos.second][pos.first].mark = false;
}

pair<int, int> decideStart(char target, vector<vector<OBJ> > &field){
	for(int i = 0; i < field.size(); i++){
		for(int j = 0; j < field[i].size(); j++){
			if(field[i][j].kind == target) return pair<int, int>(j, i);
		}
	}
}

int main(){
	int h = 0, w = 0, n = 0;
	cin >> h >> w >> n;
	cin.ignore();
	pair<int, int> start;
	vector<vector<OBJ> > field(h, vector<OBJ>(w, OBJ(' ', false)));
	for(int i = 0; i < h; i++){
		string tmp;
		getline(cin, tmp);
		for(int j = 0; j < w; j++){
			field[i][j].kind = tmp[j];
			if(tmp[j] == 'S') start = pair<int, int>(j, i);
		}
	}
	int count = 0;
	for(int i = 1; i <= n; i++){
		loop = 1500;
		mincount = h*w;
		dfs(i+'0', start, field, 0);
		count += mincount;
		start = decideStart(i+'0', field);
	}

	cout << count << endl;

	return 0;
}

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

int H, W, N, sy, sx, ans, d[10000][10000];
char c[10000][10000];
int dy[4] = {1, -1, 0, 0}, dx[4] = {0, 0, 1, -1};

void bfs() {
    ans = 0;
    for (int i = 1; i <= N; ++i) {
        queue< pair<int, int> > que;
        que.push(pair<int, int>(sy, sx));
        for (int y = 0; y < H; ++y) {
            for (int x = 0; x < W; ++x) d[y][x] = -1;
        }
        d[sy][sx] = 0;
        while (que.size()) {
            pair<int, int> p = que.front();
            que.pop();
            if (c[p.first][p.second] - '0' == i) {
                sy = p.first;
                sx = p.second;
                ans += d[p.first][p.second];
                break;
            }
            for (int j = 0; j < 4; ++j) {
                int ny = p.first + dy[j], nx = p.second + dx[j];
                if (ny >= 0 && ny < H && nx >= 0 && nx < W && c[ny][nx] != 'X' && d[ny][nx] == -1) {
                    que.push(pair<int, int>(ny, nx));
                    d[ny][nx] = d[p.first][p.second] + 1;
                }
            }
        }
    }
}

int main() {
    cin >> H >> W >> N;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            cin >> c[i][j];
            if (c[i][j] == 'S') {
                sy = i;
                sx = j;
            }
        }
    }
    bfs();
    cout << ans << endl;
    
    return 0;
}

C++: #include "stdio.h"
#include "queue"
#include "utility"
using namespace std;
typedef pair<int, int>P;

queue<P>Q;
P now;
P goal[10];
int N;
int W;
int H;
char map[1000][1000] = {};
int x[4] = { 1,0,-1,0 };
int y[4] = { 0,1,0,-1 };
int dis[1000][1001] ;
int ans=0;
int box = 0;

int main() {
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 0; i < H; i++) {
		scanf("%s", map[i]);
	}
	//printf("\n");
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (map[i][j] >= '1'&&map[i][j] <= '9') {
				goal[map[i][j] - '0'].first = i;
				goal[map[i][j] - '0'].second = j;
				map[i][j] = '.';
			}
			else if (map[i][j] == 'S') {
				now.first = i;
				now.second = j;
				map[i][j] = '.';
			}
		}
	}
	for (int i = 1; i <= N; i++) {
		Q.push(now);
		//printf("%d %d????????¢?´¢\n", now.first, now.second);
		box = 0;
		for (int j = 0; j < H; j++) {
			for (int k = 0; k < W; k++) {
				dis[j][k] = 10000000;
			}
		}
		dis[Q.front().first][Q.front().second] = 0;
		while (!Q.empty()) {
			for (int j = 0; j < 4; j++) {
				if (map[Q.front().first + y[j]][Q.front().second + x[j]] == '.') {
					if (dis[Q.front().first + y[j]][Q.front().second + x[j]] > dis[Q.front().first][Q.front().second] + 1) {
						now.first = Q.front().first + y[j];
						now.second = Q.front().second + x[j];
						dis[now.first][now.second] = dis[Q.front().first][Q.front().second] + 1;
					//	printf("%d %d??????%d %d????????£???\n", Q.front().first, Q.front().second, now.first, now.second);
						Q.push(now);
						if (now.first == goal[i].first&&now.second == goal[i].second) {
						//	printf("hijiki\n");
							box = dis[now.first][now.second];
							while (!Q.empty()) {
								Q.pop();
							}
							if (Q.empty())break;
						}
					}
				}
			}
			//printf("hijiki\n");
			if (!Q.empty()) {
				Q.pop();
			}
			//printf("hijiki\n");
		}
		//printf("hijiki\n");
		ans += box;
	}
	printf("%d\n", ans);
	return 0;
}