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>
using namespace std;
bool a[1000][1000];
int f[1000][1000];
int sum = 0, sum3=10000000;
bool sum2 = false;
void solve(int i, int j,int mi,int mj,int h,int w) {
	if (i == mi&&j == mj&&sum3>f[i][j]) {
		sum3 = f[i][j];
		sum2 = true;
		return;
	}
	if (a[i][j + 1] && f[i][j] + 1 < f[i][j + 1] && j + 1 < w) {
		f[i][j + 1] = f[i][j] + 1;
		solve(i, j + 1, mi, mj, h, w);
	}
	if (a[i][j - 1] && f[i][j] + 1 < f[i][j - 1] && j - 1 >= 0) {
		f[i][j - 1] = f[i][j] + 1;
		solve(i, j - 1, mi, mj, h, w);
	}
	if (a[i + 1][j] && f[i][j] + 1 < f[i + 1][j] && i + 1 < h) {
		f[i + 1][j] = f[i][j] + 1;
		solve(i + 1, j, mi, mj, h, w);
	}
	if (a[i - 1][j] && f[i][j] + 1 < f[i - 1][j] && i - 1 >= 0) {
		f[i - 1][j] = f[i][j] + 1;
		solve(i - 1, j, mi, mj, h, w);
	}
}
int main() {
	int h, w, n;
	cin >> h >> w >> n;
	int b[10][2], c;
	string d;
	for (int i = 0; i < h; i++) {
		cin >> d;
		for (int j = 0; j < w; j++) {
			if (d[j] == 'X')
				a[i][j] = false;
			else
				a[i][j] = true;
			if (d[j] >= '1'&&d[j] <= '9') {
				int e = (int)d[j] - '0';
				b[e][0] = i;
				b[e][1] = j;
			}
			if (d[j] == 'S') {
				b[0][0] = i;
				b[0][1] = j;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < h; j++)
			fill(f[j], f[j] + w, 1000001);
		sum2 = false;
		f[b[i][0]][b[i][1]] = 0;
		solve(b[i][0], b[i][1], b[i + 1][0], b[i + 1][1], h, w);
		sum += f[b[i + 1][0]][b[i + 1][1]];
	}
	cout << sum << endl;
}

C++: #include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
//#include <functional>
#include <cstdio>
#include <queue>
#include <fstream>
using namespace std;

#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'

struct Point
{
	int x;
	int y;
};

int dx[] = {0,-1,0,1};//ã©çvñè
int dy[] = {1,0,-1,0};
string field[1000];
int mfield[1001][1001] = {};
int life=1;
int csum = 0;
int H,W,N;


int open(Point gool)
{
	int counter=0;
	queue<Point> tp;
	tp.push(gool);
	int i,j;
	Point inp;
	Point p;
	while(1)
	{
		counter++;
		int size = tp.size();
		for(i=0;i<size;i++)
		{
			p = tp.front();
			tp.pop();
			for(j=0;j<4;j++)
			{
				inp.x = p.x+dx[j];
				inp.y = p.y+dy[j];
				if(inp.x<0 || inp.y<0 || inp.y>=H || inp.x>=W)
					continue;
				if(field[inp.y][inp.x] != CANTGO && mfield[inp.y][inp.x] != life)
				{
					//v·éÉÅZ£Ú®Â\][
					if(field[inp.y][inp.x] == life + '0' ) //©ªÉ©Á½`[YÌ­©
					{
						return counter;
					}
					else
					{
						tp.push(inp);
						mfield[inp.y][inp.x] = life;
					}
				}
			}
		}
		/*for(i=0;i<H;i++)
		{
			for(j=0;j<W;j++)
			{
				cout << mfield[i][j];
			}
			cout << endl;
		}*/
	}
	//copy(tp.begin() , tp.end() , tp.begin());
	return 0;
}

int main(void)
{
	int i,j;
	queue<Point> tp;
	Point top[10];
	Point inp;
	int count = 0;
	int counter=0;
	cin >> H >> W >> N;

	for(i=0;i<H;i++)//üÍ
	{
		cin >> field[i];
		for(j=0;j<W;j++)
		{
			if(field[i][j] == 'S')
			{
				top[0].y = i;
				top[0].x = j;
				mfield[j][i] = life;
			}
			if(field[i][j] >= '0' && field[i][j] <= '9')
			{
				top[field[i][j] - '0'].y = i;
				top[field[i][j] - '0'].x = j;
			}
		}
	}//üÍIíè

	while(1)
	{
		if((counter = open(top[life-1])) != 0)
		{
			life++;
			csum += counter;
		}
		if(life > N)
			break;
	}
		cout << csum << endl;

	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

typedef struct{
  short x,y;
  int cost;
  short ch;
}mouse;


mouse t;
int cx,cy,nx,ny,ch,nch,h,w,n;
int cost;

main(){
  scanf("%d%d%d",&h,&w,&n);
  short maxch[h][w];
  char ku[h][w];
  
  rep(i,h)scanf("%s",ku[i]);


  rep(i,h)rep(j,w){
    if(ku[i][j]=='S')cx=i,cy=j;
    maxch[i][j]=-1;
  }

  queue<mouse> Q;

  Q.push((mouse){cx,cy,0,0});
  maxch[cx][cy]=0;

  while(!Q.empty()){
    //cout<<Q.size()<<endl;
    t=Q.front();Q.pop();
    cx=t.x,cy=t.y,cost=t.cost;
    ch=t.ch;

    rep(i,4){
      nx=cx+dx[i],ny=cy+dy[i];
      if(nx<0 || nx>=h || ny<0 || ny>=w || ku[nx][ny]=='X')continue;
	 nch=ch;
	 if(ku[nx][ny]<='9' && ku[nx][ny]>='1' && ku[nx][ny]-'0'==t.ch+1){
	   nch=ch+1;
	 }

	 if(maxch[nx][ny]>=nch)continue;
	 if(nch==n){
	   printf("%d\n",cost+1);
	   return 0;
	 }
	 maxch[nx][ny]=nch;
	 Q.push((mouse){nx,ny,cost+1,nch});
    }
  }
}