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 <queue> 

using namespace std;

#define EMPTY '.'
#define OBSTACLE 'X'
#define INF 0x7fffffff

typedef pair<int, int> Coor;

int h, w, n;
char map[1000][1000];
int cost[1000][1000];

int bfs(Coor start, Coor end) {
    queue<Coor> q;

    fill((int*)cost, (int*)(((char*)cost)+sizeof(cost)), INF);

    cost[start.second][start.first] = 0;
    q.push(start);
    while (!q.empty()) {
        Coor c = q.front();q.pop();
        int x = c.first;
        int y = c.second;
        int dx[] = {-1,  0, 1, 0};
        int dy[] = { 0, -1, 0, 1};

        for (int i=0; i<4; i++) {
            int x2 = x + dx[i];
            int y2 = y + dy[i];

            if (x2 < 0 || w <= x2 || y2 < 0 || h <= y2) continue;
            if (map[y2][x2] == 'X') continue;
            if (cost[y2][x2] != INF) continue;

            cost[y2][x2] = cost[y][x] + 1;
            if (make_pair(x2, y2) == end) return cost[y2][x2];
            q.push(make_pair(x2, y2));
        }
    }
}

int main() {
    Coor start[10];
    unsigned int sum = 0;
 
    scanf("%d %d %d", &h, &w, &n);
    for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
            char c;
 
            scanf(" %c", &c);
            if (c == 'S') {
                start[0].first = j;
                start[0].second = i;
            } else if (c != 'X' && c != '.') {
                int ind = c - '0';
                start[ind].first = j;
                start[ind].second = i;
            }

            map[i][j] = c;
        }
    }

    for (int i=0; i<n; i++) {
        sum += (unsigned int)bfs(start[i], start[i+1]);
    }            

    printf("%u\n", sum);
}

C++: #include <cstdio>
#include <map>
#include <queue>
#include <string>

using namespace std;

const int INF = 100000000;
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 9;

int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W]; // ?????????????????\??´?????§??????????????°, 1~N??§?????????????????§????????°??????+1
int sx, sy, gx, gy;
int power = 1; // ??????????????????
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

typedef pair<int, int> P;

void reset_d() {
  for (int i = 0; i < H; i++)
    for(int j = 0; j < W; j++) d[i][j] = INF;
}

int bfs(int c) {
  queue<P> que;
  reset_d();
  d[sx][sy] = c;
  que.push(P(sx, sy));
  while(que.size()) {
    P p = que.front(); que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if ( 0 <= nx && nx <= H && 0 <= ny && ny <= W && field[nx][ny] != 'X' && d[nx][ny] == INF) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
        if (field[nx][ny] - '0' == power) {
          sx = nx;
          sy = ny;
          power++;
          return d[nx][ny];
        }
      }
    }
  }
  return INF;
}

void solve() {
  int c = 0;
  for (int i = 0; i < N; i++) {
    c = bfs(c);
  }
  printf("%d\n", c);
}


int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", field[i]);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (field[i][j] == 'G') {
        gx = i;
        gy = j;
      }
    }
  }
  solve();
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int64;

int h, w, n, x[11], y[11];
string buff = "S123456789";

char mas[1000][1000];

int bfs(){
  int dy[] = { 0, 1, 0, -1}, dx[] = { 1, 0, -1, 0};
  int min_cost[1000][1000];
  deque< int > quex, quey;

  int ret = 0;
  for(int i = 0; i < n; i++){
    fill_n( *min_cost, 1000 * 1000, 1 << 30);
    quey.push_back(y[i]), quex.push_back(x[i]);
    min_cost[y[i]][x[i]] = 0;
    while(!quey.empty()){
      int py = quey.front(), px = quex.front();
      quey.pop_front(), quex.pop_front();
      if(py == y[i + 1] && px == x[i + 1]){
        ret += min_cost[py][px];
        break;
      }
      for(int i = 0; i < 4; i++){
        int ny = py + dy[i], nx = px + dx[i];
        if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
        if(mas[ny][nx] == 'X') continue;
        if(min_cost[py][px] + 1 < min_cost[ny][nx]){
          min_cost[ny][nx] = min_cost[py][px] + 1;
          quey.push_back(ny), quex.push_back(nx);
        }
      }
    }
    quey.clear(), quex.clear();
  }
  return ret;
}

int main()
{
  cin >> h >> w >> n;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cin >> mas[i][j];
      if(buff.find(mas[i][j]) != string::npos){
        y[buff.find(mas[i][j])] = i;
        x[buff.find(mas[i][j])] = j;
      }
    }
  }
  cout << bfs() << endl;
}