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 <cctype>
#include <queue>
using namespace std;
typedef pair<int, int> P;

const int INF = 100000000;
const int MAX_N = 9;
const int MAX_H = 1000;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int H, W, N;
P A[MAX_N + 1];
int d[MAX_H][MAX_H];
char field[MAX_H][MAX_H + 1];

int bfs(int s) {
  queue<P> que;
  for (int i = 0; i < H; i++) 
    for (int j = 0; j < W; j++) d[i][j] = INF;
  que.push(A[s]);
  d[A[s].first][A[s].second] = 0;
  while(que.size()) {
    P p = que.front(); que.pop();
    if (p == A[s + 1]) break;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' && d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[A[s + 1].first][A[s + 1].second];
}

int main() {
  scanf("%d%d%d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", field[i]);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        A[0] = P(i, j);
      } else if (isdigit(field[i][j])) {
        A[field[i][j] - '0'] = P(i, j);
      }
    }
  }
  int res = 0;
  for (int i = 0; i < N; i++) {
    res += bfs(i);
  }
  printf("%d\n", res);
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
char cheese[1002][1002];
bool done[1002][1002];
struct P{
	int x,y,depth;
};

int dx[] = {1,-1,0,0} , dy[] = {0,0,-1,1};
int bfs(int a,int b,int c,int d){
	rep(i,1002)rep(j,1002)done[i][j] = false;
	queue<P> Q;
	P t;
	t.x = a , t.y = b , t.depth = 0;
	Q.push(t);
	while(Q.size()){
		P q = Q.front(); Q.pop();
		if(q.x == c && q.y == d){
			return q.depth;
		}
		if(done[q.y][q.x])continue;
		done[q.y][q.x] = true;
		rep(i,4){
			P t;
			t.x = q.x + dx[i];
			t.y = q.y + dy[i];
			t.depth = q.depth + 1;
			if(cheese[t.y][t.x] == 'X')continue;
			Q.push(t);
		}
	}
	return -1;
}
int main(){
	int h,w,n;cin >> h >> w >> n;
	rep(i,1002)rep(j,1002)cheese[i][j] = 'X';
	rep(i,h)rep(j,w)cin >> cheese[i+1][j+1];
	
	int px[10],py[10];
	rep(i,h+1)rep(j,w+1){
		if(cheese[i][j] >= '1' && cheese[i][j] <= '9'){
			px[cheese[i][j]-'0'] = j;
			py[cheese[i][j]-'0'] = i;
		}
		if(cheese[i][j] == 'S')px[0] = j, py[0] = i;
	}
	int ret = 0;
	rep(i,n){
		ret += bfs(px[i],py[i],px[i+1],py[i+1]);
	}
	cout << ret << endl;
}

C++: #include <cstdio>
#include <queue>
#include <map>
#include <cctype>
#include <climits>

using namespace std;

typedef pair<int, int> P;

#define W 1005
#define H 1005
#define N 15

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

char a[H][W];
int d[H][W];
P fa[N];
int h, w, n;

queue<P> que;
int bfs(int sx, int sy, int gx, int gy){
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			d[i][j] = INT_MAX;
		}
	}
	
	d[sx][sy] = 0;
	que.push(P(sx, sy));
	
	while(!que.empty()){
		P p = que.front();
		que.pop();
		int x = p.first;
		int y = p.second;

		if (x == gx && y == gy) break;
	
		for (int i = 0; i < 4; i++){
			int nx = x + dx[i];
			int ny = y + dy[i];
			
			if (nx >= 0 && nx < h && ny >= 0 && ny < w && a[nx][ny] != 'X' && d[nx][ny] == INT_MAX){
				d[nx][ny] = d[x][y] + 1;
				
				que.push(P(nx, ny));
			}
		}
	}
	return d[gx][gy];
}

int main(){
	//freopen("aoj_0558.input", "r", stdin);
	scanf("%d %d %d", &h, &w, &n);
	for (int i = 0; i < h; i++){
		getchar();
		for (int j = 0; j < w; j++){
			scanf("%c", &a[i][j]);
			if (a[i][j] == 'S'){
				fa[0] = P(i, j);
			}
			if (isdigit(a[i][j])){
				fa[a[i][j] - '0'] = P(i, j);
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++){
		sum += bfs(fa[i].first, fa[i].second, fa[i + 1].first, fa[i + 1].second);
	}
	printf("%d\n", sum);
	return 0;
}

C++: //include
#include<bits/stdc++.h>

//using
using namespace std;


//define static const typedef
#define FOR(i, s, n) for(int i = s; i < int(n); i++)
#define REP(i, n) FOR((i), 0, (n))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define DUMP(x) cerr << #x << " = " << x << "\n";
#define DEBUG(x) cerr << #x << " = " << x << " (L:" << __LINE__ << ") " << __FILE__ << "\n";
#define F first
#define S second

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

static const int INF = 1 << 25;
static const int movex[] = {0,0,1,-1};
static const int movey[] = {1,-1,0,0};
//global
int a, b, c, ans, hp = 1;
VS mp;
int sx = INF, sy = INF;
string str = "";
char inp[1000];
vector<VVI> ar;
queue<pair<int, PII > >  n;
//function
int main(){
  scanf("%d %d %d", &a, &b, &c);
  REP(i, b + 2){
    str = str + "X";
  }
  mp.PB(str);
  REP(i, a){
    scanf("%s", inp);
    mp.PB(inp);
    mp[i + 1] = "X" + mp[i + 1] + "X";
    REP(j, mp[i + 1].size()){
      if(sx != INF){
	break;
      }
      if(mp[i + 1][j] == 'S'){
	sy = i + 1;
	sx = j;
	break;
      }
    }
  }
  ar.resize(c, VVI(a + 2, VI(b + 2)));
  REP(i, c){
    REP(j, a + 2){
      REP(k, b + 2){
	ar[i][j][k] = INF;
	if(!i && sy == j && sx == k){
	  ar[0][j][k] = 0;
	}
      }
    }
  }
  mp.PB(str);
  n.push(pair<int, PII>(hp, PII(sy, sx)));

  while(!n.empty()){
    if(n.front().F != hp){
      n.pop();
      continue;
    }
    if(hp == mp[n.front().S.F][n.front().S.S] - '0'){
      hp++;
      if(hp > c){
	ans = ar[c - 1][n.front().S.F][n.front().S.S];
	break;
      }
      ar[hp - 1][n.front().S.F][n.front().S.S] = ar[hp - 2][n.front().S.F][n.front().S.S];
    }
    int nowy = n.front().S.F, nowx = n.front().S.S;
    n.pop();
    REP(i, 4){
      if(mp[nowy + movey[i]][nowx + movex[i]] != 'X' && ar[hp - 1][nowy + movey[i]][nowx + movex[i]] > ar[hp - 1][nowy][nowx] + 1){
	ar[hp - 1][nowy + movey[i]][nowx + movex[i]] = ar[hp - 1][nowy][nowx] + 1;
	n.push(pair<int, PII>(hp, PII(nowy + movey[i], nowx + movex[i])));
      }
    }
  }
  printf("%d\n",ans);
  return 0;
}