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<string>
#include<cstring>
#include<cstdio>
using namespace std;

  int used[1001][1001];
  char m[1001][1001];
  int H,W,N,sx,sy;
  
  const int dx[4]={1,0,-1,0};
  const int dy[4]={0,1,0,-1};

bool check(int x,int y){
  return x>=0&&y>=0&&x<H&&y<W&&m[x][y]!='X';
}

int bfs(int p){
  for(int i = 0; i < 1001; i++)for(int j=0;j<1001;j++)used[i][j] = 0;
  queue<pair<int,int> > que;
  que.push(make_pair(sx,sy));
  while(!que.empty()){
    pair<int,int> pos= que.front();
    que.pop();
    for(int i=0;i<4;i++){
      int x=pos.first+dx[i];
      int y=pos.second+dy[i];
      if(check(x,y) && used[x][y] == 0){
       que.push(make_pair(x,y));
       used[x][y] = used[pos.first][pos.second] + 1;
      }
      if(m[x][y]==p+'0'){
	
       sx=x;
       sy=y;
       return used[x][y];
      }
    }
  }
}



int main()
{
  cin>>H>>W>>N;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin>>m[i][j];
      if(m[i][j]=='S'){
       sx=i;
       sy=j;
      }	
    }
  }
  int res=0;
  for(int i=1;i<=N;i++){
    res+=bfs(i);
  }
  cout << res << endl;
  
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <math.h>
#include <string>
#include <string.h>
#include <stack>
#include <queue>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <stdlib.h>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 10000000000LL
#define rep(i,n) for(i=0;i<n;i++)

typedef pair<int,int> pii;

char a[1005][1005];
int used[1005][1005]={};
int h,w;
pii place[10];

int search(int sx,int sy,int gx,int gy){
    int i,j;
    int dx[4]={-1,0,1,0};
    int dy[4]={0,-1,0,1};
    rep(i,h)rep(j,w)used[i][j]=0;
    int ans=0;
    used[sx][sy]++;
    queue<pii> q1,q2;
    q1.push(pii(sx,sy));
    while(1){
        bool f=false;
        while(q1.size()){
            int x=q1.front().first;
            int y=q1.front().second;
            if(x==gx && y==gy){
                f=true;
                break;
            }
            rep(i,4){
                int nx=x+dx[i];//next
                int ny=y+dy[i];//next
                if(nx<0 || nx>=h || ny<0 || ny>=w)continue;
                if(a[nx][ny]!='X' && used[nx][ny]==0){
                    used[nx][ny]++;
                    q2.push(pii(nx,ny));
                }
            }
            q1.pop();
        }

        if(f)break;

        while(q2.size()){
            q1.push(pii(q2.front().first,q2.front().second));
            q2.pop();
        }
        ans++;
    }
    return ans;
}

int main(void) {
    int n,i,j;
    cin>>h>>w>>n;
    rep(i,h){
        cin>>a[i];
        rep(j,w)
            if(a[i][j]=='S'){
                place[0].first=i;
                place[0].second=j;
            }else if(a[i][j]>'0' && a[i][j]<='n'){
                place[a[i][j]-'0'].first=i;
                place[a[i][j]-'0'].second=j;
            }
    }
    
    int ans=0;
    rep(i,n)
        ans+=search(place[i].first,place[i].second,place[i+1].first,place[i+1].second);
        
    cout<<ans<<endl;
    return 0;
}

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

string field[1020];
bool memo[1020][1020][10];
struct Data{
    int x;
    int y;
    int t;
};
int main(){
    const int dx[] = {0,1,0,-1},dy[] = {1,0,-1,0};
    int H,W,N;
    cin>>H>>W>>N;
    Data init;
    for(int i = 0;i < H;i++){
        cin>>field[i];
        for(int j = 0;j < W;j++){
            if(field[i][j] == 'S')init.x = j,init.y = i;
        }
    }
    init.t = 0;
    for(int q = 1;q <= N;q++){
        queue<Data> Q;
        Q.push(init);
        while(!Q.empty()){
            Data D = Q.front();Q.pop();
            if(memo[D.y][D.x][q])continue;
            memo[D.y][D.x][q] = true;
            if(field[D.y][D.x] == q + '0'){
                init = D;
                if(q == N)cout<<D.t<<endl;
                break;
            }

            for(int i =0;i < 4;i++){
                int x = D.x+dx[i];
                int y = D.y+dy[i];
                if(0<=x&&x<W&&0<=y&&y<H){
                    Data E = D;
                    E.x = x;
                    E.y = y;
                    E.t++;
                    if(field[y][x]=='.'||(1+'0'<=field[y][x]&&field[y][x]<=q+'0')){
                        Q.push(E);
                    }
                }
            }
        }

    }


}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int,int> P;
int main(){
      int w,h,n,solve=0,dx[]={0,1,-1,0},dy[]={1,0,0,-1};
      P cheese[9];
      cin>>h>>w>>n;
      char d[h][w];
      int dis[h][w];
      for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                  cin>>d[i][j];
                  if('1'<=d[i][j]&&'9'>=d[i][j])
                        cheese[d[i][j]-'0'] = make_pair(i,j);
                  else if(d[i][j]=='S')
                        cheese[0] = make_pair(i,j);
            }
      }
      for(int i=0;i<n;i++){
            queue<P> que;
            for(int a=0;a<h;a++)for(int b=0;b<w;b++)dis[a][b]=-1;
            dis[cheese[i].first][cheese[i].second]=0;
            que.push(cheese[i]);
            while(!que.empty()){
                  P p = que.front();que.pop();
                  if(p == cheese[i+1]){
                        solve += dis[p.first][p.second];break;
                  }
                  for(int j = 0;j < 4;j++){
                        int nx = p.first + dx[j],ny = p.second+ dy[j];
                        if(0<=nx&&nx<h&&0<=ny&&ny<w&&d[nx][ny]!='X'&&dis[nx][ny]==-1){
                              dis[nx][ny] = dis[p.first][p.second]+1;
                              que.push(make_pair(nx,ny));
                        }

                  }
            }
      }
      cout << solve << endl;
}