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 <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;

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

  vector<string> s(h);
  map<char, pii> mp;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        s[i][j] = '0';
      }
      if (isdigit(s[i][j])) {
        mp[s[i][j]] = pii(i, j);
      }
    }
  }

  int ans = 0;
  int di[] = {-1, 0, 0, 1}, dj[] = {0, -1, 1, 0};
  for (char c = '0'; c < n + '0'; c++) {
    bool used[1010][1010] = {};
    queue< pair<pii, int> > que;
    used[mp[c].first][mp[c].second] = true;
    que.push(make_pair(mp[c], 0));

    while (!que.empty()) {
      auto v = que.front(); que.pop();
      if (v.first == mp[c + 1]) {
        ans += v.second; break;
      }
      for (int i = 0; i < 4; i++) {
        int ni = v.first.first + di[i], nj = v.first.second + dj[i];
        if (ni >= 0 && ni < h && nj >= 0 && nj < w && !used[ni][nj] && s[ni][nj] != 'X') {
          used[ni][nj] = true;
          que.push(make_pair(pii(ni, nj), v.second + 1));
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<string>
#include<utility>
#include<algorithm>
#include<stack>
#include<queue>

#define numof(array) (sizeof (array) / sizeof *(array))
using namespace std;

#define INF (1 << 29)
static const int dx[] = {1, 0, -1, 0};
static const int dy[] = {0, 1, 0, -1};

#define MAX_H 1000
#define MAX_W 1000

int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];//スタートからの距離


typedef pair<int, int> P;

int bfs(const P start, const P goal)
{
    queue<P> que;
    
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            d[i][j] = INF;
        }
    }

    que.push(start);
    d[start.second][start.first] = 0;


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

    
int main(void)
{
    cin  >> H >> W >> N;

    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            field[y][x] = 'X';
        }
    }

    P zahyo[10];//スタートと工場1,2,3...の座標
    for (int i = 0; i < 10; i++) {
        zahyo[i] = P(-1, -1);
    }


    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            cin >> field[y][x];
            
            //スタートと工場を見つけておく
            if ('0' < field[y][x] && field[y][x] <= '9') {
                zahyo[field[y][x] - '0'] = P(x, y);
            }
            if (field[y][x] == 'S') {
                zahyo[0] = P(x, y);
            }
            
        }
    }
        
    int cnt = 0;
    int answer = 0;
    while (cnt < N) {//-1でない間
        answer += bfs(zahyo[cnt], zahyo[cnt+1]);
        cnt++;
    }

    cout << answer << endl;
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int xs[10],ys[10];
int H,W,N;
string S[1002];
int main()
{
	scanf("%d%d%d",&H,&W,&N);
	for(int i=0;i<W+2;i++){
		S[0]+='X';
		S[H+1]+='X';
	}
	for(int i=1;i<=H;i++){
		string x;
		cin >> x;
		S[i]+='X'+x+'X';
	}
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++){
			if(S[i][j]!='X'&&S[i][j]!='.'){
				if(S[i][j]=='S'){
					xs[0]=j;
					ys[0]=i;
				}
				else{
					xs[S[i][j]-'0']=j;
					ys[S[i][j]-'0']=i;
				}
			}
		}
	}
	int XX[1002][1002];
	int ans=0;
	vector<pair<int,int> >vec1;
	vector<pair<int,int> >vec2;
	int dx[4]={-1,0,0,1};
	int dy[4]={0,-1,1,0};
	for(int x=0;x<N;x++){
		vec1.clear();
		vec2.clear();
		for(int i=0;i<H+2;i++)for(int j=0;j<W+2;j++)XX[i][j]=-1;
		int cou=0;
		XX[ys[x]][xs[x]]=cou;
		vec1.push_back(make_pair(ys[x],xs[x]));
		while(XX[ys[x+1]][xs[x+1]]==-1){
			vec2.clear();
			for(int i=0;i<vec1.size();i++){
				for(int j=0;j<4;j++){
					if(S[vec1[i].first+dy[j]][vec1[i].second+dx[j]]=='X')continue;
					if(XX[vec1[i].first+dy[j]][vec1[i].second+dx[j]]!=-1)continue;
					vec2.push_back(make_pair(vec1[i].first+dy[j],vec1[i].second+dx[j]));
				}
			}
			vec1.clear();
			for(int i=0;i<vec2.size();i++)vec1.push_back(vec2[i]);
			cou++;
			for(int i=0;i<vec1.size();i++){
				XX[vec1[i].first][vec1[i].second]=cou;
			}
		}
		ans+=XX[ys[x+1]][xs[x+1]];
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <climits>
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;

typedef pair<int,int> pii;

int main()
{
  int H, W, N;
  const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  cin >> H >> W >> N;
  char map[H][W];
  int d[N][H][W], hp = 1, ans = 0;
  fill(d[0][0],d[N][0],INT_MAX);
  queue<pii> q;
  REP(i,H){
    REP(j,W){
      cin >> map[i][j];
      if(map[i][j] == 'S'){
        q.push({i,j});
        d[hp-1][i][j] = 0;
      }
    }
  }
  while(1){
    pii p = q.front();
    q.pop();
    if(hp == map[p.first][p.second]-'0'){
      ans += d[hp-1][p.first][p.second];
      hp++;
      d[hp-1][p.first][p.second] = 0;
      while(!q.empty()) q.pop();
    }
    if(hp == N+1){
      cout << ans << endl;
      return 0;
    }
    REP(i,4){
      pii next_p = {p.first+dx[i],p.second+dy[i]};
      if(next_p.first < 0 || next_p.first >= H || next_p.second < 0 || next_p.second >= W || map[next_p.first][next_p.second] == 'X') continue;
      if(d[hp-1][next_p.first][next_p.second] > d[hp-1][p.first][p.second]+1){
        d[hp-1][next_p.first][next_p.second] = d[hp-1][p.first][p.second]+1;
        q.push(next_p);
      }
    }
  }
}