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>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 1005

struct LOC{

	void set(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	int row,col;
};

struct Info{
	Info(int arg_row,int arg_col,int arg_sum_dist){
		row = arg_row;
		col = arg_col;
		sum_dist = arg_sum_dist;
	}
	int row,col,sum_dist;
};

int N;
int H,W;
int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};
int min_dist[SIZE][SIZE];
char base_map[SIZE][SIZE];
LOC loc[10];


bool rangeCheck(int row,int col){

	return row >= 0 && row <= H-1 && col >= 0 && col <= W-1;
}

int calc(int start_index){

	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){

			min_dist[row][col] = BIG_NUM;
		}
	}

	queue<Info> Q;
	min_dist[loc[start_index].row][loc[start_index].col] = 0;

	Q.push(Info(loc[start_index].row,loc[start_index].col,0));

	while(!Q.empty()){

		for(int i = 0; i < 4; i++){
			int adj_row = Q.front().row+diff_row[i];
			int adj_col = Q.front().col+diff_col[i];

			if(rangeCheck(adj_row,adj_col) == false || base_map[adj_row][adj_col] == 'X' ||
					min_dist[adj_row][adj_col] <= Q.front().sum_dist+1)continue;


			min_dist[adj_row][adj_col] = Q.front().sum_dist+1;

			//辺コスト1のQueue処理なので、最初に到達した状態が最小コストのはず
			if(adj_row == loc[start_index+1].row && adj_col == loc[start_index+1].col){

				return min_dist[adj_row][adj_col];
			}
			Q.push(Info(adj_row,adj_col,min_dist[adj_row][adj_col]));
		}
		Q.pop();
	}

	return -1;
}

int main(){

	scanf("%d %d %d",&H,&W,&N);

	for(int row = 0; row < H; row++){

		scanf("%s",base_map[row]);
		for(int col = 0; col < W; col++){
			if(base_map[row][col] == 'S'){

				loc[0].set(row,col);

			}else if(base_map[row][col] >= '1' && base_map[row][col] <= '9'){

				loc[base_map[row][col]-'0'].set(row,col);
			}
		}
	}

	int ans = 0;

	for(int i = 0; i <= N-1; i++){

		ans += calc(i);
	}

	printf("%d\n",ans);

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <queue>

using namespace std;

typedef pair <int, int> point;
typedef pair < point, int> kotori;
// 2130
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

bool vst[1280][1280];
string field[1280];
int h, w, n;

void vst_clear(void)
{
	for (int i = 0; i < 1280; i++){
		for (int j = 0; j < 1280; j++){
			vst[i][j] = false;
		}
	}
}

bool is_region(int y, int x)
{
	return (0 <= y) && (y < h) && (0 <= x) && (x < w);
}

int main(void)
{
	//ios::sync_with_stdio(0); cin.tie(0);

	point start, goal;
	point f[10];

	cin >> h >> w >> n;
	for (int i = 0; i < h; i++){
		cin >> field[i]; cin.ignore();
		for (int j = 0; j < w; j++){
			if (field[i][j] == 'S'){
				field[i][j] = '0';
			}
			if (isdigit(field[i][j])){
				f[field[i][j] - '0'] = make_pair(i, j);
			}
		}
	}

	int ret = 0;
	for (int g = 1; g <= n; g++){
		// cout << g-1 << " done" << endl;
		vst_clear();
		start = f[g - 1];
		goal = f[g];

		queue < kotori > q;
		kotori a;
		a.first = start;
		a.second = 0;
		q.push(a);
		// point, step;
		while (!q.empty()){
			kotori hanayo = q.front(); q.pop();
			int x = hanayo.first.second, y = hanayo.first.first;
			// cout << y << " " << x << endl;
			if (vst[y][x]) continue;
			vst[y][x] = true;
			if (hanayo.first == goal){
				ret += hanayo.second;
				break;
			}
			for (int i = 0; i < 4; i++){
				int ny = hanayo.first.first + dy[i], nx = hanayo.first.second + dx[i];
				if (!is_region(ny, nx)) continue;
				if (vst[ny][nx]) continue;
				if (field[ny][nx] == 'X') continue;
				kotori p; p.first = make_pair(ny, nx); p.second = hanayo.second + 1;
				q.push(p);
			}
		}
	}
	cout << ret << endl;

	return 0;
}