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 <vector>
#include <math.h>
#include <string>
#include <stack>
using namespace std;

int h, w, n;
int map[1000][1000];
int map_t[1000][1000];
int step;
vector<pair<int, int>> point;


bool SearchAndCollect(int goal)
{
	vector<pair<int, int>> point_t;
	for (int i = 0; i < point.size(); i++)
	{
		if (point[i].first > 0)
		{
			if (map_t[point[i].first - 1][point[i].second] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first - 1][point[i].second] != -1)
			{
				map_t[point[i].first - 1][point[i].second] = -1;
				pair<int, int> temp;
				temp.first = point[i].first - 1;
				temp.second = point[i].second;
				point_t.push_back(temp);
			}
		}

		if (point[i].first < h - 1)
		{
			if (map_t[point[i].first + 1][point[i].second] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first + 1][point[i].second] != -1)
			{
				map_t[point[i].first + 1][point[i].second] = -1;
				pair<int, int> temp;
				temp.first = point[i].first + 1;
				temp.second = point[i].second;
				point_t.push_back(temp);
			}
		}

		if (point[i].second > 0)
		{
			if (map_t[point[i].first][point[i].second - 1] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first][point[i].second - 1] != -1)
			{
				map_t[point[i].first][point[i].second - 1] = -1;
				pair<int, int> temp;
				temp.first = point[i].first;
				temp.second = point[i].second - 1;
				point_t.push_back(temp);
			}
		}

		if (point[i].second < w - 1)
		{
			if (map_t[point[i].first][point[i].second + 1] == goal)
			{
				return true;
			}
			else if (map_t[point[i].first][point[i].second + 1] != -1)
			{
				map_t[point[i].first][point[i].second + 1] = -1;
				pair<int, int> temp;
				temp.first = point[i].first;
				temp.second = point[i].second + 1;
				point_t.push_back(temp);
			}
		}
	}
	point.clear();
	for (int i = 0; i < point_t.size(); i++)
	{
		if (point.size() <= i)
		{
			point.resize(point.size() + 1);
		}
		point[i] = point_t[i];
	}
	step++;
	return false;
}

int TimeFrom(int start)
{
	step = 1;
	int goal = start + 1;
	if (start == 10)
	{
		goal = 1;
	}

	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			map_t[i][j] = map[i][j];
		}
	}

	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			if (map[i][j] == start)
			{
				pair<int, int> temp;
				temp.first = i;
				temp.second = j;
				point.clear();
				point.push_back(temp);
				while (SearchAndCollect(goal) == false)
				{
				}
				return step;
			}
		}
	}
}

int main()
{
	string line;
	while (cin >> h >> w >> n)
	{
		for (int i = 0; i < h; i++)
		{
			cin >> line;
			for (int j = 0; j < w; j++)
			{
				switch (line[j])
				{
				case 'S':
					map[i][j] = 10;
					break;

				case '.':
					map[i][j] = 0;
					break;

				case 'X':
					map[i][j] = -1;
					break;

				default:
					map[i][j] = line[j] - '0';
					break;
				}
			}
		}

		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			if (i == 0)
			{
				cnt = cnt + TimeFrom(10);
			}
			else
			{
				cnt = cnt + TimeFrom(i);
			}
		}

		cout << cnt << endl;
	}


	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
queue <pair<int ,int> > po;
int memo[1005][1005],ans,h,w,n, miti[1005][1005];
char c;
bool flag=false;


int main(){

    cin >> h >> w >> n;
    fill((int*)miti,(int*)miti+1005,100);

    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin >> c;
            if(c=='S'){
                po.push(make_pair(j,i));
                miti[j][i]=0;
            }else if(c=='X')miti[j][i]=100;
            else if(c=='.')miti[j][i]=0;
            else miti[j][i]=c-'0'+10;
        }
    }

    for(int hp=1;hp<=n;hp++){
            flag=false;
            printf("hp=%d\n",hp);

        while(!po.empty()){
        int px,py;
        px=po.front().first;
        py=po.front().second;
        po.pop();
         printf("%d %d\n",px,py);

    int xx[4]={0,1,0,-1};
    int yy[4]={1,0,-1,0};

    if(px<1||py<1||px>w||py>h);
    else{
    for(int i=0;i<4;i++){
            if(miti[px+xx[i]][py+yy[i]]==hp+10){
                ans+=memo[px][py]+1;
                  printf("%d=(%d %d)\nans=%d\n",hp,px+xx[i],py+yy[i],ans);
                flag=true;
                while(!po.empty()){
                    po.pop();
                }
                po.push(make_pair(px+xx[i],py+yy[i]));
                fill((int*)memo,(int*)memo+1005,0);
                break;
        }else if((miti[px+xx[i]][py+yy[i]]<hp||miti[px+xx[i]][py+yy[i]]>10)&&miti[px+xx[i]][py+yy[i]]!=100){
            po.push(make_pair(px+xx[i],py+yy[i]));
            memo[px+xx[i]][py+yy[i]]=memo[px][py]+1;
            if(miti[px+xx[i]][py+yy[i]]>10&&miti[px+xx[i]][py+yy[i]]!=100);
            else miti[px+xx[i]][py+yy[i]]=hp;
                }
            }
            if(flag==true)break;
    }
        }
    }


    cout << ans << endl;


return 0;
}

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

using namespace std;

const int INF = 10000;
const int MAX_H = 1003;
const int MAX_W = 1003;
int H, W, N;
char blocks[MAX_H][MAX_W];
int init_row, init_col;

int init() {
	fill_n((char*)blocks, MAX_H * MAX_W, 'X');
	cin >> H >> W >> N;
	for (int i = 1; i <= H; i++) {
		scanf("%s", blocks[i] + 1);
		blocks[i][strlen(blocks[i])] = 'X';
	}
	
	H += 2;
	W += 2;
	
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (blocks[i][j] == 'S') {
				init_row = i;
				init_col = j;
			}
		}
	}
	
	return 0;
}

int bfs(int start_row, int start_col, int goal_row, int goal_col) {
	queue<pair<int, int>> que;
	que.emplace(start_row, start_col);
	
	int d[MAX_H][MAX_W];
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			d[i][j] = INF;
		}
	}
	d[start_row][start_col] = 0;
	
	while (!que.empty()) {
		auto loc = que.front();
		que.pop();
		
		int i = loc.first;
		int j = loc.second;
		
		int di[] = {1, -1, 0, 0};
		int dj[] = {0, 0, 1, -1};
		
		if (i == goal_row && j == goal_col) return d[i][j];
		
		for (int k = 0; k < 4; k++) {
			int ti = i + di[k];
			int tj = j + dj[k];
			
			if (blocks[ti][tj] != 'X') {
				if (d[ti][tj] > d[i][j] + 1) {
					d[ti][tj] = d[i][j] + 1;
					que.emplace(ti, tj);
				}
			}
		}
	}
	
	return 0;
}

void solve() {
	vector<int> to_i(N);
	vector<int> to_j(N);
	
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			char ch = blocks[i][j];
			if ('1' <= ch && ch <= '9') {
				to_i[(int)(ch - '1')] = i;
				to_j[(int)(ch - '1')] = j;
			}
		}
	}
	
	int s_i = init_row;
	int s_j = init_col;
	
	int ans = 0;
	for (int i = 0; i < N; i++) {
		int n = bfs(s_i, s_j, to_i[i], to_j[i]);
		ans += n;
		s_i = to_i[i];
		s_j = to_j[i];
	}
	
	cout << ans << endl;
}

int main() {
	init();
	solve();
}