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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
 
typedef struct{
  short x,y;
  int cost;
  short ch;
}mouse;
const int MAX=(1<<14)-1;
mouse Q[MAX+1];
int s,num;

mouse t;
short cx,cy,nx,ny,ch,nch,h,w,n;
int cost;

char in[16];
int inum[3];
 
main(){
  int s=0;
  int nums=0;
  do{
    read(0,in+s,1);
    if(isdigit(in[s]))inum[nums]=inum[nums]*10+in[s]-'0';
    else nums++;
  }while(in[s++]!='\n');
  h=inum[0];
  w=inum[1];
  n=inum[2];

  short maxch[h][w];
  char ku[h][w];
  
  rep(i,h)
    read(0,ku[i],w+1);


 
 
  rep(i,h)rep(j,w){
    if(ku[i][j]=='S')cx=i,cy=j;
    maxch[i][j]=-1;
  }
 
 
  Q[s+num++]=(mouse){cx,cy,0,0};
  maxch[cx][cy]=0;
 
  while(true){
    t=Q[s++&MAX];
    num--;
    cx=t.x,cy=t.y,cost=t.cost;
    ch=t.ch;
 
    rep(i,4){
      nx=cx+dx[i],ny=cy+dy[i];
      if(nx<0 || nx>=h || ny<0 || ny>=w)continue;
      if(ku[nx][ny]=='X')continue;
      nch=ch;
      if(ku[nx][ny]<='9' && ku[nx][ny]>='1' && ku[nx][ny]-'0'==t.ch+1){
        nch=ch+1;
      }
 
      if(maxch[nx][ny]>=nch)continue;
      if(nch==n){
        printf("%d\n",cost+1);
        return 0;
      }
      maxch[nx][ny]=nch;
      Q[s+num++&MAX]=(mouse){nx,ny,cost+1,nch};
    }
  }
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <string>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define fi first
#define se second
static const int INF = 1e9;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
string board[1001];
int d[1001][1001];
int h, w, n;
pair<int, int> point[10];//fi=y se=x
int ans = 0;
bool flag;
//???????????¢?´¢
void bfs(void){
	rep(i, n){//??????????????¨??´?????????
		int starty = point[i].fi, startx = point[i].se;//start
		int endy = point[i + 1].fi, endx = point[i + 1].se;//end
		queue<pair<int, int> > q;
		q.push(make_pair(starty, startx));
		rep(i, h) rep(j, w) d[i][j] = INF;
		d[starty][startx] = 0;
		flag = false;
		while(!q.empty()){
			pair<int, int> now;
			now = q.front(); q.pop();
			rep(j, 4){
				int y = now.fi + dy[j];
				int x = now.se + dx[j];
				if(y < 0 || h <= y || x < 0 || w <= x) continue;
				if(d[y][x] == INF && board[y][x] != 'X'){
					d[y][x] = d[now.fi][now.se] + 1;
					q.push(make_pair(y, x));
				}
				if(y == endy && x == endx){
					flag = true;
					ans += d[y][x];
					break;
				}
			}
			if(flag == true) break;
		}
	}
	return;
}

int main(void){
	cin >> h >> w >> n;
	rep(i, h){
		cin >> board[i];
	}

	rep(i, h) rep(j, w){
		if(board[i][j] == 'S'){
			point[0].fi = i; point[0].se = j;
		}else if ('1' <= board[i][j] && board[i][j] <= '9'){
			int tmp = board[i][j] - '0';
			point[tmp].fi = i; point[tmp].se = j;
		}
	}
	bfs();
	cout << ans;
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
/*(^(^(^(^(^o^)^)^)^)^) &#9602;&#9605;&#9607;&#9608;&#9619;&#9618;&#9617;('ω')&#9617;&#9618;&#9619;&#9608;&#9607;&#9605;&#9602; 初のBFS*/
typedef pair<int,int> p;
char x[1001][1001];
int c[10]={},d[10],y[1001][1001],a[10],b[10];
int h,q,n,i,j,now=1,sum=0,pro[10];
int bfs(){
	int dx[]={1,0,-1,0};
	int dy[]={0,1,0,-1};
	queue<p>orz;
	for(i=1;i<=h;i++){
	for(j=1;j<=q;j++){
		y[i][j]=10000000;
		if(x[i][j]=='S'){
			a[0]=i;
			b[0]=j;
			y[i][j]=0;
		}
		if(x[i][j]-'0'>0 && x[i][j]-'0'<10){
			a[x[i][j]-'0']=i;
			b[x[i][j]-'0']=j;
		}}}
	for(int u=1;u<=n;u++){
		while(orz.size()){
			orz.pop();
		}
	for(i=1;i<=h;i++){
	for(j=1;j<=q;j++){
		y[i][j]=10000000;
		}}
	while(1){
		orz.push(p(a[u-1],b[u-1]));
		p w=orz.front();
		orz.pop();
		if(w.first==a[u]&&w.second==b[u]){
			pro[u]=y[a[u]][b[u]]-10000000;
			break;
		}
		for(i=0;i<4;i++){
			int ax=w.first+dx[i];
			int ay=w.second+dy[i];
			if(ax>=1&&ax<=h&&ay>=1&&ay<=q&&y[ax][ay]==10000000&&x[ax][ay]!='X'){
				orz.push(p(ax,ay));
				y[ax][ay]=y[w.first][w.second]+1;
			}
		}
	}
	}
for(i=1;i<=n;i++){
		sum+=pro[i];
	}
return sum;
}
int main(){
scanf("%d %d %d",&h,&q,&n);
for(i=1;i<=h;i++){
scanf("%s",&x[i]);
}
for(i=1;i<=h;i++){
for(j=q-1;j>=0;j--){
	x[i][j+1]=x[i][j];
}}
int ans=bfs();
printf("%d\n",ans);
scanf("%d",&i);
return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int MAX_HW = 1000;
int n, h, w, rat = 1, s[2], g[2], flag[MAX_HW][MAX_HW][2];
char town2[MAX_HW][MAX_HW];
queue <P> que;
P p;
void bfs(int sy, int sx){
	que.push(P(sy, sx));
	flag[p.first][p.second][1] = 0;
	while (que.size()){
	RE:
		p = que.front();
		que.pop();
		flag[p.first][p.second][0] = rat;
		for (int i = -1; i <= 1; i++){
			for (int j = -1; j <= 1; j++){
				if (i == 0 || j == 0){
					if (town2[p.first + i][p.second + j] != 'X'&&flag[p.first + i][p.second + j][0] != rat&&p.first + i < h&&p.first + i >= 0 && p.second + j < w&&p.second + j >= 0) {
						if (town2[p.first + i][p.second + j]-48 == rat){
							flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
							if (rat == n) return;
							rat++;
							while (que.size()){
								que.pop();
							}
							que.push(P(p.first + i, p.second + j));
							goto RE;
						}
						flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
						que.push(P(p.first + i, p.second + j));
					}
				}
			}
		}
	}
}
int main(){
	cin >> h >> w >> n;
	rat = 1;
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			cin >> town2[i][j];
			if (town2[i][j] == 'S'){
				s[0] = i;
				s[1] = j;
			}
			if (town2[i][j]-48 == n){
				g[0] = i;
				g[1] = j;
			}
		}
	}
	bfs(s[0], s[1]);
	cout << flag[g[0]][g[1]][1] << endl;
	return 0;
}