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<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<string>
#include<vector>
using namespace std;
#define REP(i, b, e) for ( int i = b; i < (int)e; i++ )
#define rep(i, n) REP(i, 0, n)
static const int MAX = 1000;
static const int INFTY = (1<<20);

int H, W, N;
char G[MAX][MAX];
pair<int, int> C[11];

int bfs(int sy, int sx, int ty, int tx){
  queue<pair<int, int> > Q;
  int d[MAX][MAX];
  rep(i, H) rep(j, W) d[i][j] = INFTY;
  Q.push(make_pair(sy, sx));
  d[sy][sx] = 0;
  pair<int, int> u, v;
  static const int dy[4] = {0, -1, 0, 1};
  static const int dx[4] = {1, 0, -1, 0};
  int ny, nx;
  while(!Q.empty()){
    u = Q.front(); Q.pop();
    if ( u.first == ty && u.second == tx ) return d[u.first][u.second];
    rep(r, 4){
      ny = u.first + dy[r];
      nx = u.second + dx[r];
      if ( ny < 0 || nx < 0 || ny >= H || nx >= W ) continue;
	   if ( d[ny][nx] != INFTY ) continue;
	   if ( G[ny][nx] == 'X' ) continue;
	   d[ny][nx] = d[u.first][u.second] + 1;
	   Q.push(make_pair(ny, nx));
    }
  }
    return -1;

}
main(){
  cin >> H >> W >> N;
  rep(i, H) rep(j, W){
    cin >> G[i][j];
    if ( G[i][j] == 'S' ) C[0] = make_pair(i, j);
    if ( '1' <= G[i][j] && G[i][j] <= '9' ) C[G[i][j]-'0'] = make_pair(i, j);
  }
  int sum = 0;
  rep(i, N) {
    int tmp = bfs(C[i].first, C[i].second, C[i+1].first, C[i+1].second);
    sum += tmp;
  }
  cout << sum << endl;

}

C++: #include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 0x7fffffff
#define eps 1e-8
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI  acos(-1.0)
#define test puts("OK");
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second

char maps[1000+10][1000+10];
int dis[1000+10][1000+10];
bool vis[1000+10][1000+10];
const int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
PII fac[10];
int h,w,n;

int bfs(int sx,int sy,int ex,int ey)
{
	CLR(vis,0);CLR(dis,0);
	queue<PII> Q;
	Q.push(MP(sx,sy));
	while(!Q.empty())
	{
		PII p=Q.front();Q.pop();
		if(p.X==ex && p.Y==ey)
			break;
		vis[p.X][p.Y]=1;
		REP(i,4)
		{
			int tx=p.X+dir[i][0];
			int ty=p.Y+dir[i][1];
			if(tx>=0 && tx<h && ty>=0 && ty<w && !vis[tx][ty] && maps[tx][ty]!='X')
			{
				Q.push(MP(tx,ty));
				dis[tx][ty]=dis[p.X][p.Y]+1;
			}
		}
	}
	return dis[ex][ey];
}

int main()
{
	ios::sync_with_stdio(false);
	scanf("%d%d%d",&h,&w,&n);
	REP(i,h)
		REP(j,w)
		{
			cin>>maps[i][j];
			if(maps[i][j]=='S')
				fac[0]=MP(i,j);
			if(isdigit(maps[i][j]))
				fac[maps[i][j]-'0']=MP(i,j);
		}
	int ans=0;
	REP(i,n)
		ans+=bfs(fac[i].X,fac[i].Y,fac[i+1].X,fac[i+1].Y);
	printf("%d\n",ans);
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

string s[1000];
int h,w,n;
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};

int bfs(int x,int y,int now)
{
	int d[h][w]={};
	queue<pair<int,int>>Q;
	Q.push(make_pair(8,x*1000+y));
	while(!Q.empty())
	{
		pair<int,int>a=Q.front();Q.pop();
		int tx=a.second/1000,ty=a.second%1000;
		if(s[tx][ty]-'0'==now)
		{
			return a.first/10;
		}
		for(int r=0;r<4;r++)
		{
			if(abs(a.first%10-r)==2)continue;
			int sx=tx+dx[r];
			int sy=ty+dy[r];
			if(sx<0||sy<0||sx>=h||sy>=w||s[sx][sy]=='X'||d[sx][sy]!=0)continue;
			Q.push(make_pair((a.first/10+1)*10+r,sx*1000+sy));
			d[sx][sy]=1;
		}
	}
	return 1<<26;
}

int main()
{
	cin>>h>>w>>n;
	for(int i=0;i<h;i++)cin>>s[i];
	int c=0;
	pair<int,int> pr[9];
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			if(s[i][j]=='S')
			{
				pr[0]=make_pair(i,j);
			}
			else if(s[i][j]>='0'&&s[i][j]<='8')
			{
				pr[s[i][j]-'0']=make_pair(i,j);
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		c+=bfs(pr[i-1].first,pr[i-1].second,i);
	}
	cout<<c<<endl;
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int search(int sx, int sy, int dx, int dy, vector<vector<char> > &f, int w, int h)
{
vector<bool> visit;
queue<pair<pair<int, int>, int> > q;
visit.resize(w * h, false);
q.push_back(make_pair(make_pair(sx, sy), 0));
while(!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int cost = q.front().second;
q.pop();
if(visit[x * w + y])
continue;
visit[x * w + y] = true;
if(f[x][y] == 'X')
continue;
if(x == dx && y == dy)
return cost;
int ix[4] = {-1, 0, 0, 1};
int iy[4] = {0, -1, 1, 0};
for(int i = 0; i < 4; ++i) {
int nx = ix[i] + x;
int ny = iy[i] + y;
if(nx < 0 || ny < 0 || nx >= w || ny >= h)
continue;
q.push(make_pair(make_pair(nx, ny), cost + 1));
}
}
return -1;
}

int main() {
int h, w, n;
vector<vector<char> > f;
vector<pair<int, int> pos;
scanf("%d%d%d", &h, &w, &n);
f.resize(h);
pos.resize(n + 1);
for(int i = 0; i < h; ++i) {
char temp[1024];
scanf("%s", temp);
f[i].resize(w);
for(int j = 0; j < w; ++j) {
f[i][j] = temp[j];
if(f[i][j] == 'S')
pos[0] = make_pair(i, j);
if('0' <= f[i][j] && f[i][j] <= '9')
pos[f[i][j] - '0'] = make_pair(i, j);
}
}
int ans = 0;
for(int i = 1; i <= n; ++i) {
int sx = pos[i - 1].first;
int sy = pos[i - 1].second;
int dx = pos[i].first;
int dy = pos[i].second;
ans += search(sx, sy, dx, dy, f, w, h);
}
printf("%d\n", ans);
return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
using namespace std;

int main(){

  return 0;
}