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 <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

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


int main(void)
{
	ios::sync_with_stdio(false);
	int h, w, n;
	while(cin >> h >> w >> n && n)
	{
		SArray stage(h);
		PArray cheese(n + 1);
		for (int i = 0; i < h; i++)
		{
			cin >> stage[i];
			for (int j = 0; j < w; j++)
			{
				char c = stage[i][j];
				if(c == 'S')
				{
					cheese[0] = P(i, j);
				}
				if(inRange(10, c - '0'))
				{
					cheese[c - '0'] = P(i, j);
				}
			}
		}

		int ans = 0;
		for (int i = 0; i < n; i++)
		{
			P start = cheese[i];
			P goal = cheese[i + 1];

			vector<vector<bool>> visited(h, vector<bool>(w));
			queue<IP> que;
			que.push(IP(0, start));
			while (!que.empty())
			{
				IP ip = que.front(); que.pop();
				P p = ip.second;
				visited[p.first][p.second] = true;

				if(p == goal)
				{
					ans += ip.first;
					break;
				}

				for (int j = 0; j < 4; j++)
				{
					int nr = p.first + dr[j], nc = p.second + dc[j];
					if(!inRange(h, nr) || !inRange(w, nc) || visited[nr][nc]) continue;

					que.push(IP(ip.first + 1, P(nr, nc)));
				}
			}
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long  cheese[1001][1001];
long long  check[1001][1001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		if(check[y.a][y.b]==1)continue;
		check[y.a][y.b]=1;
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&check[y.a+q[i]][y.b+p[i]]!=1&&cheese[y.a][y.b]!=-1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1010;
const int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int H,W,N,sx,sy;
char maze[maxn][maxn];
bool visit[maxn][maxn];
char target = '1';
struct node{
	int x,y;
	int step;
};
void bfs(int x,int y){
	node p,q;
	queue<node> que;
	p.x = x;p.y = y;
	p.step = 0;
	que.push(p);
	memset(visit,false,sizeof(visit));
	visit[x][y] = true;
	while(!que.empty()){
		p = que.front();que.pop();
		if(maze[p.x][p.y]==target){
			if(target==N) {
				printf("%d\n",p.step);
				return;
			}
			else{
				target += 1;
				while(!que.empty()) que.pop();
				memset(visit,false,sizeof(visit));
			}
		}
		for(int i=0;i<4;i++){
			q.x = p.x + dx[i];
			q.y = p.y + dy[i];
			if(q.x>=0&&q.x<H&&q.y>=0&&q.y<W&&maze[q.x][q.y]!='X'&&!visit[q.x][q.y]){
				visit[q.x][q.y] = true;
				q.step = p.step + 1;
				que.push(q);
			}
		}
	}
}
int main(){
	scanf("%d %d %c",&H,&W,&N);
	getchar();
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			scanf("%c",&maze[i][j]);
			if(maze[i][j]=='S'){
				sx = i;sy = j;
			}
		}
		getchar();
	}
	bfs(sx,sy);
}

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

int h, w, n;
const int MAX_N = 1000;
char map[MAX_N][MAX_N];
struct point{
	int x, y;
	int step;
	point(){
		x = 0; y = 0; step = 0;
	}
	point(int xx, int yy, int ss){
		x = xx;
		y = yy;
		step = ss;
	}
};
struct point p[10];
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };

int ok(int x, int y){
	if (x >= 0 && x<h && y >= 0 && y<w) return 1;
	return 0;
}

int bfs(int next){
	int nextcheese = next + 1; int res = 0;

	queue<point> q;
	point cur(p[next].x, p[next].y, 0);
	q.push(cur);
	while (q.size()){
		cur = q.front(); q.pop();
		int x = cur.x, y = cur.y;
		int nx, ny;
		for (int i = 0; i<4; i++){
			nx = x + dx[i]; ny = y + dy[i];
			if (ok(nx, ny)){
				if ((map[nx][ny] - '0') == nextcheese) return ++cur.step;
				if (map[nx][ny] == '.'){
					point temp(nx, ny, cur.step + 1);
					q.push(temp);
				}
			}
		}
	}
}

void solve(){
	int sx, sy;
	scanf("%d%d%d", &h, &w, &n);
	for (int i = 0; i<h; i++){
		for (int j = 0; j<w; j++){
			char ch;
			cin >> ch; map[i][j] = ch;
			switch (ch){
			case '.': break;
			case 'X': break;
			case 'S': p[0].x = i; p[0].y = j; break;
			default:
				int t = ch - '0';
				p[t].x = i; p[t].y = j;
			}
		}
	}
	int step = 0;
	for (int i = 0; i<n; i++){
		step += bfs(i);
	}
	printf("%d", step);
}


int main(){
	solve();
}