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<utility>
#include<cstring>
using namespace std;
typedef struct pos{
	int x,y,d;
}pos;
queue<pos> q1;
int a[1005][1005];
bool b[1005][1005];
int main(){
	int i,j,k;
	int h,w,n;
	int x,y;
	char ca;
	pos po;
	cin>>h>>w>>n;
	cin.tie(0);
	ios::sync_with_stdio(false);
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			cin>>ca;
			if(ca=='.'){
				a[i][j]=0;
			}else if(ca=='S'){
				po.x=i; po.y=j; po.d=0;
				q1.push(po);
			}else if(ca=='X'){
				a[i][j]=-1;
			}else{
				k=1;
				for(char cs='1';cs<='9';cs++,k++){
					if(cs==ca){
						a[i][j]=k;
						break;
					}
				}
			}
		}
	}
	
	for(i=1;i<=n;i++){
		memset(b,false,sizeof(b));
		while(!q1.empty()){
			po=q1.front();
			q1.pop();
			x=po.x; y=po.y;
			if(x<0 || x>=h || y<0 || y>=w)continue;
			if(b[x][y] || a[x][y]==-1)continue;
			if(a[x][y]==i){
				break;
			}
			b[x][y]=true;
			po.d++;
			for(j=-1;j<=1;j++){
				for(k=-1;k<=1;k++){
					if((j+k)%2==0)continue;
					po.x=x+j; po.y=y+k;
					q1.push(po);
				}
			}
		}
		while(!q1.empty()){
			q1.pop();
		}
		q1.push(po);
	}
	cout<<po.d<<endl;
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>

using namespace std;

#define INF -1

typedef pair<int, int> P;

char mas[1000][1000];
int h, w, n, px, py, cnt = 0, life = 1;
int d[1000][1000];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int bfs(){

  queue<P> que;

  for(int i = 0; i < h; ++i){
    for(int j = 0; j < w; ++j){
      d[i][j] = INF;
    }
  }
  
  que.push(P(px, py));
  d[py][px] = 0;

  while(!que.empty()){
    P p = que.front(); que.pop();
    int x = p.first;
    int y = p.second;
    
    /*
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
	if(i == y && j == x)cout << 'M';
	else cout << mas[i][j];
      }
      cout << endl;
    }
    cout << endl;
    */

    //if(mas[y][x] != 'S' && mas[y][x] != '.'){
    if(life == (mas[y][x] - '0')){  
      ++life;
      px = x;
      py = y;
      mas[y][x] = '.';
      return d[y][x];
    }

    for(int i = 0; i < 4; ++i){
      int nx = x + dx[i];
      int ny = y + dy[i];
      
      if(0 <= nx && nx < w && 0 <= ny && ny < h && mas[ny][nx] != 'X' && d[ny][nx] == INF){
	que.push(P(nx, ny));
	d[ny][nx] = d[y][x] + 1;
      }
    }
  }
  return 0;
}

int main(){

  cin >> h >> w >> n;

  for(int i = 0; i < h; ++i){
    for(int j = 0; j < w; ++j){
      cin >> mas[i][j];
      if(mas[i][j] == 'S'){
	px = j;
	py = i;
      }
    }
  }

  for(int i = 0; i < 9; ++i){
    cnt += bfs();
  }
  
  cout << cnt << endl;

  return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(int a[][1002],int b[][1002],int p,int q,int d,int e,int w,int h,int m){
  if(a[p][q]==-1);
  else if(p==d&&q==e){
    if(b[p][q]==-1||b[p][q]>m)
      b[p][q]=m;
  }else if(b[p][q]==-1||b[p][q]>m){
    b[p][q]=m;
    f(a,b,p-1,q,d,e,w,h,m+1);
    f(a,b,p,q-1,d,e,w,h,m+1);
    f(a,b,p+1,q,d,e,w,h,m+1);
    f(a,b,p,q+1,d,e,w,h,m+1);
  }
  return;
}
int main(){
  int i,j;
  int a[1002][1002],b[1002][1002],c[10],d[10],w,h,n;
  int sm=0;
  memset(a,-1,sizeof(a));
  cin>>h>>w>>n;
  for(i=0;i<h;i++){
    string s;
    cin>>s;
    for(j=0;j<w;j++){
      if(s[j]=='X');
      else{
	a[j+1][i+1]=0;
	if(s[j]=='.');
	else if(s[j]=='S'){
	  c[0]=j+1;
	  d[0]=i+1;
	}else{
	  c[s[j]-'0']=j+1;
	  d[s[j]-'0']=i+1;
	}
      }
    }
  }
  for(i=0;i<n;i++){
    memset(b,-1,sizeof(b));
    f(a,b,c[i],d[i],c[i+1],d[i+1],w,h,0);
    sm+=b[c[i+1]][d[i+1]];
  }    
  cout<<sm<<endl;
  return 0;
}

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

#define MAX 200
int y[MAX][MAX];
int x[MAX][MAX];
int px[10];
int py[10];
int main(){
	memset(y,127,sizeof(y));
	char T[12]={'.','1','2','3','4','5','6','7','8','9','S','X'};
	int n,m,c,d,sum;
	string S;
	sum=0;
	cin>>n>>m>>c;
	for(int i=0;i<n;i++){
		cin>>S;
		for(int j=0;j<m;j++){
			for(int k=0;k<12;k++){
				if(S[j]==T[k]){
					x[i][j]=k;
					if(k==10){
						px[0]=i;
						py[0]=j;
					}
					if(k>=1 && k<=9){
						px[k]=i;
						py[k]=j;
					}
				}
			}
		}
	}
	for(int i=1;i<=c;i++){
		memset(y,127,sizeof(y));
		y[px[i-1]][py[i-1]]=0;
		for(int j=0;j<n*m;j++){
			for(int k=0;k<n;k++){
				for(int l=0;l<m;l++){
					if(k>=1){
						if(x[k-1][l]!=11){
							y[k-1][l]=min(y[k-1][l],y[k][l]+1);
						}
					}
					if(l>=1){
						if(x[k][l-1]!=11){
							y[k][l-1]=min(y[k][l-1],y[k][l]+1);
						}
					}
					if(x[k+1][l]!=11){
						y[k+1][l]=min(y[k+1][l],y[k][l]+1);
					}
					if(x[k][l+1]!=11){
						y[k][l+1]=min(y[k][l+1],y[k][l]+1);
					}
				}
			}
		}
		sum += y[px[i]][py[i]];
	}
	cout<<sum<<endl;
	return 0;
}