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<map>
using namespace std;
#define MAX 1001

int h, w, n;
char city[MAX][MAX];
int step[10];
int direc1[4] = { -1,0,1,0 }, direc2[4] = { 0,1,0,-1 };

void dfs(char (&nowcity)[MAX][MAX], int nowstep, bool find, int nowi, int nowj, int nowpower, map<int, int> visited) {
	if (find) {
		if (step[nowpower-1] == -1) {
			step[nowpower-1] = nowstep;
		}
		else
		{
			step[nowpower-1] = min(step[nowpower-1], nowstep);
		}
		return;
	}
	if (step[nowpower] != -1) {
		if (nowstep >= step[nowpower]) {
			return;
		}
	}
	for (int i = 0; i < 4; i++) {
		if (nowi + direc1[i] >= 0 && nowi + direc1[i] < h&&nowj + direc2[i] >= 0 && nowj + direc2[i] < w) {

			if (nowcity[nowi + direc1[i]][nowj + direc2[i]] == '.') {
				if (!visited[(nowi + direc1[i]) * w + nowj + direc2[i]]) {
					visited[(nowi + direc1[i]) * w + nowj + direc2[i]]=1;
					dfs(nowcity, nowstep + 1, false, nowi + direc1[i], nowj + direc2[i], nowpower, visited);
				}	
			}
			else if (nowcity[nowi + direc1[i]][nowj + direc2[i]] >= '0'&&nowcity[nowi + direc1[i]][nowj + direc2[i]] <= '9')
			{
				int num = nowcity[nowi + direc1[i]][nowj + direc2[i]] - '0';
				if (nowpower == num) {					
					//nowcity[nowi + direc1[i]][nowj + direc2[i]] = '.';
					/*visited.clear();
					visited[(nowi + direc1[i]) * w + nowj + direc2[i]] =1;*/
					dfs(nowcity, nowstep + 1, true, nowi + direc1[i], nowj + direc2[i], nowpower,visited);
				}
				else {
					if (!visited[(nowi + direc1[i]) * w + nowj + direc2[i]]) {
						visited[(nowi + direc1[i]) * w + nowj + direc2[i]] =1;
						dfs(nowcity, nowstep + 1, false, nowi + direc1[i], nowj + direc2[i], nowpower,visited);
					}				
				}
			}
		}
	}
}



int main() {
	cin >> h >> w >> n;
	int nowi[10], nowj[10];
	
	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			cin >> city[i][j];
			if (city[i][j] == 'S') {
				nowi[0] = i;
				nowj[0] = j;
				city[i][j] = '.';
			}
			if (city[i][j] >= '0'&&city[i][j] <= '9') {
				nowi[city[i][j] - '0'] = i;
				nowj[city[i][j] - '0'] = j;
			}
		}
	}
	for (int i = 0; i < 10; i++) {
		step[i] = -1;
	}
	map<int, int>visited;
	for (int i = 0; i < n; i++) {
		visited.clear();
		visited[nowi[i] * w + nowj[i]] = 1;
		dfs(city, 0, false, nowi[i], nowj[i], i+1, visited);
	}
	int sum=0;
	for (int i = 0; i < n; i++) {
		sum += step[i];
	}
	cout << sum << endl;
	//system("pause");
	return 0;
}

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

#define MAX_H 1000
#define MAX_W 1000
char m[MAX_H+2][MAX_W+2];
int mindist[MAX_H+2][MAX_W+2];
const int INF = MAX_H*MAX_W*2;
int d[][2] = {1,0, -1,0, 0,1, 0,-1};
using namespace std;

int bfs(int &sx, int &sy, int power){
	//init
	for(int i = 0; i < MAX_H+2; ++i){
		for(int j = 0; j < MAX_W+2; ++j){
			mindist[i][j] = INF;
		}
	}
	queue<pair<int,int>> q;
	q.push(pair<int,int>(sx,sy));
	mindist[sx][sy] = 0;
	while(true){
		auto p = q.front();
		q.pop();
		int x,y;
		x = p.first;
		y = p.second;
		if(m[x][y]-'0' == power){
			sx = x;
			sy = y;
			return mindist[x][y];
		}
		for(int i = 0; i < 4; ++i){
			int x_ = x+d[i][0];
			int y_ = y+d[i][1];
			if(m[x_][y_] == 'X'){
				continue;
			}
			if(mindist[x_][y_] > mindist[x][y]+1){
				q.push(pair<int,int>(x_, y_));
				mindist[x_][y_] = mindist[x][y]+1;
			}
		}
	}
}

int main(){
	int H, W, N;
	cin >> H >> W >> N;
	memset(m, 'X', sizeof m);
	//read
	cin.ignore();
	for(int i = 0; i < H; ++i){
		string s;
		getline(cin, s);
		for(int j = 0; j < W; ++j){
			m[i+1][j+1] = s.at(j);
		}
	}
	//search start
	int minutes = 0;
	int x, y;
	for(int i = 1; i <= MAX_H; ++i){
		for(int j = 1; j <= MAX_W; ++j){
			if(m[i][j] == 'S'){
				x = i;
				y = j;
			}
		}
	}
	for(int i = 1; i <= N; ++i){
		minutes += bfs(x, y, i);
	}
	cout << minutes << endl;
}

C++: #include <iostream>
#include <stdlib.h>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1000 * 1000;
const int MAX_N = 1024;
int W, H, N;
char map[MAX_N][MAX_N];
int sx[10], sy[10];
int ex[10], ey[10];
int d[MAX_N][MAX_N];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
int BFS(int sx, int sy, int ex, int ey) {
	queue<P> que;
	for (int i = 0; i < W; i++) {
		for (int j = 0; j < H; j++) {
			d[i][j] = INF;
		}
	}
	que.push(P(sx, sy));
	d[sx][sy] = 0;
	while (que.size())
	{
		P p = que.front();
		que.pop();
		if (p.first == ex && p.second == ey)
			break;
		for (int i = 0; i < 4; i++) {
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if (nx >= 0 && nx < W&&ny >= 0 && ny < H&&map[nx][ny] != 'X'&&d[nx][ny]==INF) {
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}


	}
	return d[ex][ey];
}
int main() {
	while (cin >> W >> H >> N) {
		int ans = 0;
		for (int i = 0; i < W; i++)
			for (int j = 0; j < H; j++) {
				cin >> map[i][j];
				if (map[i][j] == 'S') {
					sx[0] = i;
					sy[0] = j;
				}
			}
		for (int i = 0; i < W; i++)
			for (int j = 0; j < H; j++) {
				if (map[i][j] > 48 && map[i][j] < 58) {
					sx[map[i][j] - 48] = i;
					sy[map[i][j] - 48] = j;
				}
			}
		for (int i = 0; i < N; i++) {

			ans += BFS(sx[i], sy[i], sx[i + 1], sy[i + 1]);

		}
		cout << ans<<endl;
	}
	
	return 0;
}