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<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1000][1000],C[11]={"0123456789"};
int D[1000][1000];
int A[1000][1000],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
	memset(A,0,sizeof(A));
   memset(D,0,sizeof(D));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		//printf("a");
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();
		A[S.y][S.x]=1;
		S.x+=x[l];
		S.y+=y[l];
		S.Z++;
		//printf("%d\n",A[S.y][S.x]);
		if(A[S.y][S.x]==1) continue;
		//printf("b");
		if(s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
			S.x-=x[l];
			S.y-=y[l];
		}
		else if(s[S.y][S.x]=='.'){}
		else{
			if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				printf("%d\n",S.Z);
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			memset(A,0,sizeof(A));
			l=-1;
			}

		} 
		P.push(S);
	}
	return 0;
}

C++: #include <iostream>
#include <cassert>
#include <deque>
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
using namespace std;
int H,W,N;
int area[1000][1000];
int subGoal = 1;
deque<int> posX;
deque<int> posY;
deque<int> step;
void init();
void reset(int st);
int main() {
  cin >> H >> W >> N;
  init();
  int dx[4] = {1,0,-1,0};
  int dy[4] = {0,1,0,-1};
  int x,y,st;
  while(true){
    x = posX.front();
    posX.pop_front();
    y = posY.front();
    posY.pop_front();
    st = step.front();
    step.pop_front();
    rep(i,4){
      int nx = x+dx[i];
      int ny = y+dy[i];
      if(nx<0||nx>=W||ny<0||ny>=H) continue;
      if(area[nx][ny]==subGoal){
        if(subGoal==N){
          cout << st+1 << endl;
          return 0;
        }
        reset(st+1);
        break;
      }
      if(area[nx][ny]>0){
        area[nx][ny] -= 10;
        posX.push_back(nx);
        posY.push_back(ny);
        step.push_back(st+1);
      }
    }
  }
  return 0;
}

void init(){
  char input;
  rep(i,H){
    rep(j,W){
      cin >> input;
      if(input == '.'){
        area[j][i] = 10;
      }else if(input == 'X'){
        area[j][i] = -10;
      }else if(input == 'S'){
        posX.push_back(j);
        posY.push_back(i);
        step.push_back(0);
        area[j][i] = 0;
      }else{
        area[j][i] = (int)(input-'0');
      }
    }
  }
}

void reset(int st){
  posX.clear();
  posY.clear();
  step.clear();
  rep(i,H){
    rep(j,W){
      if(area[j][i] == subGoal){
        posX.push_back(j);
        posY.push_back(i);
        step.push_back(st);
      }else if(area[j][i] > -10&&area[j][i]<=0) area[j][i] += 10;
    }
  }
  subGoal++;
}

C++: #include<iostream>
#include<deque>
#include<utility>
#include<algorithm>
#include<stack>
#include<vector>

using namespace std;
vector< vector<char> > map;
 

int main(){

    int H,W,N;
    int HP=1;
    char temp;
    int dx[] = {1,-1,0,0};
    int dy[] = {0,0,1,-1};
    int count=0;
    cin >> H >> W >> N;
    vector< vector<bool> > fmap(H,vector<bool>(W));
    vector<bool> f(W);
    fill(f.begin(),f.end(),false);
    fill(fmap.begin(),fmap.end(),f);
    deque< pair<int,int> > next;
    for(int i=0;i<H;i++){
	map.push_back( vector<char>(W) );
	for(int j=0;j<W;j++){
	    cin >> temp;
	    if(temp == 'S') next.push_back(pair<int,int>(i,j));
	    map[i][j] = temp;
	}
    }
    int x,y;
    bool flag = false;
    deque< pair<int,int> > t;
    while(true){
	count++;
	while(true){
	    if(next.empty() && t.empty()) cout << HP <<endl;
	    y = next.front().first;
	    x = next.front().second;
	    next.pop_front();
	    for(int i=0;i<4;i++){
		if( dx[i]+x<0 || dx[i]+x>=W || dy[i]+y<0 || dy[i]+y>=H || map[y+dy[i]][x+dx[i]] == 'X')   continue;
		else if(HP == (int)(map[y+dy[i]][x+dx[i]] - '0') ){
		    HP++;
		    if(HP == N+1){
			flag = true;
		    }
		    t.clear();
		    next.clear();
		    t.push_back(pair<int,int>(y+dy[i],x+dx[i]));
		    fill(fmap.begin(),fmap.end(),f);
		    break;
		}
		else if(!fmap[y+dy[i]][x+dx[i]]){
		    t.push_back(pair<int,int>(y+dy[i],x+dx[i]));
		    fmap[y+dy[i]][x+dx[i]] = true;
		}

		
	    }
	    if(flag||next.empty()) break;
	}
	if(flag) break;
	swap(next,t);
    }
    cout << count << endl;
    return 0;
}

C++: //#include "stdafx.h"
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define POF pop_front
#define POB pop_back
#define MA make_pair
#define ll long long
#define VE vector<int>
#define VP vector<pair<int,int> >
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
#define YES(i) cout<<(i?"YES":"NO")<<endl
#define Yes(i) cout<<(i?"Yes":"No")<<endl
using namespace std;
//
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
//
int H, W, N;
char M[1000][1000];
pair<int, int> n[10];
bool used[1000000];
int d[4];
//
int BFS(int num) {
    int sx = n[num - 1].FI, sy = n[num - 1].SE;
    int gx = n[num].FI, gy = n[num].SE;
    deque<pair<int, int> > que;
    que.PB(MA(sx*W+sy,0));
    fill(used, used + 1000000, 0);
    used[0] = true;
    while (!que.empty()) {
        pair<int, int> P= que.front();
        que.pop_front();
        if (P.FI == gx*W+gy) {
            return P.SE;
        }
        FOR(i, 0, 4) {
            int num = P.FI + d[i];
            if (P.FI%W == 0 && i == 0) {
                continue;
            }
            else if (P.FI%W == W-1 && i == 1) {
                continue;
            }
            else if (P.FI / W == 0 && i == 2) {
                continue;
            }
            else if (P.FI / W == H - 1 && i == 3) {
                continue;
            }
            if (0 <= num/W && num/W < H && 0 <= num%W && num%W < W && !used[num]&&M[num/W][num%W]!='X') {
                que.PB(MA(num,P.SE+1));
                used[num] = true;
            }
        }
    }
}
int main() {
    cin >> H >> W >> N;
    FOR(i, 0, H) {
        FOR(j, 0, W) {
            cin >> M[i][j];
            if (M[i][j]=='S') {
                n[0] = MA(i, j);
            }
            else if ('1' <= M[i][j] && M[i][j] <= '9') {
                n[M[i][j] - '0'] = MA(i, j);
            }
        }
    }
    d[0] = -1; d[1] = 1;
    d[2] = -W; d[3] = W;
    int ans = 0;
    FOR(i, 1, N + 1) {
        ans += BFS(i);
    }
    cout << ans << endl;
    return 0;
}