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 <cstring>
using namespace std;

class State{
public:
  int x, y, cost;

  State(int _x, int _y, int _cost){
    x = _x;
    y = _y;
    cost = _cost;
  }
};

int h, w, n;
int cx[12], cy[12];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool closed[1002][1002];
char t[1002][1002];

int bfs(int sx, int sy, int gx, int gy){
  queue<State> open;
  open.push(State(sx, sy, 0));
  memset(closed, 0, sizeof(closed));

  while(!open.empty()){
    State st = open.front();
    open.pop();

    if(closed[st.y][st.x]) continue;
    closed[st.y][st.x] = true;

    if(st.x == gx && st.y == gy){
      return st.cost;
    }

    for(int i = 0; i < 4; i++){
      int nx = st.x + dx[i];
      int ny = st.y + dy[i];

      if(0 <= nx && nx < w && 0 <= ny && ny < h && t[ny][nx] == '.'){
        open.push(State(nx, ny, st.cost + 1));
      }
    }
  }

  return 99999999;
}

int main(){
  while(cin >> h >> w >> n){
    for(int i = 0; i < h; i++){
      cin >> t[i];

      for(int j = 0; j < w; j++){
        if(t[i][j] == 'S'){
          cx[0] = j;
          cy[0] = i;
          t[i][j] = '.';
        }
        else if('0' <= t[i][j] && t[i][j] <= '9'){
          int idx = t[i][j] - '0';
          cx[idx] = j;
          cy[idx] = i;
          t[i][j] = '.';
        }
      }
    }

    int ans = 0;

    for(int i = 1; i <= n; i++){
      ans += bfs(cx[i - 1], cy[i - 1], cx[i], cy[i]);
    }

    cout << ans << endl;
  }
}

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

//ios::sync_with_stdio(false);

using namespace std;

const int INF = (1<<30) - 1;

int dh[] = {0, 0, 1, -1};
int dw[] = {1, -1, 0, 0};

int bfs(vector<string>& field, pair<int, int> start, pair<int, int> end){
    int H = field.size();
    int W = field[0].size();
    vector<vector<int> > d(H, vector<int>(W, INF));
    queue<pair<int, int> > nodes;
    
    d[start.first][start.second] = 0;
    nodes.push(start);
    
    while(!nodes.empty()){
        auto node = nodes.front(); nodes.pop();
        for(int i=0; i<4; i++){
            int nh = dh[i] + node.first;
            int nw = dw[i] + node.second;
            
            if(0<=nh&&nh<H && 0<=nw&&nw<W && field[nh][nw] != 'X' && d[nh][nw] == INF){
                d[nh][nw] = d[node.first][node.second] + 1;
                
                if(nh == end.first && nw == end.second)
                    return d[nh][nw];
                
                nodes.push(make_pair(nh, nw));
            }
        }
    }
    return -1;
}

int main(){
    int H, W, N;
    cin >> H >> W >> N;
    
    vector<string> field(H);
    for(int i=0; i<H; i++)
        cin >> field[i];
    
    vector<pair<int, int> > cheeses(N+1);
    
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            char& c = field[i][j];
            if(c == 'S'){
                cheeses[0] = make_pair(i, j);
            }else if(c != '.' && c != 'X'){
                cheeses[c-'0'] = make_pair(i, j);
            }
        }
    }
    
    
    int ans = 0;
    for(int i=0; i<N; i++){
        ans += bfs(field, cheeses[i], cheeses[i+1]);
    }
    
    cout << ans << endl;
    
	return 0;
}

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

#define INF 999999999999999
#define MAX_X 1000
#define MAX_Y 1000
#define MAX_HEALTH 10
struct status {
	int health;
	int x;
	int y;
	int minutes;
};

queue<status> bfs;
bool checked[MAX_HEALTH+1][MAX_X+1][MAX_Y+1];//health,x,y
//int lastnum[11] = {0,0,0,0,0,0,0,0,0,0};
int maxhp = 0;
///queue???push??????
///??¢??????????????\??????????????¨?????????status?????´?????????????????????
bool PushToQueue(status a) {
	/*for (int i = 0;i < lastnum[a.health];i++) {
		if (checked[i].health == a.health && checked[a.health][i].x == a.x && checked[a.health][i].y == a.y && checked[a.health][i].minutes == a.minutes){
			cout << "health:" << a.health << "/" << i << "/" << lastnum[a.health] << endl;
			return true;
		} 
	}
	checked[a.health][lastnum[a.health]++] = a;*/
	if (checked[a.health][a.x][a.y] == true) return false;
	if (a.health < maxhp) return false;
	bfs.push(a);
	if (a.health > maxhp) {
		maxhp = a.health;
		//cout << "maxhp has changed to " << maxhp << endl;
	}
	checked[a.health][a.x][a.y] = true;
	return true;
}

int main() {
	for (int a = 0;a < 11;a++) {
		for (int b = 0;b < 1000;b++) {
			for (int c = 0;c < 1000;c++) {
				checked[a][b][c] = false;
			}
		}
	}

	int H , W , N;

	cin >> H >> W >> N;
	char data[H][W];
	
	for (int i = 0;i < H;i++) {
		cin >> data[i];
	}
	//cout << "A" << endl;
	//Find 'S'
	for (int y = 0;y < H;y++) {
		for (int x = 0;x < W;x++) {
			if(data[y][x] == 'S') {
				bfs.push((struct status) {1,x,y,0});
				goto getout;
			}
		}
	}


	getout:
	int result = INF;
	for (;;) {
		if (bfs.empty()) break;
		struct status popped = bfs.front();
		bfs.pop();
		//cout << popped.health << "/" << popped.x << "/" << popped.y << "/" << popped.minutes << "/" << endl;
		if (popped.health == N+1) {
			if (popped.minutes < result) {
				result = popped.minutes;
			}
		}
		else {
			for (int x = -1;x < 2;x++) {
				for (int y = -1;y < 2;y++) {
					if(x != 0 && y != 0) continue;
					if (popped.x + x <0 || popped.x + x >= W || popped.y + y < 0 || popped.y + y >= H) continue;
					if (data[popped.y + y][popped.x + x] == 'S'||data[popped.y + y][popped.x + x] == 'X') continue;
					else if (data[popped.y + y][popped.x + x] == '.') {
						PushToQueue((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1});
					}
					else {
						//cout << "*****************************factory" << endl;
						int factory = data[popped.y + y][popped.x + x] -48 ;
						//cout << data[popped.y + y][popped.x + x]<< endl;
						if (factory == popped.health) {
							//eat
							PushToQueue((struct status) {popped.health + 1,popped.x + x,popped.y + y,popped.minutes + 1});
						}
						else {
							//the mouse doesn't eat.
							PushToQueue((struct status) {popped.health,popped.x + x,popped.y + y,popped.minutes + 1});
						}
					}
				}
			}
		}
	}
	cout << result << endl;
	return 0;
}

C++: #include <stdio.h>

int main() {
    int i, j;
    int h, w, n;

    scanf("%d %d %d", &h, &w, &n);
    for (i=0; i<h; i++) {
        for (j=0; j<w; j++) {
            int c;

            scanf(" %c", &c);
            if (c == 'S')