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<cstdio>
#include<queue>
#define inf 10000000
using namespace std;
int H,W,N,x0,y0;
char map[1005][1005];
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
typedef pair<int,int>P;
int bfs(int sx,int sy)
{
	int energy=1;
	int tx,ty,d[1005][1005];
	queue<P>q;
	for(int i=0;i<H;i++)
	{
		for(int j=0;j<W;j++)
		d[i][j]=inf;
	}
	q.push(P(sx,sy));
	d[sx][sy]=0;
	while(!q.empty())
	{
		P p=q.front();
		if(map[p.first][p.second]-'0'==N)
		return d[p.first][p.second];
		q.pop();
		for(int i=0;i<4;i++)
		{
			tx=p.first+dx[i];
			ty=p.second+dy[4];
			if(tx>=0&&tx<H&&ty>=0&&ty<W&&map[tx][ty]-'0'<=energy)
			{
				d[tx][ty]=d[p.first][p.second]+1;
				energy++;
				q.push(P(tx,ty));
			}
		}
	}
}
int main(void)
{
	while(scanf("%d%d%d",&H,&W,&N)==3)
	{
		for(int i=0;i<H;i++)
		scanf("%s",&map[i]);
		bool ok=false;
		for(int i=0;i<H;i++)
		{
			for(int j=0;j<W;j++)
			{
				if(map[i][j]=='S')
				{
					x0=i;
					y0=j;
					map[i][j]='.';
					ok=true;
					break;
				}
			}
			if(ok)
			break;
		}
		int ans=bfs(x0,y0);
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <array>
#include <stack>
#include <queue>
#include <chrono>
#include <numeric>

#pragma warning(disable:4996)
#define REP(i,n) for(int i = 0 ; i < n ; ++i)
typedef long long Int;

using namespace std;

struct point{
	Int j, i,step;
	point(Int i_, Int j_,Int s) :i(i_), j(j_),step(s){}
};

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

queue<point> que;
char tile[1000][1000];
int H, W;
bool ed[1000][1000];

point solve(char target)
{
	while (!que.empty()){
		point now(que.front());
		ed[now.j][now.i] = true;
		que.pop();
		REP(i, 4){
			int x = now.j + dx[i];
			int y = now.i + dy[i];

			if (x < 0 || y < 0 || x > W || y > H || ed[x][y])
				continue;

			if (tile[y][x] == target){
				return point(y, x,now.step+1);
			}

			if (tile[y][x] != 'X'){
				que.push(point(y, x, now.step + 1));
				ed[x][y] = true;
			}
		}
	}
}

int main()
{
	cin.tie(0); ios::sync_with_stdio(false);
	//fstream fs("input.txt");
	////////////ここから開始///////////
	int n(0);
	cin >> H >> W >> n;

	REP(i, 1000)REP(j, 1000)tile[i][j] = 'X';

	REP(i, H)REP(j, W)cin >> tile[i][j];

	REP(i, H)REP(j, W){ if (tile[i][j] == 'S')que.emplace(i, j, 0); }
	Int ans(0);
	for(int i = 0 ; i < n ; ++i){
		REP(i_1, H)REP(j_1, W){ ed[i_1][j_1] = false; }
		char c = '1' + i;
		point t = solve('1' + i);
		que = {};
		que.emplace(t);
		ans = t.step;
	}
	
	cout << ans << endl;

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second
#define debug(x) cout << #x << " = " << x << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;

int h, w, n;

bool visit[1001][1001];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int bfs(vector<string> &v, P s, P g){
	memset(visit, 0, sizeof(visit));
	
	queue<pair<P, int> > q;
	q.push(MP(s, 0));

	while(!q.empty()){
		P p = q.front().first;
		int t = q.front().second;
		q.pop();

		if(visit[p.Y][p.X]) continue;
		visit[p.Y][p.X] = true;

		if(p == g) return t;

		rep(i, 4){
			int x = p.X + dx[i];
			int y = p.Y + dy[i];
			if(x < 0 || x >= w || y < 0 || y >= h) continue;
			if(v[y][x] == 'X') continue;
			q.push(MP(MP(x, y), t+1));
		}
	}

	return 1<<31;
}

int main(){
	cin >> h >> w >> n;
	vector<string> v(h);
	rep(i, h) cin >> v[i];

	vector<P> point(n+1);
	rep(i, h) rep(j, w){
		if(v[i][j] == 'S') point[0] = MP(j, i);
		if(v[i][j] >= '1' && v[i][j] <= '9'){
			point[v[i][j]-'0'] = MP(j, i);
		}
	}

	int ans = 0;
	rep(i, n){
		ans += bfs(v, point[i], point[i+1]);
	}
	cout << ans << endl;

	return 0;
}