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 "stdafx.h"
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 1000000000
using namespace std;

int row, col, n;

typedef struct point {
	int x, y;
};

char map[1000][1000];
int d[1000][1000];
int direct[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };

int BFS(int &startX, int &startY, char target) {
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			d[i][j] = INF;

	queue<point> q;
	point p;
	p.x = startX;
	p.y = startY;
	d[startX][startY] = 0;
	q.push(p);
	while (!q.empty()) {
		int nowX = q.front().x,nowY = q.front().y;
		q.pop();

		if (map[nowX][nowY] == target) {
			startX = nowX;
			startY = nowY;
			return d[nowX][nowY];
		}

		int nextX, nextY;
		for (int i = 0; i < 4; i++) {
			nextX = nowX + direct[i][0];
			nextY = nowY + direct[i][1];

			if (nextX >= 0 && nextY >= 0 && nextX < row && nextY < col && 
					map[nextX][nextY] != 'X' && d[nextX][nextY] == INF) {
				d[nextX][nextY] = d[nowX][nowY] + 1;
				p.x = nextX;
				p.y = nextY;
				q.push(p);
			}
		}
	}
	return -1;
}

int main() {
	int startX, startY, sum = 0;

	scanf("%d%d%d", &row, &col, &n);
	for (int i = 0; i < row; i++) {
		scanf("%s", map[i]);
		for (int j = 0; j < col; j++) {
			if (map[i][j] == 'S') {
				startX = i;
				startY = j;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		int dis = BFS(startX, startY, i + '1');
		sum += dis;
	}
	printf("%d\n", sum);
}

C++: #include<iostream>
#include<queue>
#include<limits.h>

int h, w, n;
char field[1001][1001];
int sx, sy, gx[9], gy[9];
int d[1000][1000];
const int INF = INT_MAX / 4;
int dx[] = { 0, 1, 0, -1 }, dy[] = { -1, 0, 1, 0 };

int bfs( int idx )
{
	typedef std::pair<int,int> point;
	std::queue<point> q;
	q.push( point( sx, sy ) );
	d[sx][sy] = 0;
	
	while( !q.empty() ){
	 	point p = q.front(); q.pop();
		int x = p.first, y = p.second;
		if( x == gx[idx] && y == gy[idx] ) break;
		
		for( int i = 0; i < 4; i++ ){
			int nx = x + dx[i], ny = y + dy[i];
			if( nx >= 0 && nx < w && ny >= 0 && ny < h && field[nx][ny] != 'X' && d[nx][ny] == INF ){
				d[nx][ny] = d[x][y] + 1;
				q.push( point( nx, ny ) );
			}
		}
	}

	return d[gx[idx]][gy[idx]];
}

void init_d(){
	for( int i = 0; i < w; i++ )
		for( int j = 0; j < h; j++ )
			d[i][j] = INF;

 	return;
}

int main()
{
	std::cin >> h >> w >> n;
	for( int j = 0; j < h; j++ )
		for( int i = 0; i < w; i++ )
			std::cin >> field[i][j];
			
	for( int i = 0; i < w; i++ ){
		for( int j = 0; j < h; j++ ){		
			if( field[i][j] == 'S' ){
			 	sx = i;
				sy = j;
			}
			for( int k = 1; k <= n; k++ ){
				if( field[i][j] == k + '0' ){
					gx[k-1] = i;
					gy[k-1] = j;
				}
			}
		}
	}
	
	init_d();
	
	int ans = 0;
	for( int i = 0; i < n; i++ ){
		ans += bfs( i );
		sx = gx[i];
		sy = gy[i];
		init_d();
	}
	
	std::cout << ans << std::endl;

	return 0;
}

C++: #include <iostream>
#include <vector>
std::vector<std::pair<int, int>> next_list(const int &x, const int &y) {
	return { std::make_pair(x + 1, y), std::make_pair(x - 1, y), std::make_pair(x, y + 1), std::make_pair(x, y - 1) };
}
template<typename T>
class Queue {
public:
	Queue(const int &n) :vector(n), f(0), b(0) {};
	T front() const {
		return vector.at(f);
	}
	T back() const {
		return vector.at(b);
	}
	void pop() {
		f = (f + 1) % vector.size();
	}
	void push(T f) {
		vector.at(b) = f;
		b = (b + 1) % vector.size();
	}
	void clear() {
		f = 0;
		b = 0;
	}
private:
	std::vector<T> vector;
	int f, b;
};
struct Route {
	Route(const int &ax = 0, const int &ay = 0, const int &an = 0, const int &atime = 0) :x(ax), y(ay), n(an), time(atime) {};
	int x, y, n, time;
	bool operator<(const Route &other) const {
		return time < other.time;
	}
};
int wfs(const std::vector<std::vector<int>> &vector, std::vector<std::vector<int>> &memo, Queue<Route> &queue, const Route &start, const int &goal) {
	queue.clear();
	queue.push(start);
	while (vector.at(queue.front().y).at(queue.front().x) != start.n + 1) {
		for (const auto &coord : next_list(queue.front().x, queue.front().y)) {
			if (vector.at(coord.second).at(coord.first) >= 0 && memo.at(coord.second).at(coord.first) != start.n) {
				memo.at(coord.second).at(coord.first) = start.n;
				queue.push(Route(coord.first, coord.second, 0, queue.front().time + 1));
			}
		}
		queue.pop();
	}
	if (start.n + 1 == goal) {
		return queue.front().time;
	}
	else {
		return wfs(vector, memo, queue, Route(queue.front().x, queue.front().y, start.n + 1, queue.front().time), goal);
	}
}
int main() {
	int h, w, n;
	std::cin >> h >> w >> n;
	std::vector<std::vector<int>> vector(h + 2, std::vector<int>(w + 2, -1));
	Route start(0, 0, 0, 0);
	char *arg = new char[w + 2];
	for (auto i = 1; i <= h; ++i) {
		std::cin >> arg;
		for (auto j = 1; j <= w; ++j) {
			switch (arg[j - 1]) {
			case '.':
				vector.at(i).at(j) = 0;
				break;
			case 'X':
				vector.at(i).at(j) = -1;
				break;
			case 'S':
				vector.at(i).at(j) = 0;
				start = Route(j, i, 0, 0);
				break;
			default:
				vector.at(i).at(j) = arg[j - 1] - '0';
			}
		}
	}
	delete[] arg;
	arg = nullptr;
	std::vector<std::vector<int>> memo(h + 2, std::vector<int>(w + 2, -1));
	Queue<Route> queue(h * w);
	std::cout << wfs(vector, memo, queue, start, n) << std::endl;
	return 0;
}