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];

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 += 2){
    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]];
    int bb;
    if(i + 2 < N){
      bb = score[C[i + 2][0]][C[i + 2][1]];
      ans += bb;
    }
    ans += aa;
  }
  
  printf("%d\n", ans);
  
  return 0;
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <queue>

constexpr int INF = 1000000000;
using Pair = std::pair<int, int>;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

int h, w, n;
std::string map[1010];
int cost[1010][1010];
Pair pts[10];

int bfs(Pair st, Pair en) {
    for(int i = 0; i < h; ++i) {
        for(int j = 0; j < w; ++j) {
            cost[i][j] = INF;
        }
    }
    cost[st.first][st.second] = 0;

    std::queue<Pair> que;
    que.push(st);

    while(!que.empty()) {
        auto p = que.front(); que.pop();

        for(int i = 0; i < 4; ++i) {
            int py = p.first + dy[i], px = p.second + dx[i];

            if(py < 0 || px < 0 || py >= h || px >= w)
                continue;

            if(map[py][px] == '.' && cost[py][px] == INF) {
                que.push({py, px});
                cost[py][px] = cost[p.first][p.second] + 1;
            }
        }
    }

    return cost[en.first][en.second];
}

int main() {
    std::cin >> h >> w >> n;

    for(int i = 0; i < h; ++i) {
        std::cin >> map[i];

        for(int j = 0; j < w; ++j) {
            if(map[i][j] != '.' && map[i][j] != 'X') {
                int idx = (map[i][j] == 'S' ? 0 : map[i][j] - '0');

                pts[idx] = {i, j};
                map[i][j] = '.';
            }
        }
    }

    int ans = 0;
    for(int i = 1; i <= n; ++i) {
        ans += bfs(pts[i - 1], pts[i]);
    }

    std::cout << ans << std::endl;
}

C++: #include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

typedef pair<int,int> P;

int h,w,n;
char m[1000][1000];
int visited[1000][1000];

int main(){
	cin>>h>>w>>n;

	int pos;
	for(int y=0;y<h;y++){
		for(int x=0;x<w;x++){
			cin>>m[x][y];
			if(m[x][y]=='S') pos=x+y*1000;
		}
	}

	memset(visited,0,sizeof(visited));
	int ans=0;
	for(int now=1;now<=n;now++){
		queue<P> q;
		q.push(make_pair(pos,0));
		while(1){
			pos=q.front().first;
			int x=pos%1000;
			int y=pos/1000;
			int cost=q.front().second;
			q.pop();
			if(visited[x][y]==now || m[x][y]=='X') continue;
			if(m[x][y]==('0'+now)){
				ans+=cost;
				break;
			}
			visited[x][y]=now;
			if(0<x) q.push(make_pair(pos-1,cost+1));
			if(x+1<w) q.push(make_pair(pos+1,cost+1));
			if(0<y) q.push(make_pair(pos-1000,cost+1));
			if(y+1<h) q.push(make_pair(pos+1000,cost+1));
		}
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<iostream>
#include<sstream>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main(){
    int h,w,n;
    scanf("%d%d%d",&h,&w,&n);
    vector<vector<char> > kmap(h,vector<char>(w));
    vector<P> ks(n+1);
    for(int i=0;i<h;i++){
        char ins[1100];
        scanf("%s\n",ins);
        for(int j=0;j<w;j++){
            kmap[i][j]=ins[j];
            int at=kmap[i][j]-'0';
            if(1<=at&&at<=n)ks[at]=make_pair(i,j);
            if(kmap[i][j]=='S')ks[0]=make_pair(i,j);
        }
    }
    int sum=0;
    int ve[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    for(int ix=0;ix<n;ix++){
        P start=ks[ix],goal=ks[ix+1];
        vector<vector<bool> > vised(h,vector<bool>(w,false));
        queue<P> now;
        now.push(start);
        vised[start.first][start.second]=true;
        for(int i=0;;i++){
            int size=now.size();
            for(int j=0;j<size;j++){
                P te=now.front();
                now.pop();
                int nx=te.first,ny=te.second;
                if(te==goal){
                    sum+=i;
                    goto QUIT;
                }
                for(int v=0;v<4;v++){
                    int ex=nx+ve[v][0],ey=ny+ve[v][1];
                    if(0<=ex&&ex<h&&0<=ey&&ey<w&&kmap[ex][ey]!='X'&&!vised[ex][ey]){
                        now.push(make_pair(ex,ey));
                        vised[ex][ey]=true;
                    }
                }
            }
        }
QUIT:   ;
    }
    printf("%d\n",sum);
}