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 <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };


int main(void)
{
	ios::sync_with_stdio(false);
	int h, w, n;
	while(cin >> h >> w >> n && n)
	{
		SArray stage(h);
		PArray cheese(n + 1);
		for (int i = 0; i < h; i++)
		{
			cin >> stage[i];
			for (int j = 0; j < w; j++)
			{
				char c = stage[i][j];
				if(c == 'S')
				{
					cheese[0] = P(i, j);
				}
				if(inRange(10, c - '0'))
				{
					cheese[c - '0'] = P(i, j);
				}
			}
		}

		int ans = 0;
		vector<vector<bool>> visited(h, vector<bool>(w));
		for (int i = 0; i < n; i++)
		{
			P start = cheese[i];
			P goal = cheese[i + 1];

			for (int i = 0; i < h; i++)
			{
				for (int j = 0; j < w; j++)
				{
					visited[i][j] = false;
				}
			}

			queue<IP> que;
			que.push(IP(0, start));
			while (!que.empty())
			{
				IP ip = que.front(); que.pop();
				P p = ip.second;
				visited[p.first][p.second] = true;

				if(p == goal)
				{
					ans += ip.first;
					break;
				}

				for (int j = 0; j < 4; j++)
				{
					int nr = p.first + dr[j], nc = p.second + dc[j];
					if(!inRange(h, nr) || !inRange(w, nc) || stage[nr][nc] == 'X' || visited[nr][nc]) continue;
					visited[nr][nc] = true;
					que.push(IP(ip.first + 1, P(nr, nc)));
				}
			}
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<queue>
/*
#include<windows.h>
*/
using namespace std;

struct DATA{
	int y;
	int x;
	int n;
};


bool  vi[1001][1001];
char cp[1001][1001];  
DATA Start,Goal;
int H,W,N;
/*
void pa(){
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(vi[i][j])printf("¡");
			else printf(" ");
		}puts("");
	}puts("");
}
*/
int BFS(){
	queue<DATA> Q;
	Q.push(Start);
	
	while(!Q.empty()){
//		pa();
//		system("cls");
		DATA p = Q.front();
		Q.pop();
//		printf("%d (%4d,%4d) %d\n", Q.size(), p.x, p.y, p.n);
		const int dy[] = {1,0,-1,0};
		const int dx[] = {0,1,0,-1};
		
		if(vi[p.y][p.x])continue;
		vi[p.y][p.x] = true;
		if(p.y == Goal.y && p.x == Goal.x)return p.n;
		
		for(int i = 0; i < 4; i++){
			if(p.x+dx[i] < 0 || p.x+dx[i] >= W || p.y+dy[i] < 0 || p.y+dy[i] >= H || cp[p.y][p.x] == 'X' && !vi[p.y+dy[i]][p.x+dx[i]])continue;
			DATA NEXT = {p.y+dy[i],p.x+dx[i],p.n+1};
			Q.push( NEXT );
		}
	}
	return -1;
}

int main(void){
	
	while(scanf("%d %d %d", &H, &W, &N) != EOF ){
		
		char next = '1';
		int ans = 0;
		for(int i = 0; i < H; i++)scanf("%s",cp[i]);
		
		while(next-'1' != N){
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					vi[i][j] = false;
				}
			}
			
			if(next == '1'){
				for(int i = 0; i < H; i++){
					for(int j= 0; j < W; j++){
						if(cp[i][j] == 'S'){
							Start.x = j;
							Start.y = i;
							Start.n = 0;
							goto A;
						}
					}
				}
			}else {
				for(int i = 0; i < H; i++){
					for(int j = 0; j < W; j++){
						if(cp[i][j] == next-1){
							Start.x = j;
							Start.y = i;
							Start.n = 0;
							goto A;
						}
					}
				}
			}
			A:
			
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					if(cp[i][j] == next){
						Goal.x = j;
						Goal.y = i;
						goto B;
					}
				}
			}
			B:
//			printf("S = (%d %d)\n", Start.x, Start.y);
//			printf("G = (%d %d)\n", Goal.x, Goal.y);
			ans += BFS();
			next++;
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<queue>
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 X[1002][1002];
	int ans=0;
	queue< pair<int,int> >que;
	int dx[4]={-1,0,0,1};
	int dy[4]={0,-1,1,0};
	for(int x=0;x<N-1;x++){
		for(int i=0;i<H+2;i++)for(int j=0;j<W+2;j++)X[i][j]=-1;
		X[ys[x]][xs[x]]=0;
		que.push(make_pair(ys[x],xs[x]));
		while(X[ys[x+1]][xs[x+1]]==-1){
			for(int j=0;j<4;j++){
				pair<int,int>P=que.front();
				que.pop();
				if(S[P.first+ys[j]][P.second+xs[j]]=='X')continue;
				if(X[P.first+ys[j]][P.second+xs[j]]!=-1)continue;
				X[P.first+ys[j]][P.second+xs[j]]=X[P.first][P.second]+1;
			}
		}
		ans+=X[ys[x+1]][xs[x+1]];
	}
	printf("%d\n",ans);
	return 0;
}