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<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int mp[1002][1002],mp2[1002][1002];
int main()
{
	char a;
	int t, j, h, w, k, i;
	pair<int, int> s, m[10];
	queue<pair<int, int> > u;
	queue<int> e;
	cin >> h >> w >> k;
	for (i = 0; i <= h; i++)
	{
		mp[i][0] = 1;
		mp[i][w + 1] = 1;
		mp2[i][0] = 1;
		mp2[i][w + 1] = 1;

	}
	for (i = 0; i <= w; i++)
	{
		mp[0][i] = 1;
		mp[h + 1][i] = 1;
		mp2[0][i] = 1;
		mp2[h + 1][i] = 1;
	}
	for (i = 1; i <= h; i++)
	{
		for (j = 1; j <= w; j++)
		{
			cin >> a;
			if (a == 'X')
			{
				mp[i][j] = 1;
			}
			if (a == 'S')
			{
				mp[i][j] = 10;
				s.first = i;
				s.second = j;
			}
			if (a == '1')
			{
				mp[i][j] = 11;
				m[1].first = i;
				m[1].second = j;
			}
			if (a == '2')
			{
				mp[i][j] = 12;
				m[2].first = i;
				m[2].second = j;
			}
			if (a == '3')
			{
				mp[i][j] = 13;
				m[3].first = i;
				m[3].second = j;
			}
			if (a == '4')
			{
				mp[i][j] = 14;
				m[4].first = i;
				m[4].second = j;
			}
			if (a == '5')
			{
				mp[i][j] = 15;
				m[5].first = i;
				m[5].second = j;
			}
			if (a == '6')
			{
				mp[i][j] = 16;
				m[6].first = i;
				m[6].second = j;
			}
			if (a == '7')
			{
				mp[i][j] = 17;
				m[7].first = i;
				m[7].second = j;
			}
			if (a == '8')
			{
				mp[i][j] = 18;
				m[8].first = i;
				m[8].second = j;
			}
			if (a == '9')
			{
				mp[i][j] = 19;
				m[9].first = i;
				m[9].second = j;
			}
		}
	}
	t = 0;
	e.push(0);
	i = 1;
	u.push(s);
	while (i <= k)
	{
		t = e.front();
		e.pop();
		s = u.front();
		u.pop();
		if (s == m[i])
		{
			i++;
			memset(mp2, 0, sizeof(mp2));
			while (!u.empty())
			{
				u.pop();
			}
			while (!e.empty())
			{
				e.pop();
			}
			e.push(t);
			u.push(s);
			continue;
		}
		if (mp[s.first + 1][s.second] != 1 && mp2[s.first + 1][s.second] != 1)
		{
			mp2[s.first + 1][s.second] = 1;
			s.first++;
			u.push(s);
			t++;
			e.push(t);
			t--;
			s.first--;
		}
		if (mp[s.first][s.second + 1] != 1 && mp2[s.first][s.second + 1] != 1)
		{
			mp2[s.first][s.second + 1] = 1;
			s.second++;
			u.push(s);
			t++;
			e.push(t);
			t--;
			s.second--;
		}
		if (mp[s.first - 1][s.second] != 1 && mp2[s.first - 1][s.second] != 1)
		{
			mp2[s.first - 1][s.second] = 1;
			s.first--;
			u.push(s);
			t++;
			e.push(t);
			t--;
			s.first++;
		}
		if (mp[s.first][s.second - 1] != 1 && mp2[s.first][s.second - 1] != 1)
		{
			mp2[s.first][s.second - 1] = 1;
			s.second--;
			u.push(s);
			t++;
			e.push(t);
			t--;
			s.second++;
		}
	}
	cout << t << endl;
	return 0;
}

C++: // g++ -std=c++11 temp.cpp
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define EACH(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;

#define INF INT_MAX/3

#define MAX_N 1000

int h,w,n;
string s[1000];
int sx,sy;
int d[1000][1000];

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

struct state{
	int x,y,level;
};

int main(){
	cin>>h>>w>>n;
	REP(i,h){
		cin>>s[i];
		REP(j,w){
			if(s[i][j]=='S'){
				sx = j; sy = i;
			}
		}
	}
	queue<state> que;
	que.push((state){sx,sy,1});
	REP(i,h)REP(j,w)d[i][j]=INF;
	d[sy][sx]=0;
	while(!que.empty()){
		state st = que.front(); que.pop();
		if(st.level==n+1){
			cout<<d[st.y][st.x]<<endl;
			break;
		}
		if(st.level+'0'==s[st.y][st.x]){
			int nt = d[st.y][st.x];
			queue<state>().swap(que);
			que.push((state){st.x,st.y,st.level+1});
			REP(i,h)REP(j,w)d[i][j]=INF;
			d[st.y][st.x]=nt;
			//dbg(nt);
			continue;
		}
		REP(i,4){
			int nx = st.x+dx[i], ny=st.y+dy[i];
			if(nx>=0&&nx<w&&ny>=0&&ny<h&&s[ny][nx]!='X'&&d[ny][nx]==INF){
				que.push((state){nx,ny,st.level});
				d[ny][nx]=d[st.y][st.x]+1;
				//dbg(d[ny][nx]);
				//dbg(nx);dbg(ny);
			}
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#define INF (1<<30)
using namespace std;

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

bool canmove(int x,int y){
  return (0 <= x && x < w && 0 <= y && y < h);  
}

int main(){
  scanf("%d %d %d",&h,&w,&n);
  (void)getchar();
  vector<int> data(n+1);
  vector<vector<char> > map(h,vector<char>(w));

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      scanf("%c",&map[i][j]);
      if(map[i][j]=='S') map[i][j]='0';
      if('0' <= map[i][j] && map[i][j] <= '9') data[map[i][j]-'0']=i*2048+j;
    }
    (void)getchar();
  }
  
  vector<vector<int> > cost(n+1,vector<int>(n+1,0));
  for(int i=0;i<=n;i++){
    queue<pair<int,int> > q;
    pair<int,int> pii;
    vector<vector<int> > temp(h,vector<int>(w,-1));
    q.push(make_pair(data[i],0));
    while(!q.empty()){
      pii=q.front();
      int y=pii.first/2048;
      int x=pii.first%2048;
      q.pop();
      if(temp[y][x] != -1) continue;
      temp[y][x]=pii.second;

      for(int k=0;k<4;k++){
	if(canmove(x+dx[k],y+dy[k]) && map[y+dy[k]][x+dx[k]]!='X'){
	  q.push(make_pair((y+dy[k])*2048+x+dx[k],pii.second+1));
	}
      }
    }
    for(int j=0;j<=n;j++){
      cost[i][j]=temp[data[j]/2048][data[j]%2048];
    }
  }

  int _m=0;
  for(int i=0;i<n;i++){
    _m += cost[i+1][i];
  }
  printf("%d\n",_m);
  return 0;
}