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>
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));
	std::cout << wfs(vector, memo, Queue<Route>(h * w * 4), start, n);
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#define inf 1000000
#define max 1010
using namespace std;
int H,W,N,x0,y0;
char map[max][max];
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int d[max][max]; 
typedef pair<int,int>P;
struct position
{
	int rx,ry,e;
}ed[9];
bool comp(const position a,const position b)
{
	return a.e<b.e;
}
int bfs(int sx,int sy,int gx,int gy)
{
	int tx,ty;
	queue<P>q;
	for(int i=0;i<H;i++)
	{
		for(int j=0;j<W;j++)
		d[i][j]=inf;
	}
	q.push(P(sx,sy));
	d[sx][sy]=0;
	while(!q.empty())
	{
		P p=q.front();
		if(p.first==gx&&p.second==gy)
		return d[p.first][p.second];
		q.pop();
		for(int i=0;i<4;i++)
		{
			tx=p.first+dx[i];
			ty=p.second+dy[i];
			if(tx>=0&&tx<H&&ty>=0&&ty<W&&map[tx][ty]!='X'&&d[tx][ty]==inf)
			{
				d[tx][ty]=d[p.first][p.second]+1;
				q.push(P(tx,ty));
			}
		}
	}
}
int main(void)
{
	while(scanf("%d%d%d",&H,&W,&N)==3)
	{
		int x1[9],y1[9];
		for(int i=0;i<H;i++)
		scanf("%s",&map[i]);
		int t=0;
		for(int i=0;i<H;i++)
		{
			for(int j=0;j<W;j++)
			{
				if(map[i][j]=='S')
				{
					x0=i;
					y0=j;
				}
				if(map[i][j]-'0'>0&&map[i][j]-'0'<=9)
				{
					ed[t].rx=i;
					ed[t].ry=j;
					ed[t].e=map[i][j]-'0';
					t++;
				}
			}
		}
		sort(ed,ed+N,comp);
		int ans=bfs(x0,y0,ed[0].rx,ed[0].ry);
		for(int i=0;i<N-1;i++)
		ans+=bfs(ed[i].rx,ed[i].ry,ed[i+1].rx,ed[i+1].ry);
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<cmath>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#define debug(ARGS) cout << #ARGS << ": " << ARGS << endl;
#define ll long long

#define INF (int)1e7
#define p pair<int, int>
#define F 0
#define X -1
#define S -2

using namespace std;

int convert(char c){
  int ret;

  if('1' <= c && c <= '9') ret = c - '1' + 1;
  else if(c == '.') ret = F;
  else if(c == 'X') ret = X;
  else if(c == 'S') ret = S;
  else ret = -3;

  return ret;
}

p find_nest(vector< vector<int> > v, int num){
  int h = v.size(), w = v[0].size();
  p ret;
  
  for(int i = 0; i < h; i++)
    for(int j = 0; j < w; j++)
      if(v[i][j] == num) ret = p(i, j);

  return ret;
}

int count_minite(vector< vector<int> > v, int start, int end){
  int h = v.size(), w = v[0].size();
  int ret = -1;
  vector< vector<bool> > m; m.resize(h); for(int i = 0; i < h; i++) m[i].resize(w, false);
  queue< pair< p, int> > q;
  q.push(pair< p, int>(find_nest(v, start), 0));

  while(!q.empty()){
    p here = q.front().first; 
    ret = q.front().second;
    q.pop();
    int y = here.first, x = here.second;
    
    if(v[y][x] == end) break;
    else if(v[y][x] == X) continue;

    m[y][x] = true;
    
    if(x > 0 && !m[y][x-1]) q.push(pair< p, int>(p(y, x-1), ret+1));
    if(x < w - 1 && !m[y][x+1]) q.push(pair< p, int>(p(y, x+1), ret+1));
    if(y > 0 && !m[y-1][x]) q.push(pair< p, int>(p(y-1, x), ret+1));
    if(y < h - 1 && !m[y+1][x]) q.push(pair< p, int>(p(y+1, x), ret+1));
  }
  
  return ret;
}

int main(){
  int h, w, n;
  cin >> h; cin >> w; cin >> n;
  vector< vector<int> > v;  
  v.resize(h); for(int i = 0; i < h; i++)  v[i].resize(w);
  int count = 0;

  for(int i = 0; i < h; i++)
    for(int j = 0; j < w; j++){
      char c; cin >> c;
      v[i][j] = convert(c);
    }

  int start = S;  
  for(int i = 1; i <= n; i++){
    count += count_minite(v, start, i);
    start = i;
  }

  cout << count << endl;
  
  return 0;
}