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<cstdio>
#include<cstring>
#include<utility>
using namespace std;
struct dir
{
	int x,y;
}dir[4]={{-1,0},{1,0},{0,-1},{0,1}};
char hole[1005][1005],factory[10][2];
int d[1005][1005],h,w;
typedef pair<int,int> P;
int bfs(int sx,int sy,int gx,int gy)
{
	memset(d,-1,sizeof(d));
	queue<P> que;
	que.push(P(sx,sy));
	d[sx][sy]=0;
	while(que.size())
	{
		P p=que.front();
		que.pop();	
		if(p.first==gx&&p.second==gy)
		break;
		for(int i=0;i<4;i++)
		{
		int nx=p.first+dir[i].x;
		int ny=p.second+dir[i].y;
		if(nx>=0&&ny>=0&&nx<h&&ny<w&&hole[nx][ny]!='X'&&d[nx][ny]==-1)
		{
			d[nx][ny]=d[p.first][p.second]+1;
			que.push(P(nx,ny));
		}
		}
	}
	return d[gx][gy];
}
int main()
{
	int sx,sy,i,j,step=0,n;
	scanf("%d%d%d\n",&h,&w,&n);
	//cin>>h>>w>>n;
	for(i=0;i<h;i++,getchar())
		for(j=0;j<w;j++)
		{
			scanf("%c",&hole[i][j]);
			if(hole[i][j]=='S')
			{
				factory[0][0]=i;
				factory[0][1]=j;
			}
			if(isdigit(hole[i][j]))
			{
			factory[hole[i][j]-'0'][0]=i;
			factory[hole[i][j]-'0'][1]=j;
			}
		}
	for(i=0;i<n;i++)
	step+=bfs(factory[i][0],factory[i][1],factory[i+1][0],factory[i+1][1]);
	//cout<<step<<endl;
	printf("%d",step);
	return 0;
 }

C++: #if 0
#cflags(-std=c++14 -Wall)
#endif
#include<iostream>
template<typename T>
T input(std::istream& os = std::cin){T t;os >> t;return t;}
 
#include<vector>
#include<string>
#include<queue>
#include<numeric>
 
struct pos{
  int x, y;
  template<typename T>
  auto&& operator[](T&& t)const{return std::forward<T>(t)[y][x];}
};
template<typename F>
struct itr{
  int r;
  std::pair<pos, int> t;
  F shortest;
  bool flag;
  itr& operator++(){++r;flag = true;return *this;}
  int& operator*(){if(flag)t = shortest(r, t.first.x, t.first.y), flag = false;return t.second;}
  bool operator==(const itr& other)const{return r == other.r;}
  bool operator!=(const itr& other)const{return !(*this == other);}
};
template<typename F>
itr<F> make_itr(const pos& p, F&& f){return ++itr<F>{0, {p, 0}, f};}
template<typename F>
itr<F> make_itr(int r, F&& f){return itr<F>{r, {}, f};}
 
int main(){
  const auto H = input<int>();
  const auto W = input<int>();
  const auto N = input<int>();
  const std::vector<std::string> data = [H]{
    std::vector<std::string> data;
    data.reserve(H);
    for(int i = 0; i < H; ++i)
      data.emplace_back(input<std::string>());
    return data;
  }();
  const pos po = [&]{
    pos po;
    for(int j = 0; j < H; ++j)
      for(int i = 0; i < W; ++i)
        if(data[j][i] == 'S')
          po = {i, j};
    return po;
  }();
  auto shortest = [&](int rank, int i, int j){
    std::vector<std::vector<char>> flags(H, std::vector<char>(W));
    std::queue<std::pair<pos, int>> q;
    auto add = [&](const pos& po, int t){
      if(!po[flags])
        if(po[data] != 'X')
          q.push({po, t+1});
       po[flags] = true;
    };
    add({i, j}, -1);
    while(true){
      const auto& d = q.front();
      const auto& p = d.first;
      if(p[data] == rank+'0')
        return q.front();
      for(int a = -1; a <= 1; a += 2)
        if(0 <= p.x + a && p.x + a < W)
          add(pos{p.x+a, p.y}, d.second);
      for(int b = -1; b <= 1; b += 2)
        if(0 <= p.y + b && p.y + b < H)
          add(pos{p.x, p.y+b}, d.second);
      q.pop();
    }
    return q.front();
  };
  std::cout << std::accumulate(make_itr(po, shortest), make_itr(N+1, shortest), 0) << std::endl;
}