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>
#include <cstring>
using namespace std;

static const int mx[] = {1, 0, -1,  0};
static const int my[] = {0, 1,  0, -1};
static const char cheese_arr[] = {"123456789"};

static const int MAX_MAP_X = 1001;
static const int MAX_MAP_Y = 1001;

static bool visited[MAX_MAP_Y - 1][MAX_MAP_X - 1];

struct MOUSE
{

	int x;
	int y;
	int cnt;

};

int main()
{

	int W, H, N;
	int ans = 0, now_search = 0;
	char MAP[MAX_MAP_Y][MAX_MAP_X];
	bool brkFlg = false;
	queue<MOUSE> search_cheese;

	cin >> H >> W >> N;
	for(int i = 0; i < H; i++) {
		cin >> MAP[i];
		for(int j = 0; j < W || !brkFlg; j++) {
			if(MAP[i][j] == 'S') {
				MOUSE in;
				in.x = j;
				in.y = i;
				in.cnt = 0;
				memset(visited, false, sizeof(visited));
				search_cheese.push(in);
				brkFlg = true;
			}
		}
	}

	brkFlg = false;

	while(!brkFlg) {
		MOUSE next, mouse_info = search_cheese.front();
		search_cheese.pop();
		for (int i = 0; i < 4; i++) {
			int nx = mouse_info.x + mx[i];
			int ny = mouse_info.y + my[i];
			if (nx > -1 && nx < W && ny > -1 && ny < H) {
				if (MAP[ny][nx] == cheese_arr[now_search]) {
					ans += mouse_info.cnt + 1;
					now_search++;
					if(now_search == N) {
						brkFlg = true;
						break;
					}
					while(!search_cheese.empty()) search_cheese.pop();
					mouse_info.x = nx;
					mouse_info.y = ny;
					mouse_info.cnt = 0;
					memset(visited, false, sizeof(visited));
					search_cheese.push(mouse_info);
				}
				else if(MAP[ny][nx] != 'X' && !visited[ny][nx]) {
					next = mouse_info;
					next.x = nx;
					next.y = ny;
					next.cnt++;
					visited[ny][nx] = true;
					search_cheese.push(next);
				}
			}
		}
	}

	cout << ans << endl;

	return 0;

}

C++: #include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <cstring>

using namespace std;
typedef pair<int, int> pii;

string field[1000];
int passed[1000][1000];

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

int main(){
	int H, W, N, answer = 0;
	cin >> H >> W >> N;
	pii from = pii(0, 0);
	for(int i = 0; i < H; ++i){
		cin >> field[i];
		char *pos = strchr(field[i].c_str(), 'S');
		if(pos != NULL){ from = pii(pos - field[i].c_str(), i); }
	}
	for(int i = 1; i <= N; ++i){
		for(int j = 0; j < H; ++j){ memset(passed[j], 0, sizeof(int) * W); }
		char goal = '0' + i;
		queue<pii> q;
		q.push(from);
		while(!q.empty()){
			pii pt = q.front();
			if(field[pt.second][pt.first] == goal){ break; }
			q.pop();
			int dist = passed[pt.second][pt.first];
			for(int j = 0; j < 4; ++j){
				pii next = pii(pt.first + dx[j], pt.second + dy[j]);
				if(
					0 <= next.first && next.first < W &&
					0 <= next.second && next.second < H &&
					field[next.second][next.first] != 'X' &&
					passed[next.second][next.first] == 0
				){
					passed[next.second][next.first] = dist + 1;
					q.push(next);
				}
			}
		}
		answer += passed[q.front().second][q.front().first];
		from = q.front();
	}
	cout << answer << endl;
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<cstdio>
#include<map>
#pragma warning(disable : 4996)
#define P pair<int,int>
using namespace std;

bool g[1000][1000]{};
int x[] = { -1,0,0,1 }, y[] = { 0,-1,1,0 };
map<int, P>j;
int a, b, c;
int meiro(int d) {
	queue<P> e;
	vector<vector<int>>f(a, vector<int>(b));
	for (int g = 0; g < a; g++) {
		for (int h = 0; h < b; h++) {
			f[g][h] = 1 << 29;
		}
	}
	e.push(j[d]);
	f[(j[d]).first][(j[d]).second] = 0;
	while(e.size()) {
		P p = e.front(); e.pop();
		if (p == j[d+1])break;
		for (int q = 0; q < 4; q++) {
			int l = p.first + x[q];
			int r = p.second + y[q];
			if (l >= 0 && l < a&&r >= 0 && r < b&&g[l][r] && f[l][r] == 1 << 29) {
				P w(l, r);
				e.push(w);
				f[l][r] = f[p.first][p.second] + 1;
			}
		}
	}
	return f[(j[d + 1]).first][(j[d + 1]).second];
}
int main() {
	scanf("%d%d%d", &a, &b, &c);
	for (int d = 0; d < a; d++) {
		for (int e = 0; e < b; e++) {
			char f;
			cin >> f;
			if (f == 'X')g[d][e] = false;
			else if (f == '.')g[d][e] = true;
			else if (f == 'S') {
				g[d][e] = true;
				P h(d, e);
				j[0] = h;
			}
			else {
				g[d][e] = true;
				P h(d, e);
				j[f - 48] = h;
			}
		}
	}
	int s = 0;
	for (int l = 0; l < c; l++) {
		s += meiro(l);
	}
	printf("%d\n", s);
}

C++: #include<iostream>

typedef std::pair<int,int> P;

int h,w,PX[4]={1,-1,0,0},PY[4]={0,0,1,-1};
char ch[100][100];
bool J[100][100];
int sum=-1;

void dfs(P p){
	J[p.first][p.second]=true;	
	for(int i=0;i<4;++i){
		if(p.first+PY[i]>=0 && h>p.first+PY[i] && w>p.second+PX[i] && p.second+PX[i]>=0){
		  if(ch[p.first][p.second]==ch[p.first+PY[i]][p.second+PX[i]]){
			dfs(P(p.first+PY[i],p.second+PX[i]));
		  }
		}
	}
}

void solve(){
	for(int i=0;i<h;++i){
		for(int j=0;j<w;++j){
			J[i][j]=false;
		}
	}
	for(int i=0;i<h;++i){
		for(int j=0;j<w;++j){
			if(J[i][j]==false){
				++sum;
				dfs(P(i,j));
			}
		}
	}
}

int main(){
	while(1){
		std::cin>>h>>w;
		if(h==0&&w==0)
			break;
		for(int i=0;i<h;++i){
			for(int j=0;j<w;++j){
				std::cin>>ch[i][j];
			}
		}
		solve();
		std::cout<<sum<<std::endl;
	}
	return 0;
}