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<deque>
#include<memory.h>

using namespace std;

int H, W, N;
char mapT[1000][1000];
int vis[1000][1000];
int temp_x = 0, temp_y;
char temp;
int final_min_route = 0;
int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };

struct location {
	int x, y, route;
};

location findSmallRouteBFS(int x, int y, char factory) {
	memset(vis, 0, sizeof(vis));
	deque<location> que;
	location temp = { x,y,0 };
	que.push_back(temp);
	vis[x][y] = 1;
	while (!que.empty()) {
		location st = que.front();
		que.pop_front();
		if (mapT[st.x][st.y] == factory) {
			return st;
		}
		for (int i = 0; i < 4; ++i) {
			int t_x = st.x + dir[i][0];
			int t_y = st.y + dir[i][1];
			if (t_x < 0 || t_x >= H || t_y < 0 || t_y >= W || mapT[t_x][t_y] == 'X') {
				continue;
			}
			location temp = { t_x,t_y,st.route + 1 };
			if (!vis[t_x][t_y]) {
				vis[t_x][t_y] = 1;
				que.push_back(temp);
			}
		}
	}

}

int main() {
	cin >> H >> W >> N;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; ++j) {
			cin >> temp;
			mapT[i][j] = temp;
			if (temp == 'S') {
				mapT[i][j] = '0';
				temp_x = i;
				temp_y = j;
			}
		}
	}

	for (int factory = 1; factory != N + 1; ++factory) {

		location lo = findSmallRouteBFS(temp_x, temp_y, mapT[temp_x][temp_y] + 1);
		temp_x = lo.x;
		temp_y = lo.y;
		final_min_route += lo.route;
	}

	cout << final_min_route << endl;
	//system("pause");
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
vector<vector<char> > v(100,vc(100));
vector<pii>u(1000);
int mx[]={0,0,1,-1};
int my[]={1,-1,0,0};
int h,w,n;
int bfs(pii st,pii g){
    queue<pii>que;
    vvi d(1000,vi(1000,INF));
    que.push(st);
    d[st.first][st.second]=0;
    while(!que.empty()){
        pii p=que.front(); que.pop();
        if(p==g)break;
        rep(i,4){
            int ny=p.first+my[i]; int nx=p.second+mx[i];
            pii np=pii(ny,nx);
            if(0<=ny&&ny<h&&0<=nx&&nx<w&&d[ny][nx]==INF&&v[ny][nx]!='X')
            {
                d[ny][nx]=d[p.first][p.second]+1;
                que.push(np);
            }
        }
    }
    return d[g.first][g.second];
}
int main(){
    pii st;
    cin>>h>>w>>n;
    int ans=0;
    rep(i,h)rep(j,w){
        cin>>v[i][j];
        if(v[i][j]=='S')st=pii(i,j);
            else if(v[i][j]!='X'&&v[i][j]!='.'){
            u[(v[i][j]-48)]=pii(i,j);
            //cout<<v[i][j]-48<<endl;
        }
    }
    u[0]=st;
    for(int i=0;i<n;i++){
        ans+=bfs(u[i],u[i+1]);
    }
    cout<<ans<<endl;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll = long long;
const ll INF = 1e9;

ll H, W, N;
vector<vector<ll>> F(10);
string M[1001];

ll dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

ll bfs(ll sx, ll sy, ll gx, ll gy) {
    ll d[1001][1001];
    queue<vector<ll>> que;
    for(ll i = 0; i < H; i++) for (ll j = 0; j < W; j++) d[i][j] = INF;

    que.push({sx, sy});
    d[sy][sx] = 0;

    while(que.size()) {
        vector<ll> p = que.front(); que.pop();
        if (p[0] == gx && p[1] == gy) break;

        for (ll i = 0; i < 4; i++) {
            ll nx = p[0] + dx[i], ny = p[1] + dy[i];
            if (M[ny][nx] != 'X' && nx >= 0 && nx < W && ny >= 0 && ny < H && d[ny][nx] == INF) {
                que.push({nx, ny});
                d[ny][nx] = d[p[1]][p[0]] + 1;
            }
        }
    }

    return d[gy][gx];
}

ll main() {
    ll ans = 0;
    cin >> H >> W >> N;
    for (ll i = 0; i < H; i++) {
        cin >> M[i];
        for (ll k = 0; k < W; k++) {
            if (M[i][k] == 'S') {
                F[0] = {k, i};
            }
            if (isdigit(M[i][k])) {
                F[M[i][k] - '0'] = {k, i};
            }
        }
    }

    for (ll i = 0; i < N; i++) {
        ans += bfs(F[i][0], F[i][1], F[i+1][0], F[i+1][1]);
    }

    cout << ans << endl;

    return 0;
}