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<cstring>
#include<cmath>
#include<cctype>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

const int inf = 0x7fffffff;
const int maxn = 1000 + 5;
char maze[maxn][maxn];
int d[maxn][maxn];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int w, h, n;
struct Node {
	int x;
	int y;
} node[10 + 5];

int bfs(Node a, Node b) {
	queue<Node> que;
	for(int i = 0; i < h; i++)
		for(int j = 0; j < w; j++)
			d[i][j] = inf;
	d[a.x][a.y] = 0;
	que.push(a);
	while(!que.empty()) {
		Node pre = que.front();
		que.pop();
		if(pre.x == b.x && pre.y == b.y)
			break;
		for(int i = 0; i < 4; i++) {
			Node now;
			now.x = pre.x + dx[i];
			now.y = pre.y + dy[i];
			if(0 <= now.x && now.x < h && 0 <= now.y && now.y < w && maze[now.x][now.y] != 'X' && d[now.x][now.y] == inf) {
				que.push(now);
				d[now.x][now.y] = d[pre.x][pre.y] + 1;
			}
		}
	}
	return d[b.x][b.y];
}
int main() {
//ios::sync_with_stdio(false);
#ifdef LOCAL
freopen("lll.in", "r", stdin);
//freopen(".out", "w", stdout);
#endif
	cin >> h >> w >> n;
	memset(maze, 0, sizeof maze);
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			cin >> maze[i][j];
			if(maze[i][j] == 'S') {
				node[0].x = i;
				node[0].y = j;
			}
			if(isdigit(maze[i][j])) {
				//cout << maze[i][j] << endl;
				//cout << i << " " << j << endl;
			node[maze[i][j] - '0'].x = i;
			node[maze[i][j] - '0'].y = j;
			}
		}
	}
	long long ans = 0;
	for(int i = 0; i < n; i++) {
		ans += bfs(node[i], node[i + 1]);
	}
	cout << ans << endl;
}

C++: #include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;


int H, W, N;
std::string field[1123];

int main()
{
	in >> H >> W >> N;
	rep(i, H) { in >> field[i]; }

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)

typedef pair<int, int> P;

int H, W, N;

char v[1001][1001];
bool visit[1001][1001];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int bfs(P s, P g){
	memset(visit, 0, sizeof(visit));
	queue<pair<int, P> > q;
	q.push(make_pair(0, s));
	int res = 0;
	while(!q.empty()){
		P p = q.front().second;
		int d = q.front().first;
		q.pop();

		if(visit[p.second][p.first]) continue;
		visit[p.second][p.first] = true;
		if(p == g) return d;

		rep(i, 4){
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
			if(v[ny][nx] == 'X') continue;
			q.push(make_pair(d+1, make_pair(nx, ny)));
		}
	}

	return -1;
}

int main(){
	cin >> H >> W >> N;
	vector<P> pos(10);
	rep(i, H) rep(j, W){
		cin >> v[i][j];
		if(v[i][j] == 'S') pos[0] = make_pair(j, i);
		for(int k = '1'; k <= '9'; k++){
			if(v[i][j] == k) pos[k-'0'] = make_pair(j, i);
		}
	}
	int ans = 0;
	rep(i, N){
		ans += bfs(pos[i], pos[i+1]);
	}
	cout << ans << endl;

	return 0;
}

C++: //#include "stdafx.h"
#include <iostream>
#include <queue>
#include <cstring>
#define INF 1000000000
using namespace std;

int row, col, n;

typedef struct point {
	int x, y;
};

char map[1000][1000];
int d[1000][1000];
int direct[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };

int BFS(int &startX, int &startY, char target) {
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			d[i][j] = INF;

	queue<point> q;
	point p;
	p.x = startX;
	p.y = startY;
	d[startX][startY] = 0;

	q.push(p);
	while (!q.empty()) {
		int nowX = q.front().x,nowY = q.front().y;
		q.pop();

		if (map[nowX][nowY] == target) {
			startX = nowX;
			startY = nowY;
			return d[nowX][nowY];
		}

		int nextX, nextY;
		for (int i = 0; i < 4; i++) {
			nextX = nowX + direct[i][0];
			nextY = nowY + direct[i][1];

			if (nextX >= 0 && nextY >= 0 && nextX < row && nextY < col && 
					map[nextX][nextY] != 'X' && d[nextX][nextY] == INF) {
				d[nextX][nextY] = d[nowX][nowY] + 1;
				p.x = nextX;
				p.y = nextY;
				q.push(p);
			}
		}
	}
	return -1;
}

int main() {
	int startX, startY, sum = 0;

	cin >> row >> col >> n;
	for (int i = 0; i < row; i++) {
		cin >> map[i];
		for (int j = 0; j < col; j++) {
			if (map[i][j] == 'S') {
				startX = i;
				startY = j;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		int dis = BFS(startX, startY, i + '1');

		if (dis == -1)cout << "error" << endl;
		else sum += dis;
	}
	cout << sum << endl;
}

C++: #include <iostream>
#include <queue>
#include <cstdlib>
#define INF 1000000
using namespace std;
typedef pair<int,int> P;
int H,W,N;
char field[1000][1000];
int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
int d[1000][1000];
int ans=0;

void bfs(int sx,int sy,int n) {
  int x,y;
  queue<P> que;
  for(int i=0;i<H;i++) {
    for(int j=0;j<W;j++) {
      d[i][j] = INF;
    }
  }
  que.push(P(sx,sy));
  d[sx][sy] = 0;

  while(!que.empty()) {
    P p = que.front();que.pop();
    x = p.first;y = p.second;
    if(field[x][y] == n+'1') break; //???
    for(int i=0;i<4;i++) {
      int nx = x+dx[i],ny = y+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[x][y]+1;
      }
    }
  }

  ans += d[x][y];
  if(n < N-1)
    bfs(x,y,n+1);
  else
    return;
}

void solve() {
  for(int i=0;i<H;i++) {
    for(int j=0;j<W;j++) {
      if(field[i][j] == 'S') {
	bfs(i,j,0);
	cout << ans << endl;
	return;
      }
    }
  }
}

int main() {
  string str;
  cin >> H >> W >> N;
  getline(cin,str);
  for(int i=0;i<H;i++) {
    getline(cin,str);
    for(int j=0;j<W;j++) {
      field[i][j] = *str.substr(j,1).c_str();
    }
  }
  solve();
}