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 <stdlib.h>
#include <queue>
#define MAX_NUM 1010
using namespace std;
int H, W, N;
bool flag[MAX_NUM][MAX_NUM] = {false};

class Node
{
public:
	Node();
	Node(int x, int y, int step);
	~Node();
	int x;
	int y;
	int step;
};

Node::Node()
{
}

Node::Node(int x, int y, int step) {
	this->x = x;
	this->y = y;
	this->step = step;
}

Node::~Node()
{
}
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int BFS(char map[][MAX_NUM], int sx, int sy, int ex, int ey) {
	queue<Node> que;
	Node node(sx, sy, 0);
	que.push(node);
	flag[sx][sy] = true;
	while (!que.empty())
	{
		Node cur = que.front();
		que.pop();
		if (cur.x == ex && cur.y == ey)
		{
			return cur.step;
		}
		else
		{
			for (int i = 0; i < 4; i++)
			{
				int k = cur.x + dir[i][0];
				int v = cur.y + dir[i][1];
				if (k>=0&&k<H&&v>=0&&v<W&&map[k][v]!='X'&&!flag[k][v])
				{
					Node temp(k, v, cur.step + 1);
					que.push(temp);
					flag[k][v] = true;
				}
			}
		}
	}
	return -1;

}

int main()
{
	int sx, sy, arr[10][2];
	char map[MAX_NUM][MAX_NUM];
	while (cin >> H >> W >> N && H && W)
	{
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> map[i][j];
				if (map[i][j] == 'S')
				{
					map[i][j] = '.';
					sx = i;
					sy = j;
				}
				if (isdigit(map[i][j]))
				{
					int index = map[i][j] - '0';
					arr[index][0] = i;
					arr[index][1] = j;
				}
			}
		}
		arr[0][0] = sx;
		arr[0][1] = sy;
		int res = 0;
		for (int i = 0; i+1 <= N; i++)
		{
			memset(flag, false, sizeof(flag));
			res += BFS(map, arr[i][0], arr[i][1], arr[i + 1][0], arr[i + 1][1]);
		}
		cout << res << endl;
	}
	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[H][W], hp = 1;
  fill(d[0],d[H],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[i][j] = 0;
      }
    }
  }
  while(!q.empty()){
    pii p = q.front();
    q.pop();
    if(map[p.first][p.second] >= '1' && map[p.first][p.second] <= '9' && hp == map[p.first][p.second]-'0') hp++;
    if(hp == N+1){
      cout << d[p.first][p.second] << 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[next_p.first][next_p.second] > d[p.first][p.second]+1){
        d[next_p.first][next_p.second] = d[p.first][p.second]+1;
        q.push(next_p);
      }
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;
static const int INF = 99999999;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

void solve()
{
	int H, W, N;
	cin >> H >> W >> N;
	vector< vector<char> > Field(H, vector<char>(W));
	P start(0, 0);
	for(int i = 0; i < H; ++i)
	{
		for(int j = 0; j < W; ++j)
		{
			cin >> Field[i][j];
			if(Field[i][j] == 'S')
			{
				start.first = j;
				start.second = i;
			}
		}
	}
	int sum = 0;
	int Target_Num = 1;
	P goal(start.first, start.second);
	while(Target_Num <= N)
	{
		vector< vector<int> > d(H, vector<int>(W));
		for(int i = 0; i < H; ++i)
		{
			for(int j = 0; j < W; ++j)
			{
				d[i][j] = INF;
			}
		}

		start = goal;
		for(int i = 0; i < H; ++i)
		{
			for(int j = 0; j < W; ++j)
			{
				if(Field[i][j] == '0' + Target_Num)
				{
					goal.first = j;
					goal.second = i;
				}
			}
		}
		queue<PP> Que;
		Que.push(PP(start, 0));
		while(!Que.empty())
		{
			PP pp = Que.front();
			Que.pop();
			if(pp.first.first == goal.first && pp.first.second == goal.second)
			{
				sum += pp.second;
				break;
			}
			else
			{
				for(int i = 0; i < 4; ++i)
				{
					int nx = pp.first.first + dx[i];
					int ny = pp.first.second + dy[i];
					if(0 <= nx && nx < W && 0 <= ny && ny < H && Field[ny][nx] != 'X' && d[ny][nx] > pp.second + 1)
					{
						d[ny][nx] = pp.second + 1;
						Que.push(PP(P(nx, ny), pp.second + 1));
					}
				}
			}
		}
		++Target_Num;
	}
	cout << sum << endl;
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <ctype.h>
using namespace std;

typedef pair<int,int> P;
int H,W,N;
int inf = 10e8;
int dx[] = {-1,1,0,0},dy[] = {0,0,-1,1};
char G[1010][1010];
int hp = 1,sum = 0;
int sx,sy;
int times[1010][1010];

bool valid(int x,int y){
	return 0<=x && x<H && 0<=y && y<W && G[x][y]!='X';
}

int bfs(){
	queue<P> q;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			times[i][j] = inf;
		}
	}
	while(hp<=N){
		q.push(P(sx,sy));
		times[sx][sy] = 0;
		while(q.size()){
			P p = q.front();q.pop();
			if(isdigit(G[p.first][p.second]) && hp == atoi(&G[p.first][p.second])){
				hp++;
				sum += times[p.first][p.second];
				sx = p.first;sy = p.second;
				for(int i=0;i<H;i++){
					for(int j=0;j<W;j++){
						times[i][j] = inf;
					}
				}
				while(q.size()){
					q.pop();
				}
				break;
			}
			for(int i=0;i<4;i++){
				int x = p.first + dx[i] , y  = p.second + dy[i];
				if(valid(x,y) && times[x][y]==inf){
					q.push(P(x,y));
					times[x][y] = times[p.first][p.second] + 1;
				}
			}
		}
	}
	return sum;
}

int main(){
	cin >> H >> W >> N;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin >> G[i][j];
			if(G[i][j] == 'S'){
				sx = i;
				sy = j;
			}
		}
	}
	cout << bfs() << endl;
}