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<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int m[1002][1002]={0},p[1002][1002]={0};
struct r{
int g,l,g2,l2;
};
int main(){
int h,w,n,i,j,a,b[2];
b[0]=1;
b[1]=-1;
char c[2001];
r gl;
queue<r> q;
cin>>h>>w>>n;
for(i=0;i<=h+1;i++){
m[i][0]=-1;
m[i][w+1]=-1;
}
for(i=0;i<=w+1;i++){
m[0][i]=-1;
m[h+1][i]=-1;
}
for(i=1;i<=h;i++){
	scanf("%s",c);
	for(j=1;j<=w;j++){
		a=c[j-1];
		if(a>=49&&a<=57){m[i][j]=a-48;}
		if(c[j-1]=='X'){m[i][j]=-1;}
		if(c[j-1]=='S'){
		gl.g=i;
		gl.g2=j;
		gl.l=0;
		gl.l2=1;
		q.push(gl);
		}
	}
}
while(!q.empty()){
	gl=q.front();
	q.pop();
	if(m[gl.g][gl.g2]==gl.l2){
		gl.l2++;
		if(gl.l2==n+1){
			break;
		}
		while(!q.empty()){
			q.pop();
		}
	}
	p[gl.g][gl.g2]=gl.l2;
gl.l++;
	for(i=0;i<2;i++){
		if(m[gl.g+b[i]][gl.g2]!=-1 && p[gl.g+b[i]][gl.g2]!=gl.l2){
			gl.g+=b[i];
			q.push(gl);
			gl.g-=b[i];
		}
		if(m[gl.g][gl.g2+b[i]]!=-1 && p[gl.g][gl.g2+b[i]]!=gl.l2){
			gl.g2+=b[i];
			q.push(gl);
			gl.g2-=b[i];
		}
	}
}
cout<<gl.l<<endl;
return 0;
}

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

using namespace std;

int W, H, N;

struct Vector2{
	int x;
	int y;
};

bool exist(int map[2000][2000], Vector2 p, int step){
	if(p.x < 0 || p.y < 0 || p.x >= W || p.y >= H){
		return false;
	}
	if(map[p.y][p.x] > step + 1){
		map[p.y][p.x] = step + 1;
		return true;
	}
	return false;
}

int map[2000][2000];

int main(){
	
	string map_[2000];
	
	cin >> H >> W >> N;
	
	for(int i = 0; i < H; i++){
		cin >> map_[i];
	}
	
	int ans = 0;
	
	Vector2 start, goal, punct, p, p1;
	
	punct.x = -1;
	punct.y = -1;
	
	queue<Vector2> que;
	
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(map_[i][j] == 'S'){
				start.x = j;
				start.y = i;
				map[i][j] = 0;
			}
		}
	}
	
	for(int loop = 0; loop < N; loop++){
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				if(map_[i][j] == 'X'){
					map[i][j] = -1;
				}else if(map_[i][j] == '0' + loop){
					start.x = j;
					start.y = i;
					map[i][j] = 0;
				}else if(map_[i][j] == '0' + loop + 1){
					goal.x = j;
					goal.y = i;
					map[i][j] = 1 << 30;
				}else{
					map[i][j] = 1 << 30;
				}
			}
		}
		que.push(start);
		que.push(punct);
		
		bool checker = false;
		
		for(int step = 0; step < 10000000; step++){
			while(true){
				p = que.front();
				que.pop();
				if(p.x == punct.x && p.y == punct.y){
					que.push(punct);
					break;
				}else if(p.x == goal.x && p.y == goal.y){
					checker = true;
					break;
				}
				p1.x = p.x - 1;
				p1.y = p.y;
				if(exist(map, p1, step)){
					que.push(p1);
				}
				p1.x = p.x + 1;
				p1.y = p.y;
				if(exist(map, p1, step)){
					que.push(p1);
				}
				p1.x = p.x;
				p1.y = p.y - 1;
				if(exist(map, p1, step)){
					que.push(p1);
				}
				p1.x = p.x;
				p1.y = p.y + 1;
				if(exist(map, p1, step)){
					que.push(p1);
				}
			}
			if(checker){
				ans += step;
				break;
			}
		}
		
		while(!que.empty()){
			que.pop();
		}
		
		/*
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				if(map[i][j] == 1 << 30){
					printf("0 ");
				}else{
					printf("%d ", map[i][j]);
				}
			}
			printf("\n");
		}
		*/
	}
	
	printf("%d\n", ans);
	
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e9
#define rep(i,n) for(int i=0;i<n;i++)
typedef pair<int,int> p;

char m[1001][1001];
int H,W,N,gx[10],gy[10],d[1001][1001],dx[]={1,0,-1,0},dy[]={0,1,0,-1};

int bfs(int no){
	queue<p> q;
	rep(i,H)rep(j,W)d[i][j]=INF;
	q.push(p(gx[no],gy[no]));
	d[gy[no]][gx[no]]=0;
	while(!q.empty()){
		p c=q.front();q.pop();
		if(c.first==gx[no+1]&&c.second==gy[no+1])return d[gy[no+1]][gx[no+1]];
		rep(i,4){
			int nx=c.first+dx[i],ny=c.second+dy[i];
			if(nx>=0&&nx<W&&ny>=0&&ny<H&&m[ny][nx]!='X'&&d[ny][nx]==INF){
				q.push(p(nx,ny));
				d[ny][nx]=d[c.second][c.first]+1;
			}
		}
	}
}

int main(){
	cin>>H>>W>>N;
	rep(i,H)rep(j,W){
		cin>>m[i][j];
		if(m[i][j]=='S'){gx[0]=j;gy[0]=i;}
		if('1'<=m[i][j]&&m[i][j]<='9'){gx[m[i][j]-'0']=j;gy[m[i][j]-'0']=i;}
	}
	int ans=0;
	rep(i,N)ans+=bfs(i);
	cout<<ans<<endl;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>

using namespace std;

struct point
{
	int x, y, step, vital;
};
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};

int H, W, N;

point search(const vector< vector<char> >& field, point start)
{
	vector< vector<bool> > isVisited(H, vector<bool>(W));
	queue<point> Que;
	Que.push(start);
	isVisited[start.y][start.x] = true;
	while (!Que.empty())
	{
		point p = Que.front();
		Que.pop();
		if (isVisited[p.y][p.x] != true && field[p.y][p.x] == p.vital + '0')
		{
			++p.vital;
			return(p);
		}
		isVisited[p.y][p.x] = true;
		for (int i = 0; i < 4; ++i)
		{
			int nx = p.x + dx[i];
			int ny = p.y + dy[i];
			if (0 <= nx && nx < W && 0 <= ny && ny < H && isVisited[ny][nx] == false && field[ny][nx] != 'X')
			{
				point np;
				np.x = nx;
				np.y = ny;
				np.step = p.step + 1;
				np.vital = p.vital;
				Que.push(np);
			}
		}
	}
}

void solve()
{
	cin >> H >> W >> N;
	vector< vector<char> > field(H, vector<char>(W));
	point start;
	for (int i = 0; i < H; ++i)
	{
		for (int j = 0; j < W; ++j)
		{
			cin >> field[i][j];
			if (field[i][j] == 'S')
			{
				start.x = j;
				start.y = i;
				start.step = 0;
				start.vital = 1;
			}
		}
	}
	int sum = 0;
	while (start.vital < N + 1)
	{
		start = search(field, start);
		sum += start.step;
		start.step = 0;
	}
	cout << sum << endl;
}

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