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() {
	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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
//#include <map>
#include <set>
#define INF (2000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef pair<int, int> P;


typedef struct
{
	int i;
	int j;
	int hp;
} STATE;

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

char map[1024][1024];
int H, W, N;
int si, sj, gi, gj;
int d[1024][1024][10];

int search()
{
	queue<STATE> que;
	
	for (int i = 1; i <= H; i++){
		for (int j = 1; j <= W; j++){
			for (int k = 0; k <= N; k++){
				d[i][j][k] = INF;
			}
		}
	}
	
	STATE tmp = {si, sj, 0};
	que.push(tmp);
	d[si][sj][0] = 0;
	
	while (que.size()){
		STATE st = que.front();
		que.pop();
		
//		printf("%d %d %d\n", st.i, st.j, st.hp);
		
//		if (st.hp == N) return (d[st.i][st.j][st.hp]);
		if (st.hp == N) break;
				
		for (int i = 0; i < 4; i++){
			int ni = st.i + dy[i];
			int nj = st.j + dx[i];
			int nhp = st.hp + (st.hp == map[ni][nj]);
			
			if (map[ni][nj] != 'X' && d[ni][nj][nhp] == INF){
				STATE tmp = {ni, nj, nhp};
				que.push(tmp);
				d[ni][nj][nhp] = d[st.i][st.j][st.hp] + 1;
			}
		}
	}
	
	return (d[gi][gj][N]);
}

int main()
{
	for (int i = 0; i < 1024; i++){
		for (int j = 0; j < 1024; j++){
			map[i][j] = 'X';
		}
	}
	
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 1; i <= H; i++){
		for (int j = 1; j <= W; j++){
			scanf(" %c", &map[i][j]);
			
			if (map[i][j] == 'S'){
				si = i;
				sj = j;
			}
			if (map[i][j] == '0' + N){
				gi = i;
				gj = j;
			}
			if (map[i][j] >= '1' && map[i][j] <= '9'){
				map[i][j] -= '1';
			}
		}
	}
	
	printf("%d\n", search());
}