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<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<set>
#include<map>
#define ll long long 
#define N 1005
using namespace std;
const int maxn =10000000;

typedef pair<int, int> p;   

string str[N];   //表示迷宫的字符出

int h,w,n;
int sx,sy,ex,ey;  //起始和终点坐标 
int dir[4][2]={0,1,1,0,-1,0,0,-1};  //方向 
int d[N][N];  //到达各个点的最短距离的坐标 
struct node
{
	int x,y;
};
vector<node> ve;
int bfs()
{
	queue<p> que;
	int i,j;
	for(i=0;i<h;i++)
	{
		for(j=0;j<w;j++)
		{
			d[i][j]=-1;
		}
	}
	que.push(p(sx,sy));
	d[sx][sy]=0;
	while(que.size())
	{
		p temp=que.front();
		que.pop();
		if(temp.first==ex&&temp.second==ey)
		break;
		for(i=0;i<4;i++)
		{
			int dx=temp.first+dir[i][0];
			int dy=temp.second+dir[i][1];
			if(dx>=0&&dy>=0&&dx<h&&dy<w&&str[dx][dy]!='X'&&d[dx][dy]==-1)
			{
				que.push(p(dx,dy));
				d[dx][dy]=d[temp.first][temp.second]+1;
				if(dx==ex&&dy==ey)
				return d[dx][dy];
			} 
		}
	}
} 
void find(char x,char y)
{
	int i,j;
	for(i=0;i<h;i++)
	{
		for(j=0;j<w;j++)
		{
			if(str[i][j]==x)
			{
				sx=i;
				sy=j;
			}
			if(str[i][j]==y)
			{
				ex=i;
				ey=j;
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//让cin，cout变得快一点
	cin>>h>>w>>n;
	int i,j;
	int ans=0;
	for(i=0;i<h;i++)
	{
		cin>>str[i];
	}
	for(i=0;i<n;i++)
	{
		if(i==0)
		{
			find('S','1');
			ans+=bfs();
		}
		else
		{
			char x='0'+i;
			char y='0'+i+1;
			find(x,y);
			ans+=bfs();
		}
	}
	cout<<ans<<endl;
	return 0;   
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <map>
#include <stdlib.h>

#define rep(i, a, b) for(int i = (a); (i) < (b); (i)++)
#define ft first
#define sd second

using namespace std;

char m[1000][1000];
int dis[1000][1000];

int main() {
  int h, w, n;

  cin >> h >> w >> n;

  int sx, sy;
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> m[j][i];
      if(m[j][i] == 'S') {
	sx = j;
	sy = i;
      }
    }
  }

  pair<int, int> p;
  p.ft = sx;
  p.sd = sy;

  queue<pair<int, int> > state;
  rep(i, 0, h) {
    rep(j, 0, w) {
      dis[j][i] = -1;
    }
  }
  dis[sx][sy] = 0;
  state.push(p);

  int dx[4] = {-1,   0,  1, 0};
  int dy[4] = { 0,  -1,  0, 1};
  for(int lp = 1; lp <= n; lp ++) {
    int map2[1000][1000] = {0};
    while(state.size()) {

      pair<int, int> now = state.front();
      state.pop();
      map2[now.ft][now.sd] = 1;
      if(m[now.ft][now.sd]-48 == lp) {
	sx = now.ft;
	sy = now.sd;
	break;
      }
      
      rep(lp2, 0, 4) {
	int x = now.ft+dx[lp2];
	int y = now.sd+dy[lp2];
	
	if(x < 0 || w <= x || y < 0 || h <= y) continue;
	if(map2[x][y] == 0 && m[x][y] != 'X') {
	  state.push(make_pair(x, y));
	  dis[x][y] = dis[now.ft][now.sd] + 1;
	}
      }
    }
    while(state.size()) state.pop();

    state.push(make_pair(sx, sy));
  }

  cout << dis[sx][sy] << endl;
  return 0;
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))

int w,h,n,x[10],y[10];
const int inf = 20000;
char s[1000][1001];
short m[1000][1000];
int f(int c){
	int a=x[c],b=y[c];
	int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
	queue<pii> q;
	rep(i,h)rep(j,w)m[i][j]=inf;
	m[b][a]=0;
	q.push(pii(b,a));
	while(!q.empty()){
		int tx=q.front().second,ty=q.front().first;
		q.pop();
		rep(i,4){
			int cx=tx+dx[i],cy=ty+dy[i];
			if(in(cx,0,w-1) && in(cy,0,h-1) && s[cy][cx]!='X'){
				if(m[cy][cx]>m[ty][tx]+1){
					m[cy][cx]=m[ty][tx]+1;
					q.push(pii(cy,cx));
				}
			}
		}
	}
	return m[y[c+1]][x[c+1]];
}
int main(){
	int cnt;
	scanf("%d%d%d",&h,&w,&n);
	rep(i,h){
		scanf("%s",s[i]);
		rep(j,w){
			if(s[i][j]=='S'){
				x[0]=j;
				y[0]=i;
			}else if(in(s[i][j],'1','9')){
				int c=s[i][j]-'0';
				x[c]=j;
				y[c]=i;
			}
		}
	}
	cnt=0;
	rep(i,n)cnt+=f(i);
	printf("%d\n",cnt);
	return 0;
}

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