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 <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cctype>

#define MAX 1001
#define INF (1<<29)

using namespace std;

struct Point{
  int x, y;
};

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int H, W, N;
char field[MAX][MAX];
Point cheese[11];
int d[MAX][MAX];
int ans;

void bfs(){
  for(int i = 1 ; i <= N ; i++){
    Point sp = cheese[i-1];
    int sx = cheese[i-1].x;
    int sy = cheese[i-1].y;
    int gx = cheese[i].x;
    int gy = cheese[i].y;
    
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
	d[i][j] = INF;
      }
    }
    
    queue<Point> que;
    
    que.push(sp);
    d[sy][sx] = 0;

    while(!que.empty()){
      Point now = que.front();
      que.pop();
      
      int x = now.x;
      int y = now.y;
      
      if(x == gx && y == gy){
	ans += d[gy][gx];
	break;
      }
      
      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){
	  if(field[ny][nx] != 'X'){
	    Point next;
	    
	    next.x = nx;
	    next.y = ny;
	    
	    if(d[ny][nx] == INF){
	      d[ny][nx] = d[y][x] + 1;
	      que.push(next);
	    }
	  }
	}
      }
    }
  }
}

int main(){
  cin >> H >> W >> N;
  
  for(int i = 0 ; i < H ; i++){
    for(int j = 0 ; j < W ; j++){
      cin >> field[i][j];
      if(field[i][j] == 'S'){
	cheese[0].x = j;
	cheese[0].y = i;
      }
      if(isdigit(field[i][j])){
	cheese[field[i][j]-'0'].x = j;
	cheese[field[i][j]-'0'].y = i;
      }
    }
  }
  bfs();
  cout << ans << endl;
}

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

typedef std::pair<int, int> T;

const int vecx[4] = { 1,0,-1,0 };
const int vecy[4] = { 0,-1,0,1 };
int h, w, n;
T goal[10];
char filed[1000][1000];
int si, sj;
int ans;
int dis[1000][1000];

void bfs(T _goal, T _start) {
	std::queue<T> q;
	q.push(_start);
	while (q.size()) {
		T tmp = q.front();
		q.pop();
		if (tmp == _goal) {
			ans += dis[_goal.first][_goal.second];
			break;
		}
		int x = tmp.first;
		int y = tmp.second;
		for (int i = 0; i < 4; i++) {
			if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 || y + vecy[i] >= w)continue;
			if (filed[x + vecx[i]][y + vecy[i]] != 'X' && !dis[x + vecx[i]][y + vecy[i]]) {
				dis[x + vecx[i]][y + vecy[i]] = dis[x][y] + 1;
				q.push(std::make_pair(x + vecx[i], y + vecy[i]));
			}
		}
	}
}

int main() {
	std::cin >> h >> w >> n;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			std::cin >> filed[i][j];
			if (filed[i][j] == 'S') {
				goal[0].first = i;
				goal[0].second = j;
			}
			if (filed[i][j] > '0' && filed[i][j] <= '9') {
				goal[filed[i][j] - '0'].first = i;
				goal[filed[i][j] - '0'].second = j;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		bfs(goal[i + 1], goal[i]);
		for (int j = 0; j < h; j++)for (int k = 0; k < w; k++)dis[j][k] = 0;
	}
	std::cout << ans << std::endl;
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int H,W,N;
const int MAX_H=1000;
const int MAX_W=1000;
string S[MAX_H];
int ans=0;
int X[10],Y[10];
int D[MAX_W][MAX_H];
int cou;
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
int xs,ys,xg,yg;
int E[MAX_W][MAX_H];
int solve(){
	cou=0;
	while(D[xg][yg]==-1){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				E[j][i]=D[j][i];
			}
		}
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(D[j][i]==cou){
					for(int k=0;k<4;k++){
						if(j+dx[k]>=0&&j+dx[k]<W&&i+dy[k]>=0&&i+dy[k]<H){
							if(D[j+dx[k]][i+dy[k]]==-1&&S[i+dy[k]][j+dx[k]]!='X'){
								E[j+dx[k]][i+dy[k]]=cou+1;
							}
						}
					}
				}
			}
		}
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)D[j][i]=E[j][i];
		cou++;
	    }
	return D[xg][yg];
}

int main()
{
	scanf("%d%d%d",&H,&W,&N);
	for(int i=0;i<H;i++)cin >> S[i];
	for(int i=0;i<W;i++){
		for(int j=0;j<H;j++){
			if(S[j][i]!='X'&&S[j][i]!='.'){
				if(S[j][i]=='S'){
					X[0]=i;Y[0]=j;
				}
				else{
					X[S[j][i]-'0']=i;
					Y[S[j][i]-'0']=j;
				}
			}
		}
	}
	for(int ti=0;ti<N;ti++){
		for(int i=0;i<W;i++){
		    for(int j=0;j<H;j++){
			    D[i][j]=-1;
		    }
	    }
		xs=X[ti];
		ys=Y[ti];
		xg=X[ti+1];
		yg=Y[ti+1];
		D[xs][ys]=0;
		ans+=solve();
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#define INF 99999999
using namespace std;
typedef pair <int,int> P;
int sx,sy;  
int H,W,N;
int ans=0,g=0;
char S[1010][1010];
int gx,gy;
int d[1010][1010];
int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0};
int bfs();
int main(){
  cin >> H >> W >> N;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin >> S[i][j];
      if(S[i][j] == 'S'){
	sx = i;
	sy = j;
      }
    }
  }
  for(int i=0;i<N;i++){
    int res = bfs();
    ans += res;
    g++;
  }
  cout << ans <<  endl;
}
int bfs(){
  queue <P> qu;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      d[i][j] = INF;
      if(S[i][j] == '1' + g){
	gx = i;
	gy = j;
      }
    }
  }
  qu.push(P(sx,sy));
  d[sx][sy] = 0;
  while(!qu.empty()){
    P p = qu.front();
    qu.pop();
    if(p.first == gx && p.second == gy)break;
    for(int i=0;i<4;i++){
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if(0 <= nx && nx < H && 0 <= ny && ny < W && S[nx][ny] != 'X' && d[nx][ny] == INF){
	qu.push(P(nx,ny));
	d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  sx = gx;
  sy = gy;
  return d[gx][gy];
}