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<queue>
#include<algorithm>
#include<string.h>
using namespace std;
struct nagase {
		int x;
		int y;
		int c;
		int r;
};
int kani[4]={0,1,-1,0};
int ebi[4]={1,0,0,-1};
int map[1001][1001]={0};
int s[1001][1001]={0};
int main(){
	queue<nagase>Q;
	nagase kotani;
	nagase yanagi;
	int h,w,n;
	cin>>h>>w>>n;
	char a;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>a;
			if(a=='S'){
				kotani.x=i;
				kotani.y=j;
				kotani.c=1;
				kotani.r=0;
				s[i][j]=1;
				Q.push(kotani);
			}
			else if(a=='X'){
				map[i][j]=-1;
			}
			else if(a=='.'){
				map[i][j]=0;
			}
			else{
				map[i][j]=(int)(a-48);
			}
		}
	}
	while(!Q.empty()){
		yanagi=Q.front();
		Q.pop();
		if(yanagi.c==map[yanagi.x][yanagi.y]){
			if(yanagi.c==n){
				cout<<yanagi.r<<endl;
				break;
			}
			while(!Q.empty()){
				Q.pop();
			}
			yanagi.c++;
			memset(s,0,sizeof(s));
			s[yanagi.x][yanagi.y]=1;
			Q.push(yanagi);
		}
		else{
			for(int i=0;i<4;i++){
					kotani.c=yanagi.c;
					kotani.x=yanagi.x+kani[i];
					kotani.y=yanagi.y+ebi[i];
					kotani.r=yanagi.r+1;
					if(kotani.x>=0&&kotani.x<h&&kotani.y>=0&&kotani.y<w&&map[kotani.x][kotani.y]!=-1&&s[kotani.x][kotani.y]!=1){
					s[kotani.x][kotani.y]=1;
					Q.push(kotani);
				}
			}
		}
	}
}

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;
short 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)continue;
      if(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});
    }
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;

int H, W, N;
#define MAX_H 1000
#define MAX_W 1000
#define MAX_N 9

char maze[MAX_H + 1][MAX_W + 1];
int sy, sx, gy, gx;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> H >> W >> N;
	int pos[10][2];
	rep(y, 0, H) rep(x, 0, W) {
		cin >> maze[y][x];
		switch (maze[y][x]) {
			case 'S' :
				pos[0][0] = x;
				pos[0][1] = y;
				break;
			case '1' :
				pos[1][0] = x;
				pos[1][1] = y;
				break;
			case '2' :
				pos[2][0] = x;
				pos[2][1] = y;
				break;
			case '3' :
				pos[3][0] = x;
				pos[3][1] = y;
				break;
			case '4' :
				pos[4][0] = x;
				pos[4][1] = y;
				break;
			case '5' :
				pos[5][0] = x;
				pos[5][1] = y;
				break;
			case '6' :
				pos[6][0] = x;
				pos[6][1] = y;
				break;
			case '7' :
				pos[7][0] = x;
				pos[7][1] = y;
				break;
			case '8' :
				pos[8][0] = x;
				pos[8][1] = y;
				break;
			case '9' :
				pos[9][0] = x;
				pos[9][1] = y;
				break;
		}
	}

	int hp = 1;

	int dx[4] = {0, -1, 1, 0};
	int dy[4] = {-1, 0, 0, 1};
	int ans = 0;
	rep(j, 0, N) {
		queue<P> q;
		sx = pos[j][0];
		sy = pos[j][1];
		gx = pos[j + 1][0];
		gy = pos[j + 1][1];
		int d[H][W];
		rep(y, 0, H) rep(x, 0, W) d[y][x] = INF;
		d[sy][sx] = 0;
		q.push(make_pair(sx, sy));
		while(q.size()) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			if(maze[y][x] == (hp - '0')) {
				hp++;
				break;
			}
			rep(i, 0, 4) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				if(0 <= nx && nx < W && 0 <= ny && ny < H && maze[ny][nx] != 'X' && d[ny][nx] == INF) {
					d[ny][nx] = d[y][x] + 1;
					q.push(make_pair(nx, ny));
				}
			}
		}
		ans += d[gy][gx];
	}
	cout << ans << endl;


}