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 <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;
const int MAX=(1<<15)-1;
mouse Q[MAX+1];
int s,num;
 
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)fread(ku[i],1,w+1,stdin);
 
 
  rep(i,h)rep(j,w){
    if(ku[i][j]=='S')cx=i,cy=j;
    maxch[i][j]=-1;
  }
 
 
  Q[s+num++]=(mouse){cx,cy,0,0};
  maxch[cx][cy]=0;
 
  while(true){
    t=Q[s++&MAX];
    num--;
    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[s+num++&MAX]=(mouse){nx,ny,cost+1,nch};
    }
  }
}

C++: 
#include <iostream>
#include <vector>
#include <string>
#include <queue>

using namespace std;

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

vector< string > field;
int x, y, d = 0;

void bfs(int H, int W, int n ) {

	vector< vector< int > > visited( H, vector< int >( W, 0 ) );

	int gx, gy;
	for( int i = 0; i < H; i++ ) {
		for( int j = 0; j < W; j++ ) {
			if( field[i][j] == n + '0' ) {
				gy = i;
				gx = j;
			}
		}
	}

	queue< state > q;
	state s = { y, x, 0 };
	visited[y][x] = 1;
	q.push( s );
	while( !q.empty() ) {
		
		state p = q.front();
		q.pop();
		if( p.x == gx && p.y == gy ) {
			y = gy;
			x = gx;
			d += p.d;
			break;
		}
		for( int i = 0; i < 4; i++ ) {
			int d[4][2] = { { 1, 0 }, { -1, 0 }, {0, 1}, { 0, -1 } };
			state np;
			np.x = p.x + d[i][0];
			np.y = p.y + d[i][1];
			np.d = p.d + 1;
			if( np.x >= 0 && np.x < W && np.y >= 0 && np.y < H ) {
				if( !visited[np.y][np.x] ) {
					if( field[np.y][np.x] != 'X' ) {
						visited[np.y][np.x] = 1;
						q.push( np );
					}
				}
			}
		}
	}


}

int solve() {
	//幅優先探索で解ける？
	int H, W, N;
	cin >> H >> W >> N;
	field.resize(H);
	for( int i = 0; i < H; i++ )
		cin >> field[i];
	for( int i = 0; i < H; i++ ) {
		for( int j = 0; j < W; j++ ) {
			if( field[i][j] == 'S' ) {
				x = j;
				y = i;
				field[i][j] = '.';
			}
		}
	}
	for( int i = 1; i <= N; i++ ) {
		bfs( H, W, i );
	}
	cout << d << endl;
	return 0;
}
     
int main(void){
	while(1){
		solve();
		break;
	}
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#include<cstdio>

using namespace std;
int H,W,N,sx,sy,ans = 0,Index;
int imap[1400][1400];
char cmap[1400][1400];
bool used[1400][1400];
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
typedef pair<int,int> P;
void Init(){
  for(int i=0;i<1400;i++)for(int j=0;j<1400;j++)used[i][j] = false;
}

void bfs(int x,int y){
  queue<P> que;
  que.push(P(x,y));
  used[y][x] = true;

  while(que.size()){
    /*
   
    */

    P p = que.front(); que.pop();
if(cmap[p.second][p.first]-'0' == Index){
  // cout << Index << " is INNNNNNNNN" << endl;
      ans+=imap[p.second][p.first];
      Index++;
      Init();
      imap[p.second][p.first] = 0;
      bfs(p.first,p.second);
      return;
    }
    

    for(int i=0;i<4;i++){
      int nx = p.first+dx[i],ny = p.second+dy[i];
      if(!used[ny][nx] && 0<=nx && nx < W && 0<=ny && ny < H && imap[ny][nx] != -1){

	imap[ny][nx] = imap[p.second][p.first]+1;
	que.push(P(nx,ny));
	used[ny][nx] = true;
      }
    }

  }

  return;
}


int main(){
  char c;
  while(cin >> H >> W >> N){
    ans = 0;
    Index = 1;
    Init();
    for(int i=0;i<H;i++)for(int j=0;j<W;j++)imap[i][j] = 0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin >> c;
	cmap[i][j] = c;
	if(c == '.'){
	  imap[i][j] = 0;
	}
	else if(c == 'X'){
	  imap[i][j] = -1;
	}
	else if(c == 'S'){
	  sx = j;
	  sy = i;
	}
	else{
	  imap[i][j] = 0;
	}
      }
    }
    
    bfs(sx,sy);

    cout << ans << endl;

  }
  return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#include <map>
#define rep(i, n) for (int i=0; i<n; ++i)
#define FOR(i, a, b) for (int i=a; i<b; ++i)
#define pb(x) push_back(x)
#define ll long long
#define ul unsigned long long
#define dbg(x) cout  << #x << " : " << (x) << endl
#define out(x) cout << (x)
#define outl(x) cout << (x) << endl
#define int(n); int n; cin >> n;
#define long(n); long long n; cin >> n;
#define dbl(f); double f; cin >> f;
#define s(s); string s; cin >> s;
#define into(a, n) for (int i = 0; i < n; ++i) cin >> a[i]
using namespace std;

const int inf =1000000000;

int h,w,n;
char f[1000][1000];
int d[1000][1000];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int gx[10],gy[10];
typedef pair<int, int> P;

int bfs(int sx, int sy, int l) {
  queue<P> q; 
  rep(i, h) rep(j, w) d[j][i]=inf;
  q.push(P(sx, sy));
  d[sx][sy]=0;
  while(q.size()) {
    P p=q.front(); q.pop();
    if (p.first==gx[l]&&p.second==gy[l]) break;
    rep(i, 4) {
      int nx=p.first+dx[i], ny=p.second+dy[i];
      if (nx>=0&&n<w&&ny>=0&&ny<h&&f[nx][ny]!='X'&&d[nx][ny]==inf) {
        q.push(P(nx,ny));
        d[nx][ny]=d[p.first][p.second]+1;
      }
     }
  }
  return d[gx[l]][gy[l]];
} 

int main () {
  cin>>h>>w>>n;
  ll cnt=0;
  rep(i,h) {
    rep(j,w) {
      cin>>f[j][i];
      if (f[j][i]=='S') {gx[0]=j;gy[0]=i;f[j][i]='.';}
      if (f[j][i]!='.'&&f[j][i]!='X') {
        gx[(int)(f[j][i]-'0')]=j;
        gy[(int)(f[j][i]-'0')]=i;
        f[j][i]='.';
      }
    }
  }
  rep(i, n) {
    cnt+=bfs(gx[i], gy[i], i+1);
  }
  outl(cnt);
  return 0;
}