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 <cstdio>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

int H, M, N;

int go[4][2];

bool K[1002][1002];

bool flg[1002][1002];
int score[1002][1002];

int C[11][2];

int ans = 0;

//int saiki(int sx, int sy, int gx, int gy){
//  if(!flg[sx][sy]){
//    return score[sx][sy];
//  }
//  if(sx == gx){
//    if(sy == gy){
//      return 0;
//    }
//  }
//  int ret;
//  if(flg[sx][sy + 1]){
//    ret = saiki(sx, sy + 1, gx, gy);
//  }else{
//    ret = 2147483600;
//  }
//  for(int i = 0; i < 3; i++){
//    if(flg[sx + go[i][0]][sy + go[i][1]]){
//      int a = saiki(sx + go[i][0], sy + go[i][1], gx, gy);
//      if(ret > a){
//        ret = a;
//      }
//    }
//  }
//  return ret + 1;
//}

void saiki(int sx, int sy, int s){
  if(flg[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;
      flg[i][j] = false;
      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++){
        flg[j][k] = K[j][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;
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
typedef pair<P,P> P2;

static const double eps = 1e-8;

const int tx[4] = {-1,0,1,0};
const int ty[4] = {0,1,0,-1};

char stage[1001][1001];
int gCost[1001][1001];

int main(){
	int W,H,N;
	while(~scanf("%d %d %d",&H,&W,&N)){
		for(int y=0;y<H;y++){
			char buf[1024];
			scanf("%s",buf);
			for(int x=0;x<W;x++){
				stage[y][x] = buf[x];
			}
		}

		vector<P> pos;
		for(int i=0;i<=N;i++){				
			for(int y=0;y<H;y++){
				for(int x=0;x<W;x++){
					if(i==0){
						if(stage[y][x] == 'S'){
							pos.push_back(P(x,y));
							goto next;
						}
					}
					else{
						if(stage[y][x] == '0' + i){
							pos.push_back(P(x,y));
							goto next;
						}
					}
				}
			}
next:;
		}

		priority_queue<P2,vector<P2>,greater<P2> > que; 
		for(int i=0;i+1<pos.size();i++){
			que.push(P2(P(pos[i].first,pos[i].second),P(pos[i+1].first,pos[i+1].second)));
		}

		int res = 0;
		while(!que.empty()){
			int sx = que.top().first.first;
			int sy = que.top().first.second;
			int gx = que.top().second.first;
			int gy = que.top().second.second;

			que.pop();

			queue<PP> que2;
			que2.push(PP(0,P(sx,sy)));

			memset(gCost,0x3,sizeof(gCost));
			gCost[sy][sx] = 0;
			while(!que2.empty()){
				int cost = que2.front().first;
				int x = que2.front().second.first;
				int y = que2.front().second.second;

				que2.pop();
				
				for(int i=0;i<4;i++){
					int dx = x + tx[i];
					int dy = y + ty[i];
					if(dx == gx && dy == gy) {
						res += cost + 1;
						while(!que2.empty()) que2.pop();
						break;
					}
					if(dx < 0 || dx >= W || dy < 0 || dy >= H) continue;
					if(stage[dy][dx] == 'X') continue;
					if(gCost[dy][dx] <= cost + 1) continue;
					gCost[dy][dx] = cost + 1;
					que2.push(PP(cost+1,P(dx,dy)));
				}
			}
		}

		printf("%d\n",res);
	}
}