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 "bits/stdc++.h"
#define rep(i,a,n) for(int i = a;i < n;i++)
#define INF 1e9
#define fs first
#define sc second
typedef unsigned long long ull;
typedef long long ll;
using namespace std;

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

int main(){
    ll h,w,n,ans = 0;
    cin >> h >> w >> n;
    char cells[h][w];
    bool is_visited[h][w];
    ll x[n+1],y[n+1],cnt = 0,d[h][w];
    queue<pair<int,int> >que;
    pair<int,int>p;
    
    rep(i,0,h){
        rep(j,0,w){
            cin >> cells[i][j];
            if(cells[i][j] == 'S'){
                y[0] = i;
                x[0] = j;
            }
            else if(cells[i][j] >= '0' && cells[i][j] <= '9'){
                y[cells[i][j]-'0'] = i;
                x[cells[i][j]-'0'] = j;
            }
        }
    }
    
    rep(i,0,n){
        rep(j,0,h){
            rep(k,0,w){
                is_visited[j][k] = false;
                d[j][k] = INF;
            }
        }
        
        
        d[y[i]][x[i]] = 0;
        p.fs = y[i];
        p.sc = x[i];
        que.push(p);
        
        while(!que.empty()){
            p = que.front();
            que.pop();
            //cout << p.fs << " " << p.sc << endl;
            if(p.fs == y[i+1] && p.sc == x[i+1]){
                ans += d[y[i+1]][x[i+1]];
              	//cout << i << endl;
            }
            
            rep(j,0,4){
              	//cout << p.fs+dy[j] << " " << p.sc+dx[j] << " " << y[i+1] << " " << x[i+1] << endl;
                if(p.fs+dy[j] >= 0 && p.fs+dy[j] <= h-1 && p.sc+dx[j] >= 0 && p.sc+dx[j] <= w-1){
                    if(cells[p.fs+dy[j]][p.sc+dx[j]] == '.' || cells[p.fs+dy[j]][p.sc+dx[j]] == 'S' ||(cells[p.fs+dy[j]][p.sc+dx[j]] >= '0' &&  cells[p.fs+dy[j]][p.sc+dx[j]] <= '9')){
                        if(!is_visited[p.fs+dy[j]][p.sc+dx[j]]){
                            d[p.fs+dy[j]][p.sc+dx[j]] = min(d[p.fs][p.sc]+1,d[p.fs+dy[j]][p.sc+dx[j]]);
                            is_visited[p.fs+dy[j]][p.sc+dx[j]] = true;
                            p.fs += dy[j];
                            p.sc += dx[j];
                            que.push(p);
                            p.fs -= dy[j];
                            p.sc -= dx[j];
                          	//cout << p.fs+dy[j] << " " << p.sc+dx[j] << " " <<  d[p.fs+dy[j]][p.sc+dx[j]] << endl;
                        }
                        
                    }
                }
            }
            
        }
        //cout << d[y[i+1]][x[i+1]] << endl;
    }
  
  	cout << ans << endl;
  
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
int w, h, n;
char map[1024][1024];
// 各点到当前工厂的距?
int d[1024][1024];
const int direction[4][2] = {
	{ -1, 0 },
	{ 1, 0 },
	{ 0, -1 },
	{ 0, 1 },
};
 
int factory[16][2];
typedef pair<int, int> P;

int bfs(const int& sx, const int& sy, const int& gx, const int& gy)
{
	//memset(d, -1, sizeof(d));
	for (int i = 0; i < h; ++i)
	{
		for (int j = 0; j < w; ++j)
		{
			d[j][i] = -1;
		}
	}
	queue<P> que;
	que.push(P(sx, sy));
	d[sx][sy] = 0;
	while (que.size())
	{
		const 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 + direction[i][0];
			int ny = p.second + direction[i][1];
			// 是否可以移?，并且?点没有???
			if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx][ny] != 'X' && d[nx][ny] == -1)
			{
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}

	return d[gx][gy];
}
 
///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
 
	cin >> h >> w >> n;
	for (int i = 0; i < h; ++i)
	{
		for (int j = 0; j < w; ++j)
		{
			cin >> map[j][i];
		}
	}
 
	for (int i = 0; i < h; ++i)
	{		for (int j = 0; j < w; ++j)
		{
			if (map[j][i] == 'S')
		{
				factory[0][0] = j;
				factory[0][1] = i;
				map[j][i] = '.';
			}
			else if (isdigit(map[j][i]))
			{
				int index = map[j][i] - '0';
				factory[index][0] = j;
				factory[index][1] = i;
			}
		}
	}
 
	int step = 0;
	for (int i = 0; i < n; ++i)
	{
		// 按?序吃遍中?
		step += bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);
	}
	
	cout << step << endl;
 
	return 0;
}

C++: #include<iostream>
#include<limits.h>
#include<queue>
using namespace std;
char cheese[1001][1001];
int dx[4] = {-1,0,0,1};
int dy[4] = {0,1,-1,0};
int H, W, N,sx,sy;
int d[1001][1001]; //各个位置到指定地点最短距&#31163; 
int bfs(int sx, int sy, int ex,int ey){
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W; ++j){
			d[i][j] = INT_MAX;
		}
	}
	typedef pair<int, int>P;
	queue<P> que;
	que.push(P(sx,sy));
	d[sx][sy] = 0;
	while(!que.empty()){
		P p = que.front();
		que.pop();
		if(p.first == ex && p.second == ey){
			break;
		}
		for(int i = 0; i < 4; ++i){
			int x = p.first + dx[i], y = p.second + dy[i];
			if(x >= 0 && x <= H && y >= 0 && y < W && cheese[x][y] != 'X' && d[x][y] == INT_MAX){
				que.push(P(x,y));
				d[x][y] = d[p.first][p.second] + 1;
			}
		}
	}
	return d[ex][ey];
}
int main(){
	int result = 0;
	cin >> H >> W >> N;
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W; ++j){
			cin >> cheese[i][j];
			if(cheese[i][j] == 'S'){
				sx = i;
				sy = j;
			}
		}
	}
	// result += bfs(sx,sy,'1');
	for(int k = 1; k <= N; ++k){
		for(int i = 0; i < H;++i){
			for(int j = 0; j < W; ++j){
				if(cheese[i][j] == ('0' + k) ){
					int ex = i;
					int ey = j;
					result += bfs(sx,sy,ex,ey);
					sx = ex;
					sy = ey;					
				}
			}
		}
	}
	cout << result << endl;
}