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>
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;
 
 
 
//************************************
// Method:    bfs
// FullName:  bfs
// Access:    public 
// Returns:   int
// Qualifier:
// Parameter: const int & sx 起点x
// Parameter: const int & sy 起点y
// Parameter: const int & gx 终点x
// Parameter: const int & gy 终点y
//************************************
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;
}
///////////////////////////End Sub//////////////////////////////////

C++: #include<stdio.h>
#include<stack>
#include<iostream>
using namespace std;
int main(void)
{
	char p[2];
	stack<pair<int,int> > q;
	pair<int,int> g,s,w;
	int x,y,n,m,a=0,f[1002][1002],l=1,l2;
	scanf("%d %d %d",&x,&y,&l2);
	for(n=0;n<=1002;n++){
		for(m=0;m<=1002;m++){
			if(n==0||n>x||m==0||m>y){
				f[n][m]=10;
			}
			else{
				scanf("%c",&p);
				if(p[1]=='S'){
					f[n][m]=0;
					s.first=n;
					s.second=m;
					q.push(s);
				}
				else if(p[1]=='X'){
					f[n][m]=10;
				}
				else if(p[1]=='.'){
					f[n][m]=0;
				}
				else{
					f[n][m]=p[1];
				}
			}
		}
	}
	while(!q.empty()){
		s=q.top();
		q.pop();
		if(f[s.first+1][s.second]<=l){
			w.first=s.first+1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second+1]<=l){
			w.first=s.first;
			w.second=s.second+1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first-1][s.second]<=l){
			w.first=s.first-1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second-1<=l]){
			w.first=s.first;
			w.second=s.second-1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}
	}
	printf("%d\n",a);
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
 
using namespace std;
 
#define P(p) cout<<(p)<<endl
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return
#define C(i) cin>>i
#define C2(i,j) cin>>i>>j
#define C3(i,j,k) cin>>i>>j>>k
#define C4(i,j,k,m) cin>>i>>j>>k>>m
////////////////////////////////////////////////////////////

struct S{
  int num;
  int x;
  int y;
};

int main(){
  int h,w,n;
  C3(h,w,n);
  
  int f[h][w] = {};
  pair<int,int> g[n+1];
  pair<int,int> s[n+1];
  rep(i,0,h){
    rep(j,0,w){
      char c;
      cin >> c;
      if(c == 'X'){
        f[i][j] = -1;
      }
      else if(c == '.'){
        f[i][j] = 0;
      }
      else if(c == 'S'){
        pair<int,int> tt = {j,i};
        s[1] = tt;
      }
      else{
        int t = c - '0';
        f[i][j] = t;
        pair<int,int> tt = {j,i};
        g[t] = tt;
        
        if(t < n){
          s[t+1] = tt;
        }

      }
    }
  }

  int ans = 0;
  rep(i,1,n+1){
    int sx = s[i].first;
    int sy = s[i].second;
    int gx = g[i].first;
    int gy = g[i].second;
    //printf("sx:%d sy:%d gx:%d gy:%d\n",sx,sy,gx,gy);
    queue<struct S> q;
    struct S tmp = {0,sx,sy};
    q.push(tmp);
    
    int fNum[h][w];
    rep(i,0,h)
      rep(j,0,w)
      fNum[i][j] = 0xFFFF;
    

    while(!q.empty()){
      struct S s = q.front();
      //printf("i:%d n:%d x:%d y:%d\n",i,s.num,s.x,s.y);
      q.pop();
      if(f[s.y][s.x] == -1){
        continue;
      }
      if(s.num >= fNum[s.y][s.x] && s.num != 0)
        continue;

      fNum[s.y][s.x] = s.num;
      if(s.y == gy && s.x == gx)
        continue;

      else{
        if(s.y > 0){
          if(f[s.y-1][s.x] != -1&& fNum[s.y-1][s.x] > s.num+1){
            struct S t = {s.num+1,s.x,s.y-1};
            q.push(t);
          }
        }
        if(s.x + 1 <= w - 1 ){
          if(f[s.y][s.x+1] != -1 && fNum[s.y][s.x+1] > s.num+1){
            struct S r = {s.num+1,s.x+1,s.y};
            q.push(r);
          }
        }
        if(s.x > 0){
          if(f[s.y][s.x-1] != -1 && fNum[s.y][s.x-1] > s.num+1){
            struct S l = {s.num+1,s.x-1,s.y};
            q.push(l);
          }
        }
        if(s.y + 1 <= h -1 ){
          if(f[s.y+1][s.x] != -1 && fNum[s.y+1][s.x] > s.num+1){
            struct S b = {s.num+1,s.x,s.y+1};     
            q.push(b);
          }
        }
      }
    }

    ans += fNum[gy][gx];
  }

  P(ans);

  ret 0;
}