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>
#define N 1002
#define INFINITY 2000000000
using namespace std;
void bfs();
int h,w,n,num[N][N],d[N*N],u,m,x=1;
string color[N*N],in[N*N];
int main(){
  int i,j,k;
  cin >> h >> w >> n;
  for(i=0;i<h+2;i++)
    for(j=0;j<w+2;j++)
      num[i][j]=-1;
  for(i=1;i<=h;i++) cin >> in[i],in[i]='X'+in[i];
  k=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      if(in[i][j]!='X') num[i][j]=k,k++;
    }
  }
  bfs();
  return 0;
}
void bfs(){
  int i,j,p=1,t=0,flag=0,time[10];
  char start='S',goal='1';
  queue<int> q;
  /*  while(1){
    if(flag==1) start=x+'0',goal=x+'0'+1,x++;
    if(x>n+1) break;*/
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	color[num[i][j]]="WHITE",d[num[i][j]]=INFINITY;
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==start) color[num[i][j]]="GLAY",d[num[i][j]]=0,q.push(num[i][j]);
      }
    }
    while(q.empty()!=1){
      u=q.front();
      q.pop();
      for(i=1;i<=h;i++){
	for(j=1;j<=w;j++){
	  if(num[i][j]!=-1&&(num[i-1][j]==u||num[i+1][j]==u||num[i][j+1]==u||num[i][j-1]==u)&&color[num[i][j]]=="WHITE"){
	    color[num[i][j]]="GLAY";
	    d[num[i][j]]=d[u]+1;
	    if('0'<=in[i][j]&&in[i][j]<='9') time[in[i][j]-'0']=d[num[i][j]];
	    q.push(num[i][j]);
	  }
	}
      }
      color[u]="BLACK";
    }
    for(i=1;i<=n;i++) cout << "i = " << i << ' ' << time[i] << endl;
    /*    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==goal) t+=d[num[i][j]];
      }
    }
    flag=1;
    }*/
  cout << t << endl;
}

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

int H, M, N;
int go[4][2];
bool K[1002][1002];
bool flg[1002][1002];
int score[1002][1002];
int C[11][2];
int ans = 0;

struct Tops{
  int tx, ty, co;
  
  Tops(int x, int y, int c){
    tx = x;
    ty = y;
    co = c;
  }
};

queue<Tops> que;

int cal(int x, int y){
  while(!que.empty()){
    Tops t = que.front();
    que.pop();
    flg[t.tx][t.ty] = false;
    if(t.tx == x && t.ty == y){
      return t.co;
    }
    for(int i = 0; i < 4; i++){
      if(flg[t.tx + go[i][0]][t.ty + go[i][1]]){
        que.push(Tops(t.tx + go[i][0], t.ty + go[i][1], t.co + 1));
      }
    }
  }
  return 2147483647;
}

int main(){
  
  go[0][0] = 1;
  go[0][1] = 0;
  go[1][0] = 0;
  go[1][1] = -1;
  go[2][0] = -1;
  go[2][1] = 0;
  go[3][0] = 0;
  go[3][1] = 1;
  
  for(int i = 0; i <= H + 1; i++){
    for(int j = 0; j <= M + 1; j++){
      score[i][j] = 2147483640;
      flg[i][j] = false;
      K[i][j] = false;
    }
  }
  
  scanf("%d%d%d", &H, &M, &N);
  
  for(int i = 0; i < H; i++){
    char str[M + 1];
    scanf("%s", str);
    for(int j = 0; j < M; j++){
      if(str[j] == '.'){
        K[i + 1][j + 1] = true;
      }else if(str[j] == 'X'){
        K[i + 1][j + 1] = false;
      }else if(str[j] == 'S'){
        C[0][0] = i + 1;
        C[0][1] = j + 1;
        K[i + 1][j + 1] = true;
      }else{
        C[str[j] - '0'][0] = i + 1;
        C[str[j] - '0'][1] = j + 1;
        K[i + 1][j + 1] = true;
      }
    }
  }
  
  for(int i = 0; i < N; i++){
    for(int j = 0; j <= M + 1; j++){
      for(int k = 0; k <= H + 1; k++){
        flg[j][k] = K[j][k];
      }
    }
    while(!que.empty()){
      que.pop();
    }
    que.push(Tops(C[i + 1][0], C[i + 1][1], 0));
    int aa = cal(C[i][0], C[i][1]);
    ans += aa;
  }
  
  printf("%d", ans);
  
  return 0;
}

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

using namespace std;

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

void solve(vector<vector<char> >& board, int H, int W, int N, 
           int x, int y, int power, int count, int step, int& ret) {
  int prev_value = board[x][y];
  if (step >= ret) return;
  if (board[x][y] >= 1 && board[x][y] <= 9) {
    power++;
    count++;
    board[x][y] = '.';
    if (count == N) {
      ret = min(ret, step);
      board[x][y] = prev_value;
      return;
    }
  }

  //board[x][y] = 'X';
  for (int i = 0; i < 4; ++i) {
    int next_x = x + dir[i][0];
    int next_y = y + dir[i][1];
    if (next_x >= 0 && next_x < H
        && next_y >= 0 && next_y < W
        && board[next_x][next_y] != 'X') {
        if (board[next_x][next_y] >= 1 
            && board[next_x][next_y] < 9
            && power < board[next_x][next_y]) 
          continue;
        solve(board, H, W, N, next_x, next_y, power, count, step + 1, ret);
    }
  }
  board[x][y] = prev_value;
}

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  vector<vector<char> > board(H, vector<char>(W));
  char c;
  int x, y;
  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      cin >> c;
      if (c >= '1' && c <= '9') {
        c = c - '0';
      }
      board[h][w] = c;
      if (c == 'S') {
        x = h;
        y = w;
      }
    }
  }
  int ret = H * W;
  board[x][y] = '.';
  solve(board, H, W, N, x, y, 1, 0, 0, ret);
  cout << ret << endl;
}

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

using namespace std;

int nowS = 0;//??????-1
int x, y, n;
int mp[1000][1000] = {};//????????¨??????
int k[4] = {1, -1, 0, 0};
int h[4] = {0, 0, 1, -1};
int cx[9] = {};
int cy[9] = {};
vector<int> bufX, bufY, bufD;

bool search(int xx, int yy, int dd) {//???????????????????????\????????????

	bufX.erase(bufX.begin());//??????????¶????
	bufY.erase(bufY.begin());
	bufD.erase(bufD.begin());
	
	if (mp[yy][xx] == nowS + 1)
		return false;

	mp[yy][xx] = nowS + 1;//?????£??????????????????????????°
	for (int i = 0; i < 4; i++) {
		if (xx + k[i] < 0 || xx + k[i] >= x || yy + h[i] < 0 || yy + h[i] >= y)//?????????
			continue;
		if (mp[yy + h[i]][xx + k[i]] == -1) {//?£?
			continue;
		}
		if (cx[nowS] == xx + k[i] && cy[nowS] == yy + h[i]) {//??????
			nowS++;
			bufX.clear();//????????????
			bufY.clear();
			bufD.clear();
			if (nowS == n) {//?????????????????????
				cout << dd + 1 << endl;
				return true;
			}
			else {//??????
				bufX.push_back(xx + k[i]);
				bufY.push_back(yy + h[i]);
				bufD.push_back(dd + 1);
			}
			return false;
		}
		if(mp[yy + h[i]][xx + k[i]] >= 0 && mp[yy + h[i]][xx + k[i]] < nowS + 1){//??????
			bufX.push_back(xx + k[i]);
			bufY.push_back(yy + h[i]);
			bufD.push_back(dd + 1);
		}
	}
	return false;
}

int main() {
	int ch = 0;
	cin >> y >> x >> n;
	for (int i = 0; i < y; i++) {//??\???
		char c[1001];
		cin >> c;
		for (int j = 0; j < x; j++) {
			if(c[j] == 'X')
				mp[i][j] = -1;
			else if(c[j] == 'S'){
				bufX.push_back(j);
				bufY.push_back(i);
				bufD.push_back(0);
				mp[i][j] = 0;
			}	
			else {
				mp[i][j] = 0;
			}
			if (c[j] >= 48 && c[j] <= 57) {
				int m;
				m = c[j] - 49;
				cx[m] = j;
				cy[m] = i;		
				ch++;
			}
		}
	}
	

	while(!search(bufX[0], bufY[0], bufD[0])){}//??????????????§?????????

	return 0;
}