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 <vector>
#include <queue>

using namespace std;

vector < vector<int> > hyou(1002, vector<int>(1002, -1));
int dh[] = {0, 1, 0, -1};
int dw[] = {1, 0, -1, 0};

typedef pair<int, int> P;
typedef pair<int, P> Q;

int search(int h, int w, int n) {
	int counter = 0;
	queue<Q> que;
	Q p;
	(p.second).first = h;
	(p.second).second = w;
	p.first = 0;
	que.push(p);
	while (!que.empty()) {
		Q pp = que.front();
		que.pop();
		if (hyou[(pp.second).first][(pp.second).second] == n) {
			counter = pp.first;
			break;
		} else {
			if (hyou[(pp.second).first][(pp.second).second] == 0) {
				hyou[(pp.second).first][(pp.second).second] = -2;
			}
			for (int i = 0; i < 4; i++) {
				if (hyou[(pp.second).first+dh[i]][(pp.second).second+dw[i]] != -1 && hyou[(pp.second).first+dh[i]][(pp.second).second+dw[i]] != -2) {
					Q pq;
					(pq.second).first = (pp.second).first+dh[i];
					(pq.second).second = (pp.second).second+dw[i];
					pq.first = pp.first+1;
					que.push(pq);
				}
			}
		}
	}
	for (int i = 0; i < 1002; i++) {
		for (int j = 0; j < 1002; j++) {
			if (hyou[i][j] == -2) {
				hyou[i][j] = 0;
			}
		}
	}
	return counter;
}

int main() {
	int h, w, n;
	cin >> h >> w >> n;
	char c;
	int sh, sw;
	int data[10][2];
	for (int i = 1; i <= h; i++) {
		for (int j = 1; j <= w; j++) {
			cin >> c;
			if (c == '.') {
				hyou[i][j] = 0;
			} else if (c == 'S') {
				hyou[i][j] = 0;
				sh = i;
				sw = j;
			} else if (c == 'X') {
				hyou[i][j] = -1;
			} else {
				int d = c - '0';
				hyou[i][j] = d;
				data[d-1][0] = i;
				data[d-1][1] = j;
			}
		}
	}
	int counter = 0;
	for (int i = 0; i < n; i++) {
		counter += search(sh, sw, i+1);
		sh = data[i][0];
		sw = data[i][1];
	}
	cout << counter << endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
const int INF=10000;
typedef pair<int,int> P;
char town[1001][1001];
int h,w,n;
int s;
int d[1001][1001];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char num='1';
int sx,sy,gx,gy;
int ans=0;

int bfs(){
queue<P> que;

 for(int i=0;i<h;i++){
   for(int j=0;j<w;j++){
     d[i][j]=INF;
   }
 }
 d[sy][sx]=0;
 que.push(P(sy,sx));

 while(que.size()){
   P p=que.front(); que.pop();
   
   if(town[p.first][p.second]==num){
     sy=p.first;
     sx=p.second;
     return d[p.first][p.second];
     break;
   }
   
   for(int i=0;i<4;i++){
     int ny=p.first + dy[i],nx=p.second+dx[i];
     
     if(0<=nx && nx<w && 0<=ny && ny<h && town[ny][nx]!='X' && d[ny][nx]==INF){
       que.push(P(ny,nx));
       d[ny][nx]=d[p.first][p.second]+1;
     }
   }
 }
}


int main(){
  cin>> h>> w>> n;
  
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>> town[i][j];
      if(town[i][j]=='S'){
	sy=i;
	sx=j;
      }
    }
  }

for(num='1';num<'1'+n;num++){
  ans+=bfs();
 }
  cout<< ans<< endl;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
struct aa {
	int x;
	int y;
	int time;
};

const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };
int getdis(const int fromx, const int fromy, const int gx, const int gy,const vector<vector<int>>&field) {
	vector<vector<int>>memo(1002, vector<int>(1002, 99999999));
	memo[fromy][fromx] = 0;
	queue<aa>que;
	que.push(aa{ fromx,fromy,0 });
	int ans = 0;
	while (!que.empty()) {
		aa atop = que.front();
		que.pop();
		const int ax = atop.x;
		const int ay = atop.y;
		if (ax == gx&&ay == gy) {
			ans = atop.time;
			break;
		}
		for (int i = 0; i < 4; ++i) {
			const int nex = ax + dx[i];
			const int ney = ay + dy[i];
			if (field[ney][nex]) {

			}
			else {
				if (memo[ney][nex]>atop.time + 1) {
					memo[ney][nex] = atop.time + 1;
					que.push(aa{ nex,ney,atop.time + 1 });
				}
			}
		}
	}
	return ans;
}

int main() {
	int H, W, C; cin >> H >> W >> C;
	vector<vector<int>>field(H + 2, vector<int>(W + 2,1));
	vector<int>xs(C+1), ys(C + 1);
	int sx, sy;
	for (int i = 0; i < H; ++i) {
		string st; cin >> st;
		for (int j = 0; j < W; ++j) {
			if (st[j] == '.')field[i + 1][j + 1] = false;
			else if (st[j] == 'X') {

			}
			else {
				field[i + 1][j + 1] = false;
				if (st[j] == 'S') {
					xs[0] = j + 1;
					ys[0] = i + 1;
				}
				else {
					xs[st[j] - '0'] = j + 1;
					ys[st[j] - '0'] = i + 1;
				}
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < C; ++i) {
		ans += getdis(xs[i], ys[i], xs[i + 1], ys[i + 1], field);
	}
	cout << ans << endl;
	return 0;
}