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<stdio.h>
#include<queue>
using namespace std;
int mt,my,mn,X,Y,T;
int field[9][1010][1010] = {0};
int dx[] = {-1,0,1,0},dy[] = {0,-1,0,1};
int mouse(int i,int x,int y,int time)
{//printf("%d %d %d\n",x,y,time);
    queue<int> QX,QY,QT;
    QX.push(x);QY.push(y);QT.push(time);
    field[i][x][y] = i + 2;
    while(QX.size())
    {
        /*for(int l = 0; l < 10; l++)
        {
            for(int j = 0; j < 10; j++)
            {
                printf("%3d",field[i][j][l]);
            }
            printf("\n");
        }printf("\n");*/
        for(int j = 0; j < 4; j++)
        {
            if(0 > QX.front() + dx[j] || QX.front() + dx[j] >= my || 0 > QY.front() + dy[j] || QY.front() + dy[j] >= mt);
            else if(field[i][QX.front() + dx[j]][QY.front() + dy[j]] == i + 1)
            {
                X = QX.front() + dx[j];Y = QY.front() + dy[j];T = QT.front() + 1;
                return 0;
            }
            else if(field[i][QX.front() + dx[j]][QY.front() + dy[j]] == 0)
            {
                QX.push(QX.front() + dx[j]);QY.push(QY.front() + dy[j]);QT.push(QT.front() + 1);
                field[i][QX.front() + dx[j]][QY.front() + dy[j]] = i + 2;
            }
        }
        QX.pop();QY.pop();QT.pop();
    }
}
int main()
{
    char cha;
    scanf("%d%d%d",&mt,&my,&mn);
    scanf("%c",&cha);
    for(int i = 0; i < mt; i++)
    {
        for(int j = 0; j < my + 1; j++)
        {
            scanf("%c",&cha);
            if(cha == 'S')
            {
                X = j;Y = i;T = 0;
            }
            else if(cha == 'X')
            {
                for(int k = 0; k < 9; k++)
                {
                    field[k][j][i] = -1;
                }
            }
            else if('0' <= cha && cha <= '9')
            {
                field[cha - '0' - 1][j][i] = cha - '0';
            }
        }
    }//ok
    /*for(int k = 0; k < 9; k++)
    {
        for(int i = 0; i < t; i++)
        {
            for(int j = 0; j < y; j++)
            {
                printf("%3d",field[k][j][i]);
            }
            printf("\n");
        }
        printf("\n");
    }*/
    for(int i = 0; i < mn; i++)
    {
        mouse(i,X,Y,T);
    }
    printf("%d\n",T);
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <numeric>
#include <climits>
#include <iterator>
#include <iomanip>
#include <stack>
#include <set>
#include <bitset>
#include <functional>
using namespace std;
const constexpr int INF = 1e9;
//typedef std::pair<std::string,double> P;

#define FOR(i, a, n) for (ll i = (ll)a; i<(ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;
typedef vector<int> VI;
const constexpr ll MOD = 1e9 + 7;

struct Less {
	bool operator()(const pair<int, int>& x, const pair<int, int>& y) const {
		return x.first > y.first;
	}
};
//グラフの隣接リスト
//VI g[200010];
//頂点の入次数を管理
int h[100010];

int g[101][101];
int dxy[5]={-1, 0, 1, 0, -1};
vector<string> field;
int H, W, N, sy, sx, gy, gx;

int bfs(){
    int dist[1010][1010];
    for(int i=0; i<H; ++i){
        for(int j=0; j<W; ++j){
            dist[i][j] = 1e9;
        }
    }
    dist[sy][sx]=0;
    queue<pair<int, int> > que;
    que.push({sy, sx});
    
    while(!que.empty()){
        pair<int, int> p = que.front(); que.pop();
        
        if(p.first==gy&&p.second==gx) break;
        
        for(int i=0; i<4; ++i){
            int ny = p.first + dxy[i+1],
                nx = p.second + dxy[i];
            if(ny>=0&&ny<H&&nx>=0&&nx<W&&dist[ny][nx]==1e9&&field[ny][nx]!='X'){
                que.push({ny, nx});
                dist[ny][nx] = dist[p.first][p.second]+1;
            }
        }
    }
    
    return dist[gy][gx];
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> H >> W >> N;
    cin.ignore();
    REP(i, H){
        string s; getline(cin, s);
        field.push_back(s);
    }
    
    vector<pair<int, int> > vp;
    
    REP(j, H){
            REP(k, W){
                if(field[j][k]=='S'){
                    vp.push_back({j, k});
                } 
            }
        }
    for(int i=0; i<N; ++i){
        REP(j, H){
            REP(k, W){
                if(field[j][k]==(char)('1'+i)){
                    vp.push_back({j, k});
                } 
            }
        }
    }
    ll ans=0;
    for(int i=0; i<(int)vp.size()-1; ++i){
        sy = vp[i].first; sx =vp[i].second;
        gy = vp[i+1].first; gx =vp[i+1].second;
        
        ans += bfs();
    }
    cout << ans << endl;
	return 0;
}

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

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

int H,W,N;
vector<string> maze;
int bfs(int sx,int sy,int gx,int gy){
	int visited[1001][1001];
	int dist[1001][1001];
	memset(visited,0,sizeof(visited));
	memset(dist,0,sizeof(dist));
	
	queue<int> qx,qy;
	qx.push(sx);qy.push(sy);

	dist[sy][sx] = 0;
	visited[sy][sx] = 1;

	while(!qx.empty()){
		int x = qx.front();qx.pop();
		int y = qy.front();qy.pop();
		for(int i=0;i<4;i++){
			int nx = x + dx[i];
			int ny = y + dy[i];
			if( nx < 0 || ny < 0 || ny >= H || nx >= W )continue;
			if( maze[ny][nx] != '.')continue;
			if( visited[ny][nx] )continue;

			qx.push(nx);
			qy.push(ny);
			dist[ny][nx] = dist[y][x] + 1;
			visited[ny][nx] = 1;

			if(ny == gy && nx == gx)return dist[ny][nx];
		}
	}
	return -1;
}

int main(void){
	
	cin >> H >> W >> N;

	maze = vector<string>(H);
	vector<int> gxs(N+1),gys(N+1);
	for(int i=0;i<H;i++){
		cin >> maze[i];
		for(int j=0;j<W;j++){
			char c = maze[i][j];
			if( c != '.' && c != 'X' ){
				if( c == 'S' ){
					gys[0] = i;
					gxs[0] = j;
				}else{
					gys[c - '0'] = i;
					gxs[c - '0'] = j;
				}
				maze[i][j] = '.';
			}
		}
	}

	int ret = 0;
	for(int i=0;i<N;i++){
		ret += bfs( gxs[i], gys[i], gxs[i+1], gys[i+1] );
	}
	cout << ret << endl;

	return 0;
}