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<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int h, w, n;

bool CanMove(int y, int x, vector< vector<char> > m){
    if(y < 0) return false;
    if(x < 0) return false;
    if(y >= h) return false;
    if(x >= w) return false;
    if(m[y][x] == 'X') return false;
    return true;
}

class C{
    public:
        int y, x, step;
        C(int _y, int _x, int _step);
};

C::C(int _y, int _x, int _step){
    y = _y;
    x = _x;
    step = _step;
}

int bfs(int gy, int gx, int y, int x, vector< vector<char> > m){
    queue<C> open;
    open.push( C(y, x, 0) );
    vector< vector<int> > closed(m.size(), vector<int>(m[0].size()));
    REP(i, m.size()) REP(j, m[0].size()) closed[i][j] = INF;
    closed[y][x] = 0;
    int my[] = {0, 0, 1, -1};
    int mx[] = {1, -1, 0, 0};
    int ret;

    while(!open.empty()){
        C now = open.front();
        open.pop();
        if(now.y == gy && now.x == gx){
            ret = now.step;
            break;
        }

        REP(i, 4){
            int ny = now.y + my[i], nx = now.x + mx[i];
            if(CanMove(ny, nx, m) && closed[ny][nx] > now.step + 1){
                closed[ny][nx] = now.step + 1;
                open.push( C(ny, nx, now.step + 1) );
            }
        }
    }
    return ret;
}

int main(){
    cin >>h >>w >>n;
    vector< vector<char> > m(h, vector<char>(w) );
    vector<int> y(n + 2), x(n + 2);

    //start of input
    REP(i, h){
        REP(j, w){
            cin >>m[i][j];
            if(m[i][j] == 'S'){
                y[0] = i;
                x[0] = j;
            }
            if(m[i][j] - '0' > 0 && m[i][j] - '0' <= n){
                int num = m[i][j] - '0';
                y[num] = i;
                x[num] = j;
            }
        }
    }
    //end of input

    int ans = 0;
    REP(i, n){
        ans += bfs(y[i + 1], x[i + 1], y[i], x[i], m);
    }
    cout <<ans <<endl;
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

int H, M, N;

int go[4][2];

bool K[1002][1002];

int score[1002][1002];

int C[11][2];

int ans = 0;

void saiki(int sx, int sy, int s){
  if(K[sx][sy]){
    if(score[sx][sy] > s){
      score[sx][sy] = s;
      for(int i = 0; i < 4; i++){
        saiki(sx + go[i][0], sy + go[i][1], s + 1);
      }
    }
  }
}

int main(){
  
  go[0][0] = 1;
  go[0][1] = 0;
  go[1][0] = 0;
  go[1][1] = -1;
  go[2][0] = -1;
  go[2][1] = 0;
  go[3][0] = 0;
  go[3][1] = 1;
  
  for(int i = 0; i <= H + 1; i++){
    for(int j = 0; j <= M + 1; j++){
      score[i][j] = 2147483640;
      K[i][j] = false;
    }
  }
  
  scanf("%d%d%d", &H, &M, &N);
  
  for(int i = 0; i < H; i++){
    char str[M + 1];
    scanf("%s", str);
    for(int j = 0; j < M; j++){
      if(str[j] == '.'){
        K[i + 1][j + 1] = true;
      }else if(str[j] == 'X'){
        K[i + 1][j + 1] = false;
      }else if(str[j] == 'S'){
        C[0][0] = i + 1;
        C[0][1] = j + 1;
        K[i + 1][j + 1] = true;
      }else{
        C[str[j] - '0'][0] = i + 1;
        C[str[j] - '0'][1] = j + 1;
        K[i + 1][j + 1] = true;
      }
    }
  }
  
  for(int i = 0; i < N; i++){
    for(int j = 1; j <= H; j++){
      for(int k = 1; k <= M; k++){
        if(K[j][k]){
          score[j][k] = 2147483640;
        }else{
          score[j][k] = -2147483640;
        }
      }
    }
    saiki(C[i + 1][0], C[i + 1][1], 0);
    int aa = score[C[i][0]][C[i][1]];
    ans += aa;
  }
  
  printf("%d\n", ans);
  
  return 0;
}