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<bits/stdc++.h>
using namespace std;

#define INF 1000000

typedef pair<int,int>P;

char maps[1001][1001];
int check[1000][1000];
int dy[4]={-1,0,1,0};
int dx[4]={0,-1,0,1};
int h,w,cheese;
int sy,sx;
char tekitou='1';

int bfs(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      check[i][j]=INF;
    }
  }
  queue<P> que;
  que.push(P(sy,sx));
  check[sy][sx]=0;
  while(!que.empty()){
    P p=que.front();que.pop();
    int y=p.first;
    int x=p.second;
    //goal
    if(maps[y][x]==tekitou){
      tekitou+=1;
      sy=y;
      sx=x;
      return check[y][x];
    }
    for(int i=0;i<4;i++){
      int ny = y+dy[i];
      int nx = x+dx[i];
      if(ny>=0 && ny<h && nx>=0 && nx<w && check[ny][nx]==INF && maps[ny][nx]!='X'){
	check[ny][nx]=check[y][x]+1;
	que.push(P(ny,nx));
      }
    }
  }
}

int main(){
  int ans=0;
  cin>>h>>w>>cheese;
  for(int i=0;i<h;i++){
    cin>>maps[i];
  }

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(maps[i][j]== 'S' ){
	sy=i;
	sx=j;
	break;
      }
    }
  }
  ans+=bfs();

  for(int i=0;i<cheese-1;i++){
    ans+=bfs();
  }
  cout<<ans<<endl;
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;

#define inRange(x,a,b) (a <= x && x < b)
int di[8] = {0,0,1,-1,1,1,-1,-1};
int dj[8] = {1,-1,0,0,1,-1,1,-1};

// H*W*4*N = 3.6e7
int main(){
    int h, w, n;
    cin >> h >> w >> n;
    n++;
    char mat[h][w];
    int pi[n], pj[n], si, sj;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> mat[i][j];
            if(mat[i][j] == 'S')    pi[0] = i, pj[0] = j;
            if(inRange(mat[i][j], '1', '9'+1))  pi[mat[i][j]-'0'] = i, pj[mat[i][j]-'0'] = j;
        }
    }
    int ans = 0;
    for(int i = 0; i+1 < n; i++){
        vector<vector<int>> dis(h, vector<int>(w,-1));
        queue<pair<pair<int,int>,int>> q;
        q.push({{pi[i], pj[i]},0});
        while(!q.empty()){
            auto p = q.front(); q.pop();
            int x = p.first.first, y = p.first.second, d = p.second;
            if(dis[x][y] != -1) continue;
            dis[x][y] = d;
            if(x == pi[i+1] && y == pj[i+1]){
                ans += d;
                break;
            }
            for(int k = 0; k < 4; k++){
                int ni = x+di[k], nj = y+dj[k];
                if(inRange(ni,0,h)&&inRange(nj,0,w)&&mat[ni][nj]!='X'&&dis[ni][nj]==-1){
                    q.push({{ni,nj},d+1});
                }
            }
        }
    }
    cout << ans << 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{
  int x,y;
  int cost;
  int ch;
}mouse;

int maxch[1000][1000];

main(){
  int h,w,n;
  cin>>h>>w>>n;
  char ku[h][w];
  rep(i,h)cin>>ku[i];

  int sx,sy;
  rep(i,h)rep(j,w)if(ku[i][j]=='S')sx=i,sy=j;

  queue<mouse> Q;
  set<pair<PI,int> > app;
  Q.push((mouse){sx,sy,0,0});
  app.insert(mp(mp(sx,sy),0));

  int ans=0;
  while(!Q.empty()){
    //    cout<<Q.size()<<endl;
    mouse t=Q.front();Q.pop();
    int cx=t.x,cy=t.y,cost=t.cost;
    int ch=t.ch;
    if(ch==n){
      ans=cost;
      break;
    }



    rep(i,4){
      int nx=cx+dx[i],ny=cy+dy[i];
      if(nx<0 || nx>=h || ny<0 || ny>=w)continue;
	 if(ku[nx][ny]=='X')continue;
	 int nch=ch;
	 if(ku[nx][ny]<='9' && ku[nx][ny]>='1'){
	   int num=ku[nx][ny]-'0';
	   if(num==t.ch+1){
	     nch=ch+1;
	   }
	 }
	 if(maxch[nx][ny]>nch)continue;
	 maxch[nx][ny]=nch;
	 if(app.count(mp(mp(nx,ny),nch)))continue;
	 Q.push((mouse){nx,ny,cost+1,nch});
	 app.insert(mp(mp(nx,ny),nch));
    }
  }
  cout<<ans<<endl;
}