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++: /*
 * main.cpp
 *
 *  Created on: 2015年10月21日
 *      Author: chen
 */
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<memory.h>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff                                          //INT_MAX
#define inf 0x3f3f3f3f                                          //int??????????????????
#define FOR(i,a) for((i)=0;i<(a);(i)++)                          //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;
template<class T>
T Mint(T a, T b, T c) {
	if (a>b) {
		if (c>b)
			return b;
		return c;
	}
	if (c>a)
		return a;
	return c;
}
template<class T>
T Maxt(T a, T b, T c) {
	if (a>b) {
		if (c>a)
			return c;
		return a;
	}
	else if (c > b)
		return c;
	return b;
}

const int maxn=1010;
int T,n,m;
int h,w;
char map[maxn][maxn];
bool vis[maxn][maxn];
int sx,sy,ex,ey;
int dx[]={-1,1,0,0};
int dy[]={0,0,1,-1};

struct node{
	int x,y;
	int t;
}aim[15];

bool judge(int x,int y){
	if(x<0||x>h||y<0||y>w)	return false;
	if(map[x][y]=='X') return false;
	if(vis[x][y]) return false;
	return true;
}

int bfs(){
	MEM1(vis);
	queue<node>q;
	node s,e;
	s.x=sx,s.y=sy,s.t=0;
	vis[s.x][s.y]=1;
	q.push(s);
	while(!q.empty()){
		s=q.front();
		//cout<<s.t<<endl;
		q.pop();
		if(s.x==ex&&s.y==ey){
			return s.t;
		}
		for(int i=0;i<4;i++){
			e.x=s.x+dx[i];
			e.y=s.y+dy[i];
			e.t=s.t+1;
			if(!judge(e.x,e.y))
				continue;
			q.push(e);
			vis[e.x][e.y]=1;
		}
	}
	return 0;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
#endif
	while(~sft(h,w,n)){
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>map[i][j];
				if(map[i][j]=='S'){
					sx=i;
					sy=j;
				}
				if(isdigit(map[i][j])){
					aim[map[i][j]-'0'].x=i;
					aim[map[i][j]-'0'].y=j;
				}
			}
		}
		aim[0].x=sx;aim[0].y=sy;
		int step=0;
		for(int i=0;i<n;i++){
			sx=aim[i].x;sy=aim[i].y;
			ex=aim[i+1].x;ey=aim[i+1].y;
			step+=bfs();
		}
		printf("%d\n",step);
	}
	return 0;
}

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

#define MAX_SIZE 1000
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

char M[MAX_SIZE][MAX_SIZE];
int H, W;

void bfs(int &sy, int &sx, int &ans, const char g) {
  int D[MAX_SIZE][MAX_SIZE];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      D[i][j] = -1;
    }
  }

  queue<P> que;
  que.push(P(sy, sx));
  D[sy][sx] = 0;
  while (que.size()) {
    P p = que.front(); que.pop();
    if (M[p.first][p.second] == g) {
      sy = p.first;
      sx = p.second;
      ans += D[p.first][p.second];
      break;
    }

    for (int i = 0; i < 4; ++i) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (0 <= ny && ny < H &&
          0 <= nx && nx < W &&
          M[ny][nx] != 'X' &&
          D[ny][nx] == -1) {
        que.push(P(ny, nx));
        D[ny][nx] = D[p.first][p.second] + 1;
      }
    }
  }
}

int main() {
  int N;
  cin >> H >> W >> N;

  int sy, sx;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> M[i][j];
      if (M[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < N; ++i) {
    bfs(sy, sx, ans, i + 1 + '0');
  }

  cout << ans << endl;

  return 0;
}

C++: #include <stdio.h>
#include <queue>

using namespace std;

const int INF = 10000000;
char maze[1005][1005];
int d[1005][1005];
int w, h;

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

struct Coor {
	int x, y;
	Coor(int x, int y) : x(x), y(y) {}
};

queue<Coor> Q;

int bfs(int sx, int sy, int gx, int gy)
{
	int i, j;
	int nx, ny;
	
	while (Q.size() != 0){
		Q.pop();
	}
	
	for (i = 0; i < h; i++){
		for (j = 0; j < w; j++){
			d[i][j] = INF;
		}
	}
	
	d[sy][sx] = 0;
	
	Q.push(Coor(sx, sy));
	
	while (Q.size() != 0){
		
		Coor p = Q.front();
		Q.pop();
		
		if (p.x == gx && p.y == gy){
			break;
		}
		
		for (i = 0; i < 4; i++){
			
			nx = p.x + dx[i];
			ny = p.y + dy[i];
			
			if (nx >= 0 && ny >= 0 && nx < w && ny < h && maze[ny][nx] != 'X' && d[ny][nx] == INF){
				
				Q.push(Coor(nx, ny));
				d[ny][nx] = d[p.y][p.x] + 1;
			}
		}
	}
	return (d[gy][gx]);
}
			

int main(void)
{
	int n, ans = 0;
	int sx, sy;
	int x, y;
	int i, j;
	int gx[9], gy[9];
	
	scanf("%d %d %d", &h, &w, &n);
	
	for (i = 0; i < h; i++){
		for (j = 0; j < w; j++){
			scanf(" %c", &maze[i][j]);
			
			if (maze[i][j] == 'S'){
				sx = j;
				sy = i;
			}
			else if (maze[i][j] == '1'){
				gx[0] = j;
				gy[0] = i;
			}
			else if (maze[i][j] == '2'){
				gx[1] = j;
				gy[1] = i;
			}
			else if (maze[i][j] == '3'){
				gx[2] = j;
				gy[2] = i;
			}
			else if (maze[i][j] == '4'){
				gx[3] = j;
				gy[3] = i;
			}
			else if (maze[i][j] == '5'){
				gx[4] = j;
				gy[4] = i;
			}
			else if (maze[i][j] == '6'){
				gx[5] = j;
				gy[5] = i;
			}
			else if (maze[i][j] == '7'){
				gx[6] = j;
				gy[6] = i;
			}
			else if (maze[i][j] == '8'){
				gx[7] = j;
				gy[7] = i;
			}
			else if (maze[i][j] == '9'){
				gx[8] = j;
				gy[8] = i;
			}
		}
	}
	
	ans += bfs(sx, sy, gx[0], gy[0]);
	
	for (i = 1; i < n; i++){
		ans += bfs(gx[i - 1], gy[i -  1], gx[i], gy[i]);
	}
	
	printf("%d\n", ans);
	
	return (0);
}