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<cstdio>
#include<queue>
#include<map>
#include<utility>
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
int d[1000][1000]={{0}};
char a[1000][1000]={{0}};
int h,w,x,y,z=0,ans=0;
char life = '1',n;
using namespace std;
struct P{
    int x;
    int y;
    int z;
};
void bfs(int x,int y,int ans);
void reset();
int main(){

    cin>>h>>w>>n;
    reset();

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>a[i][j];
            if(a[i][j]=='S'){
                x = i;
                y = j;
            }
        }
    }
    bfs(x,y,0);



    return 0;
}
void bfs(int x,int y,int ans){
    queue <P> q;
    P p;
    int Q = 1,z = 0;

    p.x = x;
    p.y = y;
    p.z = z;
    q.push(p);
    d[x][y]=0;
    while(!q.empty()){
        p = q.front();
        if(p.x==-1){
            q.pop();
            Q++;
            continue;
        }/*
        cout<<"----"<<endl;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cout<<" ";
                if(d[i][j]==100000)cout<<'x';
                else cout<<d[i][j];
            }
            cout<<"  "<<z<<endl;
        }
        cout<<endl;
*/
        q.pop();
        x = p.x;
        y = p.y;
        z = p.z;
        if(a[x][y]==life){

            ans += z;
            if(life==n){
                cout<<ans<<endl;
                return;
            }
            life++;
            reset();
            d[x][y]=0;
            bfs(x,y,ans);
            return;
        }
        else{
            d[x][y] = z;
        }
        if(x!=0&&d[x+dx[0]][y+dy[0]]==100000&&a[x+dx[0]][y+dy[0]]!='X'){
            p.x = x+dx[0],p.y = y+dy[0],p.z = z+1;
            q.push(p);
        }
        if(y!=w-1&&d[x+dx[1]][y+dy[1]]==100000&&a[x+dx[1]][y+dy[1]]!='X'){
            p.x = x+dx[1],p.y = y+dy[1],p.z = z+1;
            q.push(p);
        }
        if(x!=h-1&&d[x+dx[2]][y+dy[2]]==100000&&a[x+dx[2]][y+dy[2]]!='X'){
            p.x = x+dx[2],p.y = y+dy[2],p.z = z+1;
            q.push(p);
        }
        if(y!=0&&d[x+dx[3]][y+dy[3]]==100000&&a[x+dx[3]][y+dy[3]]!='X'){
            p.x = x+dx[3],p.y = y+dy[3],p.z = z+1;
            q.push(p);
        }
        p.x = p.y = p.z = -1;
        q.push(p);
    }
    return;

}
void reset(){
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            d[i][j]=100000;
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define loop(i,n) for(int i=1;i<=n;i++)
typedef pair<int,int> P;
char a[1002][1002];
P f[12];
int d[1002][1002];

int bfs(P from,P to){
  loop(i,1000)loop(j,1000)d[i][j]=-1;
  queue<P>que;
  que.push(from);
  d[from.first][from.second]=0;
  int dir[5]={0,-1,0,1,0};
  while(!que.empty()){
    P p=que.front();que.pop();
    if(p.first==to.first&&p.second==to.second)break;
    loop(i,4){
      int qx=p.first+dir[i-1],qy=p.second+dir[i];
      if(a[qx][qy]!='X'&&d[qx][qy]==-1){
        que.push(P(qx,qy));
        d[qx][qy]=d[p.first][p.second]+1;
      }
    }
  }
  return d[to.first][to.second];
}

int main(){
  int h,w,n;
  char t;
  cin>>h>>w>>n;
  loop(i,1002)loop(j,1002)a[i-1][j-1]='X';
  loop(i,h)loop(j,w){
    cin>>t;
    if('1'<=t&&t<='9'){
      f[t-'0']=P(i,j);
    }else if(t=='S'){
      f[0]=P(i,j);
    }
    a[i][j]=t;
  }
  int c=0;
  loop(i,n){
    c+=bfs(f[i-1],f[i]);
  }
  cout<<c<<endl;
  return 0;
}

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