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 <cstdio>
#include <iostream>
#include <queue>
#include <string.h>

using namespace std;

int W, H, N;
typedef pair<int, int> P;
queue<P> que;
char maze[1003][1003];
int timer[1003][1003];
int pos_x, pos_y;
int sh_x[4] = {0, 0, -1, 1};
int sh_y[4] = {1, -1, 0, 0};
int res = 0;
int strength = 1;

void solve() {
  que.push(P(pos_x, pos_y));

  while(que.size()) {
    P pos = que.front(); que.pop();
    if (maze[pos.second][pos.first] - '0' == strength) {
      //printf("[%d] [%d]\n", pos.second, pos.first);
      while(que.size()) que.pop();
      strength++;
      if (strength == N+1) {
        res = timer[pos.second][pos.first];
        break;
      }

      for (int i=0;i<H;i++) {
        for (int j=0;j<W;j++) {
          if (maze[i][j] == '#') maze[i][j] = '.';
        }
      }

      maze[pos.second][pos.first] = '#';
      que.push(pos);
      continue;
    }
    for (int i=0;i<4;i++) {
      int new_x = pos.first + sh_x[i];
      int new_y = pos.second + sh_y[i];
      if (new_x >= 0 && new_x < W && new_y >=0 && new_y < H && maze[new_y][new_x] != '#' && maze[new_y][new_x] != 'X') {
        //printf("Go to [%d] [%d]\n", new_y, new_x);
        que.push(P(new_x, new_y));
        if (maze[new_y][new_x] == '.') {
          maze[new_y][new_x] = '#';
        }
        timer[new_y][new_x] = timer[pos.second][pos.first] + 1;
      }
    }
  }
}

int main() {
  while (1)
  {
    scanf("%d %d %d", &H, &W, &N);
    getchar();
    if (!W && !H) {
      break;
    }
    
    for (int i = 0; i < H; i++)
    {
      fgets(maze[i], 1002, stdin);
      maze[i][W] = '#';
    }

    for (int i=0;i<H;i++) {
      for (int j=0;j<W;j++) {
        if (maze[i][j] == 'S') {
          pos_x = j;
          pos_y = i;
        }
        if (i==H-1) {
          maze[H][j] = '#';
        }
      }
    }

    solve();
    printf("%d\n", res);
    memset(timer, 0, sizeof(timer[0][0]) * H * W);
    strength = 1;
  }
  return 0;
}

C++: #include<iostream>

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

int h,w,PX[4]={1,-1,0,0},PY[4]={0,0,1,-1};
char ch[100][100];
int sum=-1;

void dfs(P p){
	ch[p.first][p.second]=',';	
	for(int i=0;i<4;++i){
		if(p.first+PY[i]>=0 && h>p.first+PY[i] && w>p.second+PX[i] && p.second+PX[i]>=0){
		  if(ch[p.first][p.second]==ch[p.first+PY[i]][p.second+PX[i]]){
			dfs(P(p.first+PY[i],p.second+PX[i]));
		  }
		}
	}
}

void solve(){
	for(int i=0;i<h;++i){
		for(int j=0;j<w;++j){
			if(ch[i][j]!=','){
				++sum;
				dfs(P(i,j));
			}
		}
	}
}

int main(){
	while(1){
		std::cin>>h>>w;
		if(h==0&&w==0)
			break;
		for(int i=0;i<h;++i){
			for(int j=0;j<w;++j){
				std::cin>>ch[i][j];
			}
		}
		solve();
		std::cout<<sum<<std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;
const int maxn = 1005;
const int inf = 1e6;

typedef pair<int, int> P;
char data[maxn][maxn];
int dist[maxn][maxn];
int sx, sy, w, h, n, sum;
P fac[15];

void initialize(){
    for(int i=0; i<h; ++i)
    {
        for(int j=0; j<w; ++j)
        {
            dist[i][j] = inf;
        }
    }
}

void bfs(P s, P g)
{
    int a[4] = {-1, 0, 1, 0}, b[4] = {0, -1, 0, 1};
    queue<P> que;
    que.push(s);


    dist[sx][sy] = 0;

    while(!que.empty())
    {
        P cur = que.front();
        que.pop();
        int x = cur.first, y = cur.second;
        if(cur==g)
        {
            return;
        }

        for(int i=0; i<4; ++i)
        {
            int nx = x + a[i], ny = y + b[i];
            if(nx>=0 && nx<h && ny>=0 && ny<w && data[nx][ny]!='X' && dist[nx][ny]==inf)
            {
                que.push(P(nx, ny));
                dist[nx][ny] = min(dist[nx][ny], dist[x][y]+1);
            }
        }
    }
}

int main()
{
    //while(scanf("%d%d", &w, &h)!= EOF && w && h){
    scanf("%d%d%d", &h, &w, &n);
    for(int i=0; i<h; ++i)
        scanf("%s", &data[i]);

    for(int i=0; i<h; ++i)
    {
        for(int j=0; j<w; ++j)
        {
            if(data[i][j]=='S')
            {
                fac[0] = P(i,j);
            }
            else if(data[i][j]>='1' && data[i][j]<='9')
                fac[data[i][j]-'0'] = P(i,j);
        }
    }
    sum = 0;
    for(int i=1; i<=n; ++i)
    {
        int x = fac[i-1].first, y = fac[i-1].second;
        initialize();
        dist[x][y] = 0;
        bfs(fac[i-1], fac[i]);
        data[x][y] = '.';
        sum += dist[fac[i].first][fac[i].second];
    }
    printf("%d\n", sum);

    //}
    return 0;
}

C++: #include<iostream>
#include<queue>
#define fi first
#define se second
using namespace std;
int dy[4]={0,0,-1,1},dx[4]={1,-1,0,0};
int X,Y,N;
char field[1000][1001];
pair<int,int> time[1000][1001];
struct point{
    int y,x,t,n;
    point(int y_,int x_,int t_,int n_){
        y=y_;x=x_;t=t_;n=n_;
    }
};
int bfs(int y,int x);
int main(){
    cin>>Y>>X>>N;
    int rep;
    for(int i=0;i<Y;i++)cin>>field[i];
    for(int i=0;i<Y;i++){
        for(int j=0;j<X;j++){
            time[i][j].fi=time[i][j].se=-1;
        }
    }
    for(int i=0;i<Y;i++){
        bool flag=false;
        for(int j=0;j<X;j++){
            if(field[i][j]=='S'){
                rep=bfs(i,j);
                flag=true;
                break;
            }
        }
        if(flag)break;
    }
    cout<<rep<<endl;
    return 0;

}

int bfs(int y,int x){
    point start=point(y,x,0,0);
    queue<point>Q;
    Q.push(start);
    while(Q.size()){

        point a=Q.front();
        Q.pop();
        if(a.y<0 || a.y>=Y ||a.x<0||a.x>=X)continue;

        if(field[a.y][a.x]=='X')continue;

        if(a.n+1==field[a.y][a.x]-'0')a.n++;

        if(a.n>time[a.y][a.x].se){
            time[a.y][a.x].fi=a.t;
            time[a.y][a.x].se=a.n;
        }else if(a.n==time[a.y][a.x].se&&a.t<time[a.y][a.x].fi){
            time[a.y][a.x].fi=a.t;
        }else continue;

        if(a.n==N){
            return a.t;
        }


        for(int i=0;i<4;i++){
            point next=point(a.y+dy[i],a.x+dx[i],a.t+1,a.n);
            Q.push(next);
        }
    }
}