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 <utility>
#include <queue>
using namespace std;

typedef pair<int, int> P;

int main()
{
	int H, W, N, gn = 0, res = 0;
	cin >> H >> W >> N;
	char ch;
	vector< vector<int> > a(W, vector<int>(H, 1000002));
	P start;
	vector< P > goal(N);

	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> ch;
			if (ch == 'X') {
				a[j][i] = -1;
			}
			else if (ch == 'S') {
				start.first = j;
				start.second = i;
			}
			else if (ch >= '1' && ch <= '9') {
				goal[ch - '1'].first = j;
				goal[ch - '1'].second = i;
			}
		}
	}
	while (gn < N) {
		queue< P > bfs;
		bfs.push(start);
		a[start.first][start.second] = 0;
		while (!bfs.empty()) {
			P temp = bfs.front();
			bfs.pop();
			if (temp.first + 1 < W && a[temp.first + 1][temp.second] == 1000002) {
				if (goal[gn].first == temp.first + 1 && goal[gn].second == temp.second) {
					res += a[temp.first][temp.second] + 1;
					break;
				}
				else {
					a[temp.first + 1][temp.second] = a[temp.first][temp.second] + 1;
					bfs.push(P(temp.first + 1, temp.second));
				}
			}
			if (temp.first - 1 >= 0 && a[temp.first - 1][temp.second] == 1000002) {
				if (goal[gn].first == temp.first - 1 && goal[gn].second == temp.second) {
					res += a[temp.first][temp.second] + 1;
					break;
				}
				else {
					a[temp.first - 1][temp.second] = a[temp.first][temp.second] + 1;
					bfs.push(P(temp.first - 1, temp.second));
				}
			}
			if (temp.second + 1 < H && a[temp.first][temp.second + 1] == 1000002) {
				if (goal[gn].first == temp.first && goal[gn].second == temp.second + 1) {
					res += a[temp.first][temp.second] + 1;
					break;
				}
				else {
					a[temp.first][temp.second + 1] = a[temp.first][temp.second] + 1;
					bfs.push(P(temp.first, temp.second + 1));
				}
			}
			if (temp.second - 1 >= 0 && a[temp.first][temp.second - 1] == 1000002) {
				if (goal[gn].first == temp.first && goal[gn].second == temp.second - 1) {
					res += a[temp.first][temp.second] + 1;
					break;
				}
				else {
					a[temp.first][temp.second - 1] = a[temp.first][temp.second] + 1;
					bfs.push(P(temp.first, temp.second - 1));
				}
			}
		}
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (a[j][i] != -1) {
					a[j][i] = 1000002;
				}
			}
		}
		start = goal[gn];
		gn++;
	}
	cout << res << endl;
	return 0;
}

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

int main(){
  int h,w,n;
  int x[10],y[10];
  string map[1000];
  int dmap[1000][1000];
  queue<int> qx,qy;
  int dis;

  cin >> h >> w >> n;

  for(int i=0;i<h;i++)cin >> map[i];
 
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(map[i][j] == 'S'){
	x[0] = j;
	y[0] = i;
	break;
      }
      if(map[i][j] != 'X' && map[i][j] != '.'){
	x[map[i][j] - '0'] = j;
	y[map[i][j] - '0'] = i;
      }
    }
  }

  dis = 0;
  for(int i=0;i<n;i++){
    while(!qx.empty())qx.pop();
    while(!qy.empty())qy.pop();
    for(int k=0;k<h;k++){
      for(int j=0;j<w;j++){
	dmap[k][j] = 10000000;
      }
    }
    dmap[y[i]][x[i]] = 0;
    qx.push(x[i]);
    qy.push(y[i]);

    while(!qx.empty()){
      if(qx.front()>0 && map[qy.front()][qx.front()-1] != 'X' && dmap[qy.front()][qx.front()-1] > dmap[qy.front()][qx.front()] + 1){
	dmap[qy.front()][qx.front()-1] = dmap[qy.front()][qx.front()] + 1;
	qx.push(qx.front()-1);
	qy.push(qy.front());
      }
      if(qy.front()>0 && map[qy.front()-1][qx.front()] != 'X' && dmap[qy.front()-1][qx.front()] > dmap[qy.front()][qx.front()] + 1){
	dmap[qy.front()-1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
	qx.push(qx.front());
	qy.push(qy.front()-1);
      }
      if(qx.front()<w-1 && map[qy.front()][qx.front()+1] != 'X' && dmap[qy.front()][qx.front()+1] > dmap[qy.front()][qx.front()] + 1){
	dmap[qy.front()][qx.front()+1] = dmap[qy.front()][qx.front()] + 1;
	qx.push(qx.front()+1);
	qy.push(qy.front());
      }
      if(qy.front()<h-1 && map[qy.front()+1][qx.front()] != 'X' && dmap[qy.front()+1][qx.front()] > dmap[qy.front()][qx.front()] + 1){
	dmap[qy.front()+1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
	qx.push(qx.front());
	qy.push(qy.front()+1);
      }
      qx.pop();
      qy.pop();
    }
    
    dis += dmap[y[i+1]][x[i+1]];
  }

  cout << dis << endl;
}

C++: #include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge { LL to, cost; };

LL H, W, N;
std::string field[1123];
LL Sx, Sy, xy[11];
LL ans;

LL d[1123456];
std::vector<edge>G[1123456];
void dijkstra(LL s)
{
	std::priority_queue<P, std::vector<P>, std::greater<P>>que;
	std::fill(d, d + H * W + 1, INF);
	d[s] = 0;
	que.push(P(0, s));
	while (!que.empty())
	{
		P p = que.top(); que.pop();
		LL v = p.second;
		if (d[v] < p.first) { continue; }
		rep(i, G[v].size())
		{
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main()
{
	in >> H >> W >> N;
	rep(i, H) { in >> field[i]; }

	rep(x, H)
	{
		rep(y, W)
		{
			if (field[x][y] == 'X') { continue; }
			if (field[x][y] == 'S') { Sx = x; Sy = y; }
			if (field[x][y] != '.') { xy[field[x][y] - '0'] = x*W + y; }
			if (x < H - 1)
			{
				if (field[x + 1][y] != 'X')
				{
					G[x*W + y].push_back({ (x + 1)*W + y,1 });
					G[(x + 1)*W + y].push_back({ x*W + y,1 });
				}
			}
			if (y < W - 1)
			{
				if (field[x][y + 1] != 'X')
				{
					G[x*W + y].push_back({ x*W + (y + 1),1 });
					G[x*W + (y + 1)].push_back({ x*W + y,1 });
				}
			}
		}
	}
	dijkstra(Sx*W + Sy);
	rep(i, N)
	{
		ans += d[xy[i + 1]];
		dijkstra(xy[i + 1]);
	}

	out << ans << std::endl;
	return 0;
}