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<stdlib.h>
#include<cstring>
#include<string.h>
using namespace std;
struct ma
{
	int x;
	int y;
};
int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 };
char m[1200][1200];
int map[1200][1200];
int main()
{
	int ans = 0;
	int tate, yoko;
	int n;
	cin >> tate >> yoko >> n;
	
	ma sta;
	for (int i = 0; i < tate; i++)
	{
		for (int k = 0; k < yoko; k++)
		{
			cin >> m[i][k];
			if (m[i][k] == 'S')
			{
				sta.x = k;
				sta.y = i;
			}
		}
	}
	int HP = 1;
	queue<ma> hoge;
	for (int i = 0; i < n; i++)
	{
		hoge.push(sta);
		
		memset(map, -1, sizeof(map));
		map[sta.y][sta.x] = 0;
		while (!hoge.empty())
		{
			ma now;
			now = hoge.front();
			hoge.pop();
			for (int k = 0; k < 4; k++)
			{
				if (now.y + dy[k] >= tate || now.x + dx[k] >= yoko)continue;
				if (now.y + dy[k] < 0 || now.x + dx[k] < 0)continue;
				if (m[now.y + dy[k]][now.x + dx[k]] != 'X')
				{
					if (map[now.y + dy[k]][now.x + dx[k]] != -1)
					{
						continue;
					}
					map[now.y + dy[k]][now.x + dx[k]] = map[now.y][now.x] + 1;
					if (m[now.y + dy[k]][now.x + dx[k]] - '0' == HP)
					{
						m[now.y + dy[k]][now.x + dx[k]] = '.';
						ans += map[now.y + dy[k]][now.x + dx[k]];
						while (!hoge.empty())hoge.pop();
						HP++;
						sta.x = now.x + dx[k];
						sta.y = now.y + dy[k];
						break;
					}
					else
					{
						ma hage;
						hage.x = now.x + dx[k];
						hage.y = now.y + dy[k];
						hoge.push(hage);
					}
				}
			}

		/*	for (int i = 0; i < tate; i++)
			{
				for (int k = 0; k < yoko; k++)
				{
					cout << map[i][k] << " ";
				}
				cout << endl;
			}
			cout << endl;*/
		}
	}
	cout << ans << endl;

}

C++: #include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <map>
#include <vector>
#include <string>
#include <vector>
#include <cassert>
#include <cmath>
#include <cctype>
#include <queue>
#include <algorithm>
#include <memory>
#include <memory.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 30;

int h, w, n;
int dy[] = {
	1, 0, -1, 0
};
int dx[] = {
	0, 1, 0, -1
};
char area[1002][1002];
int dis[1002][1002];
P fac[10];

int bfs(P s, P g){
	queue<P> que;
	fill(&dis[0][0], &dis[1001][1001]+1, INF);
	que.push(s);
	dis[s.first][s.second] = 0;
	while(!que.empty()){
		P p = que.front();
		que.pop();
		if(p == g) break;
		int y = p.first;
		int x = p.second;
		for(int i = 0; i < 4; i++){
			int y2 = y + dy[i];
			int x2 = x + dx[i];
			if(area[y2][x2] == 'X') continue;
			if(dis[y2][x2] > dis[y][x] + 1){
				dis[y2][x2] = dis[y][x] + 1;
				que.push(P(y2, x2));
			}
		}
	}
	return dis[g.first][g.second];
}

int main(){
	while(cin >> h >> w >> n){
		fill(&area[0][0], &area[1001][1001], 'X');
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				cin >> area[i][j];
				if(area[i][j] == 'S'){
					area[i][j] = '.';
					fac[0] = P(i,j);
				}else if(isdigit(area[i][j])){
					fac[area[i][j] - '0'] = P(i,j);
					area[i][j] = '.';
				}
			}
		}
		int res = 0;
		for(int i = 0; i < n; i++){
			res += bfs(fac[i], fac[i+1]);
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long  cheese[1001][1001];
long long check[1001][1001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int cut=99999;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){//
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
			
		}
	}
	/*for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cout<<cheese[i][j];
		}
		cout<<endl;
	}*/
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(n==y.d&&n==cheese[y.a][y.b]){
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			if(cut>y.c){
				cut=y.c;
			}
			break;
		}
		if(y.d==cheese[y.a][y.b]){
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&cheese[y.a+q[i]][y.b+p[i]]!=-1&&check[y.a+q[i]][y.b+p[i]]!=1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<cut<<endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

#define x first.first
#define y first.second
#define mp(a, b) make_pair(a, b)

typedef pair <int, int> Point;

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
string field[1280];
bool visited[1280][1280];
Point factory[10];
int h, w, n;

void visited_clear(void)
{
	for (int i = 0; i < 1280; i++){
		for (int j = 0; j < 1280; j++){
			visited[i][j] = false;
		}
	}
}

int calc(Point start, Point goal)
{
	long long ret = 0;
	queue < pair <Point, int> > q;
	q.push(mp(start, 0));

	while (!q.empty()){
		pair <Point, int> now = q.front();
		q.pop();
		// cout << now.x << " " << now.y << endl;
		if (visited[now.y][now.x]) continue;
		if (now.y == goal.second && now.x == goal.first){
			ret = now.second;
			break;
		}

		visited[now.y][now.x] = true;
		for (int i = 0; i < 4; i++){
			int ny = now.y + dy[i], nx = now.x + dx[i];
			if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
			if (field[ny][nx] == 'X') continue;
			q.push(mp(mp(nx, ny), now.second + 1));
		}
	}

	return ret;
}

int main(void)
{
	cin >> h >> w >> n;
	for (int i = 0; i < h; i++){
		cin >> field[i];
		for (int j = 0; j < w; j++){
			if (field[i][j] == 'S'){
				factory[0] = mp(j, i);
			}
			if ('1' <= field[i][j] && field[i][j] <= '9'){
				factory[field[i][j] - '0'] = mp(j, i);
			}
		}
	}

	long long ret = 0;
	for (int i = 0; i < n; i++){
		visited_clear();
		ret += calc(factory[i], factory[i + 1]);
	}

	cout << ret << endl;
}