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<algorithm>
#define  point pair<int,int>
using namespace std;
char map[1000][1000];
int dist[1000][1000];
int dy[4] = { 0,1,0,-1 };
int dx[4] = { 1,0,-1,0 };
int H, W;
int ans;
vector<pair<int, point>> s;
int sx, sy, ex, ey;
void BFS()
{
	
	for (int i = 0; i < H; ++i)
		for (int j = 0; j < W; ++j)
			dist[i][j] = 1000000;
	queue<point> node;
	node.push({ sx, sy });
	dist[sx][sy] = 0;
	while (1)
	{
		point p = node.front();
		node.pop();
		for (int i = 0; i <= 3; ++i)
		{
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if (0 <= nx&&nx < H&&ny < W&&ny >= 0 && dist[nx][ny] == 1000000 && map[nx][ny] != 'X')
			{
				dist[nx][ny] = dist[p.first][p.second] + 1;
				if (nx == ex&&ny == ey)
				{
					ans += dist[nx][ny];
					return;
				}
				node.push({ nx,ny });
			}
		}
	}
}
int main()
{
	int index = 0;
	int N;
	cin >> H >> W >> N;
	for (int i = 0; i < H; ++i)
		for (int j = 0; j < W; ++j)
		{
			cin >> map[i][j];
			if (map[i][j] == 'S')
				s.push_back({ 0,{i,j} });
			if ('1' <= map[i][j] && map[i][j] <= '9')
				s.push_back({ map[i][j] - '0',{i,j} });
		} 
	sort(s.begin(), s.end());
	for (int i = 0; i < s.size()-1 ; ++i)
	{
		for (int i = 0; i < H; i++)
			for (int j = 0; j < W; ++j)
				dist[i][j] = 1000000;
		sx = s[i].second.first;
		sy = s[i].second.second;
		ex = s[i + 1].second.first;
		ey = s[i + 1].second.second;
		BFS();
	}
	cout << ans << endl;
	return 0;
}

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

typedef struct{
	int x;
	int y;
}point;

long d[1000][1000]={0};

int main(){
	int h, w, n;
	char f[1001][1001];
	point start, goal[9];
	
	scanf(" %d %d %d", &h, &w, &n);
	for(int i=0; i<h; ++i){
		for(int j=0; j<w; ++j){
			scanf(" %c", &f[i][j]);
			
			if(f[i][j]=='S'){
				start.x=j;
				start.y=i;	
			}
			else if('1'<=f[i][j] && f[i][j]<='1'+n-1){
				goal[f[i][j]-'1'].x=j;
				goal[f[i][j]-'1'].y=i;	
			}
		}
	}
	
	int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1};
	int now=0;
	long ans=0;
	while(now<n){
		memset(d, -1, sizeof(d));
		
		queue<point> que;
		d[start.y][start.x]=0;
		que.push(start);
		
		while( !que.empty() ){
			point v=que.front();
			que.pop();
			for(int i=0; i<4; ++i){
				int nx=v.x+dx[i], ny=v.y+dy[i];
				if(nx>=0 && nx<w && ny>=0 && ny<h && f[ny][nx]!='X' && d[ny][nx]==-1){
					d[ny][nx]=d[v.y][v.x]+1;
					point tmp;
					tmp.x=nx; tmp.y=ny;
					que.push(tmp);
				}
			}	
		}
		
		ans+=d[goal[now].y][goal[now].x];
		start=goal[now];
		now++;
	}
	
	printf("%ld\n", ans);
}

C++: #include <cstdio>
#include <vector>
#include <iostream>

const int STEP_NUM = 4;
const int STEP[STEP_NUM][STEP_NUM] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

const int MAX_SIZE = 1000;
char field[MAX_SIZE + 1][MAX_SIZE + 1];

const int MAXN = 10;
int locationsX[MAXN];
int locationsY[MAXN];
 
int H, W, N;

int get_shortest_distance(int current_HP)
{
	int sx = locationsX[current_HP-1];
	int sy = locationsY[current_HP-1];
	int ex = locationsX[current_HP];
	int ey = locationsY[current_HP];

	bool is_visit[MAX_SIZE + 1][MAX_SIZE + 1]={};
	is_visit[sx][sy]=true;

	std::vector<std::pair<int,int>> frontier;
	frontier.push_back(std::pair<int,int>{sx,sy});
	int nx,ny;
	for(int step=0;step<W*H;step++)
	{
		std::vector<std::pair<int,int>> next_frontier;
		for(auto p:frontier)
			for(int i=0;i<STEP_NUM;i++)
			{
				nx = p.first+STEP[i][0];
				ny = p.second+STEP[i][1];
				if(nx<0 || H<=nx) 		continue;
				if(ny<0 || W<=ny) 		continue;
				if(field[nx][ny]=='X') 	continue;
				if(is_visit[nx][ny]) 	continue;
				next_frontier.push_back(std::make_pair(nx,ny));
				is_visit[nx][ny] = true;
				if(nx==ex && ny==ey)	return step+1;
			}
		frontier = next_frontier;
	}
	return -1;
}

int solve()
{
	int total_step = 0;
	for(int i=1;i<N+1;i++)
		total_step += get_shortest_distance(i);
	return total_step;
}

int main()
{
	scanf("%d %d %d", &H, &W, &N);
	for(int i=0; i<H; i++)
		scanf("%s", field[i]);
	for(int i=0; i<H; i++)
		for(int j=0; j<W; j++)
		{
			int HP=-1;
			if(field[i][j]=='S')	HP=0;
			if(field[i][j]=='1')	HP=1;
			if(field[i][j]=='2')	HP=2;
			if(field[i][j]=='3')	HP=3;
			if(field[i][j]=='4')	HP=4;
			if(field[i][j]=='5')	HP=5;
			if(field[i][j]=='6')	HP=6;
			if(field[i][j]=='7')	HP=7;
			if(field[i][j]=='8')	HP=8;
			if(field[i][j]=='9')	HP=9;
			if(HP!=-1)
			{
				locationsX[HP]=i;
				locationsY[HP]=j;
			}
		}
	printf("%d\n", solve());
	return 0;
}