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 <queue>
using namespace std;

typedef pair<int, int> P;

const int MAX_H = 1000, MAX_W = 1000;
const int INF = 100000000;
int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];
int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
int result = 0;
P start, goal;

int bfs(P start, P goal);

int main(){
	cin >> H;
	cin >> W;
	cin >> N;
	for(int i=0; i<H; i++){
		cin >> field[i];
	}

	for(int i=0; i<H; i++){
		for(int j=0; j<W; j++){
			if(field[i][j] == 'S'){
				goal.first = i;
				goal.second = j;
			}
		}
	}

	for(int n=1; n<=N; n++){
		start.first = goal.first;
		start.second = goal.second;

		for(int i=0; i<H; i++){
			for(int j=0; j<W; j++){
				if(field[i][j] == n+'0'){
					goal.first = i;
					goal.second = j;
				}
			}
		}
		result += bfs(start, goal);
	}

	cout << result << endl;

	return 0;
}

int bfs(P start, P goal){
	for(int i=0; i<MAX_H; i++)
		for(int j=0; j<MAX_W; j++) d[i][j] = INF;

	queue<P> que;
	que.push(start);
	d[start.first][start.second] = 0;

	while(que.size()){
		P p = que.front();
		que.pop();

		if(p.first==goal.first && p.second==goal.second) break;

		for(int i=0; i<4; i++){
			int xnew = p.first + dx[i];
			int ynew = p.second + dy[i];

			if(0<=xnew && xnew<H && 0<=ynew && ynew<W && field[xnew][ynew]!='X' && d[xnew][ynew]==INF){
				que.push(P(xnew, ynew));
				d[xnew][ynew] = d[p.first][p.second] + 1;
			}
		}
	}

	return d[goal.first][goal.second];
}

C++: #include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
//#include <functional>
#include <cstdio>
#include <queue>
#include <fstream>
using namespace std;

#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'

struct Point
{
	int x;
	int y;
};

int dx[] = {0,-1,0,1};//ã©çvñè
int dy[] = {1,0,-1,0};
string field[1000];
int mfield[1001][1001] = {};
int life=1;
int csum = 0;
int H,W,N;


int open(Point gool)
{
	int counter=0;
	queue<Point> tp;
	tp.push(gool);
	int i,j;
	Point inp;
	Point p;
	while(1)
	{
		counter++;
		int size = tp.size();
		for(i=0;i<size;i++)
		{
			p = tp.front();
			tp.pop();
			for(j=0;j<4;j++)
			{
				inp.x = p.x+dx[j];
				inp.y = p.y+dy[j];
				if(inp.x<0 || inp.y<0 || inp.y>=H || inp.x>=W)
					continue;
				if(field[inp.y][inp.x] != CANTGO && field[inp.y][inp.x] != START  && mfield[inp.y][inp.x] != life)
				{
					//v·éÉÅZ£Ú®Â\][
					if(field[inp.y][inp.x] == life + '0' ) //©ªÉ©Á½`[YÌ­©
					{
						return counter;
					}
					else
					{
						tp.push(inp);
						mfield[inp.y][inp.x] = life;
					}
				}
			}
		}
		/*for(i=0;i<H;i++)
		{
			for(j=0;j<W;j++)
			{
				cout << mfield[i][j];
			}
			cout << endl;
		}*/
	}
	//copy(tp.begin() , tp.end() , tp.begin());
	return 0;
}

int main(void)
{
	int i,j;
	queue<Point> tp;
	Point top[10];
	Point inp;
	int count = 0;
	int counter=0;
	cin >> H >> W >> N;

	for(i=0;i<H;i++)//üÍ
	{
		cin >> field[i];
		for(j=0;j<W;j++)
		{
			if(field[i][j] == 'S')
			{
				top[0].y = i;
				top[0].x = j;
				mfield[j][i] = life;
			}
			if(field[i][j] >= '0' && field[i][j] <= '9')
			{
				top[field[i][j] - '0'].y = i;
				top[field[i][j] - '0'].x = j;
			}
		}
	}//üÍIíè

	while(1)
	{
		if((counter = open(top[life-1])) != 0)
		{
			life++;
			csum += counter;
		}
		if(life > N)
			break;
	}
		cout << csum << endl;

	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int h, w, n;
char field[1010][1010];
int sx, sy, gx[10], gy[10];
ll ans = 0;

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

int d[1010][1010];

void solve(int k){
	REP(i, h){
		REP(j, w){
			d[i][j] = INF;
		}
	}

	queue<P> que;

	que.push(P(sy, sx));
	d[sy][sx] = 0;

	while (que.size()){
		P p = que.front(); que.pop();

		int y = p.first, x = p.second;

		if (y == gy[k] && x == gx[k])break;

		REP(i, 4){
			int nx = x + dx[i], ny = y + dy[i];
			if (nx >= 0 && nx < w&&ny >= 0 && ny < h){
				if (field[ny][nx] != 'X'&&d[ny][nx] == INF){
					d[ny][nx] = d[y][x] + 1;
					que.push(P(ny, nx));
				}
			}
		}
	}
}

int main(){
	int pos;
	cin >> h >> w >> n;
	REP(i, h){
		REP(j, w){
			cin >> field[i][j];
			if (field[i][j] == 'S'){
				sy = i, sx = j;
			}
			if (field[i][j] >= '1'&&field[i][j] <= '9') {
				pos = field[i][j] - '0';
				gy[pos] = i, gx[pos] = j;
			}
		}
	}
	for (int k = 1; k <= n;k++){
		solve(k);
		ans += d[gy[k]][gx[k]];
		sy = gy[k], sx = gx[k];
	}
	cout << ans << endl;
}

C++: #include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
using namespace std;

#define INF 10000000

typedef pair<int,int> P;

queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int H,W,N,sx,sy,sum=0;

int main() {
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++) {
		scanf("%s",map[i]);
		char *ptr=strchr(map[i],'S');
		if(ptr!=NULL) sx=ptr-map[i],sy=i;
	}
	que.push(P(sx,sy));
	for(int i=1;i<=N;i++) {
		for(int j=0;j<H;j++) {
			for(int k=0;k<W;k++) {
				vst[j][k]=INF;
			}
		}
		vst[sy][sx]=0;
		while(!que.empty()) {
			P p=que.front();
			que.pop();
			if(map[p.second][p.first]==i+'0') {
				sum+=vst[p.second][p.first];
				while(!que.empty()) que.pop();
				que.push(P(p.first,p.second));
				sx=p.first,sy=p.second;
				break;
			}
			for(int j=0;j<4;j++) {
				int nx=p.first+dx[j],ny=p.second+dy[j];
				if(map[ny][nx]!='X'&&vst[ny][nx]==INF) {
					vst[ny][nx]=vst[p.second][p.first]+1;
					que.push(P(nx,ny));
				}
			}
		}
	}
	printf("%d\n",sum);
	return 0;
}