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;

void zero();

struct p{
	int x, y, cnt;
};
int H, W, N;
int tizu[1001][1001];
bool flag[1001][1001] = { false };
int main(){
	queue<p> que;
	char a;
	char h[10] = "123456789";
	cin >> H >> W >> N;
	for (int i = 0; i < H; i++){
		for (int j = 0; j < W; j++){
			cin >> a;
			if (a == 'S'){
				tizu[i][j] = 0;
				p temp;
				temp.x = j;
				temp.y = i;
				temp.cnt = 0;
				que.push(temp);
			}
			else if (a == 'X'){
				tizu[i][j] = -1;
			}
			else if (a == '.'){
				tizu[i][j] = 0;
			}
			else {
				for (int k = 0; k < 9; k++){
					if (h[k] == a){
						tizu[i][j] = k + 1;
					}
				}

			}
			//cout << tizu[i][j];
		}
		//cout << endl;
	}
	int max = 1;
	int step = 0;
	while (!que.empty()){
		p temp = que.front();
		que.pop();
		if (tizu[temp.y][temp.x] == max){
			max++;
			while (!que.empty()) que.pop();
			if (max - 1 == N) {
				max = temp.cnt;
				break;
			}
			zero();
		}
		if (tizu[temp.y][temp.x] != true) continue;
		flag[temp.y][temp.x] = true;
		for (int i = 0; i < 4; i++){
			int dx[4] = { 1, -1, 0, 0 };
			int dy[4] = { 0, 0, 1, -1 };
			int yy = temp.y + dy[i];
			int xx = temp.x + dx[i];
			if (yy > -1 && xx > -1 && yy < H && xx < W && tizu[yy][xx] != -1 && tizu[temp.y][temp.x] != true){
					p tt;
					tt.y = yy;
					tt.x = xx;
					tt.cnt = temp.cnt + 1;
					que.push(tt);
			}
		}
	}
	cout << max << endl;
	return 0;
}
void zero(){
	for (int i = 0; i < H; i++){
		for (int j = 0; j < W; j++){
			flag[i][j] = false;
		}
	}
}

C++: #include<cstdio>
#include<queue>
#include<utility>
using namespace std;
char map[1000][1001];
int visited[1000][1000] = {};
int vec[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct point {
	int x; int y; int count;
};
int main() {
	int H, W, N;
	int i, j, k, x, y;
	int count, life;
	int factory[9][2];
	while (scanf("%d %d %d", &H, &W, &N) != EOF) {
		for (i = 0; i < H; i ++) scanf("%s", map[i]);
		life = 0;
		queue<struct point> q;
		for (i = 0; i < H; i ++) {
			for (j = 0; j < W; j ++) {
				visited[i][j] = 0;
				if (map[i][j] == 'S') {
					x = i, y = j;
				} else if (0 < map[i][j] - '0' && map[i][j] - '0' <= N) {
					factory[map[i][j] - '1'][0] = i; factory[map[i][j] - '1'][1] = j;
				}
			}
		}
		struct point p = {x, y, 0};
		q.push(p);
		visited[x][y] = 1;
		int flag = 1;
		while (flag) {
			struct point p = q.front(); q.pop();
			for (k = 0; k < 4; k ++) {
				x = p.x + vec[k][0]; y = p.y + vec[k][1];
				if (-1 < x && x < H && -1 < y && y < W && !(visited[x][y]) && map[x][y] != 'X') {
					if (x == factory[life][0] && y == factory[life][1]) {
						if (life == N - 1) {
							flag = 0; count = p.count + 1;
						}
						life ++;
						for (;!q.empty(); q.pop());
						for (i = 0; i < 1000; i ++) for (j = 0; j < 1000; j ++) visited[i][j] = 0;
						struct point pp = {x, y, p.count + 1};
						q.push(pp); visited[x][y] = 1;
						break;
					}
					struct point pp = {x, y, p.count + 1};
					q.push(pp); visited[x][y] = 1;
				}
			}
		}
		printf("%d\n", count);
	}
	return 0;
}

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

using namespace std;

int H, W, N;
char mapT[1000][1000];
char temp_map[1000][1000];

int temp_x = 0, temp_y;
char temp;
int min_rou = 0;
int min_route = 500000;
int final_min_route = 0;
vector<pair<int, int>> dir = { { -1,0 }, { 0,1 }, { 1,0 }, { 0,-1 } };

void findSmallRoute(int x, int y, char &factory) {
	if (mapT[x][y] == factory) {
		++min_rou;
		if (min_route > (min_rou-1)) {
			min_route = min_rou - 1;
			temp_x = x;
			temp_y = y;
		}
		--min_rou;
		return;
	}
	if (x < 0 || y >= W || x >= H || y < 0 || mapT[x][y] == 'X') {
		return;
	}
	++min_rou;
	mapT[x][y] = 'X';
	for (vector<pair<int, int>>::iterator it = dir.begin(); it != dir.end(); ++it) {
		findSmallRoute(x + it->first, y + it->second, factory);
		
	}
	mapT[x][y] = temp_map[x][y];
	--min_rou;
	return;
}

int main() {
	
	int start_X = 0, start_Y = 0;
	cin >> H >> W >> N;
	getchar();
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; ++j) {
			cin >> temp;
			mapT[i][j] = temp;
			if (temp == 'S') {
				start_X = i;
				start_Y = j;
			}
		}
	}

	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			temp_map[i][j] = mapT[i][j];
		}
	}
	start_X = 0;
	start_Y = 7;
	for (int factory = 1; factory != N + 1; ++factory) {
		
		char n = factory + '0';
		findSmallRoute(start_X, start_Y, n);
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				mapT[i][j] = temp_map[i][j];
			}
		}
		final_min_route += min_route;
		min_rou = 0;
		min_route = 500000;
		
		start_X = temp_x;
		start_Y = temp_y;
	}
	
	cout << final_min_route << endl;
	//system("pause");
	return 0;
}