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<map>
#include<queue>
using namespace std;
typedef pair<int,int> P;

int bfs();
char mas[1111][1111];
int d[1111][1111];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int sx,sy;
int ans=0;
char life='0';
int h,w,n;
int INF=10000000;

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'){
	sx = i,sy = j;
      }
    }
  }
  for(int i=1;i<=n;i++){
    life++;
    ans += bfs();
  }
  cout << ans << endl;
}

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(sx,sy));
  d[sx][sy] = 0;
  while(que.size()){
    P p = que.front();
    que.pop();
    if(mas[p.first][p.second] == life){
      sx = p.first,sy=p.second;
      break;
    }
    for(int i=0;i<4;i++){
      int nx=p.first + dx[i];
      int ny=p.second + dy[i];
      if(nx >= 0 && nx < h && ny >= 0 && ny < w && mas[nx][ny] != 'X' && d[nx][ny] == INF){
	que.push(P(nx,ny));
	d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[sx][sy];
}

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][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]);
		std::cout << ans << std::endl;
		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 <algorithm>
#include <queue>
using namespace std;

const int INF = 1<<30;
const int maxn = 1000+10;

int w,h,n;
char mp[maxn][maxn];
int d[maxn][maxn];

int dir[4][2] = {
	{-1,0},
	{1,0},
	{0,1},
	{0,-1},
};

int fac[10][2];

int bfs(int sx,int sy,int gx,int gy){
	memset(d,-1,sizeof(d));

	queue<pair<int,int> > Q;
	Q.push(make_pair(sx,sy));
	d[sx][sy] = 0;
	while(!Q.empty()){
		pair<int,int> p = Q.front(); Q.pop();
		if(p.first==gx && p.second==gy) break;

		for(int i=0; i<4; i++){
			int tx = p.first + dir[i][0];
			int ty = p.second + dir[i][1];
			if(tx>=0 && tx<h && ty>=0 && ty<w && mp[tx][ty]!='X' && d[tx][ty]==-1){
				Q.push(make_pair(tx,ty));
				d[tx][ty] = d[p.first][p.second] + 1;
			}
		}
	}

	return d[gx][gy];
}

int main(){
	scanf("%d%d%d",&h,&w,&n);	
	for(int i=0; i<h; i++)
		for(int j=0; j<w; j++)
			cin >> mp[i][j];

	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			if(mp[i][j] == 'S'){
				fac[0][0] = i;
				fac[0][1] = j;
				mp[i][j] = '.';
			}else if(isdigit(mp[i][j])){
				int index = mp[i][j] - '0';
				fac[index][0] = i;
				fac[index][1] = j;
			}
		}
	}

	int step = 0;
	for(int i=0; i<n; i++){
		step += bfs(fac[i][0],fac[i][1],fac[i+1][0],fac[i+1][1]);
	}

	printf("%d\n",step);
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
double  cheese[1001][1001];
double  check[1001][1001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
			
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(cheese[y.a][y.b]==-1)continue;
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&check[y.a+q[i]][y.b+p[i]]!=1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int m,n;
int maxe=0;
int che=0;
int k;
struct node{
  int x,y;
};
char mape[1111][1111];
int d[1111][1111];
queue<node> q;
int da[]={0,0,1,-1};
int db[]={1,-1,0,0};
void bfs(){
  while(!q.empty()&&che<k){
    node nn;
    int x=q.front().x;
    int y=q.front().y;
    q.pop();
    if(mape[x][y]>='0'&&mape[x][y]<='9'){
      che++;
      maxe+=d[x][y];
    }
    else mape[x][y]='X';
    for(int i=0;i<4;i++)
      if(x+da[i]>=0&&x+da[i]<m&&y+db[i]>=0&&y+db[i]<n&&mape[x+da[i]][y+db[i]]!='X'){
	int w=x+da[i];
	int e=y+db[i];
	d[w][e]=d[x][y]+1;
	nn.x=w;
	nn.y=e;
	mape[w][e]='X';
	q.push(nn);
      }
  }
}
int main(){
  cin>>m>>n>>k;
  while(!q.empty())q.pop();
  node nn;
  memset(d,-1,sizeof(d));
  for(int i=0;i<m;i++)
    for(int j=0;j<n;j++){
      cin>>mape[i][j];
      if(mape[i][j]=='S'){
	nn.x=i;
	nn.y=j;
	d[i][j]=0;
	q.push(nn);
	mape[i][j]='X';
      }
    }
  bfs();
  cout<<maxe<<"\n";
}