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<string>
#include<queue>
#define MAX 1002
using namespace std;

char xy[MAX][MAX];
int cost[MAX][MAX];
int hp[MAX][MAX];

const int INF=1>>21;

struct state{
  int x;
  int y;
  int co;
  int hp;
  state(){}
  state(int x,int y,int co,int hp):
    x(x),y(y),co(co),hp(hp){}
};

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int H,W,N;

int solve(int x,int y){
  int i,j;
  queue<state> q;
  q.push( state(x,y,0,1) );
  cost[x][y]=0;
  hp[x][y]=1;

  while( !(q.empty()) ){

    state p = q.front();
    q.pop();

    x=p.x;
    y=p.y;

    int hpf=0;

    for(i=0;i<4;i++){

      if( x+dx[i] < 0 || x+dx[i] >= W || y+dy[i] < 0 || y+dy[i] >= H )
	continue;

      if(xy[y+dy[i]][x+dx[i]]=='X') continue;

      if(cost[x+dx[i]][y+dy[i]]<p.co+1){
	if(hp[x+dx[i]][y+dy[i]]>=p.hp){
	  continue;
	}
      }
      
      if(xy[y+dy[i]][x+dx[i]]-'0' == p.hp){
	hpf=1;
	
	if(p.hp >= N){
	  return p.co+1;
	}
	
	while( !(q.empty()) ){
	  q.pop();
	}
	
	q.push( state(x+dx[i],y+dy[i],p.co+1,p.hp+hpf) );
	
	break;
	
      }
      
      cost[x][y]=p.co;
      hp[x][y]=p.hp;

      q.push( state(x+dx[i],y+dy[i],p.co+1,p.hp) );


    }

  }

  return -1;
}

main(){
  int i,j,sx,sy;

  cin >> H >> W >> N;

  for(i=0;i<H;i++){
    scanf("%s",xy[i]);
    for(j=0;j<W;j++){
      if(xy[i][j]=='S'){
	sx=j;
	sy=i;
      }
    }
  }

  int n=solve(sx,sy);

  cout << n << endl; 
  
  return 0;
}

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

typedef pair<int, int> P;
vector<P> v(10);

int h, w, n;
char map[1000][1001];

const int INF = 100000000;
int d[1000][1000];

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

int bfs(int t){
	queue<P> que;
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			d[j][i] = INF;
		}
	}
	que.push(v[t]);
	d[v[t].first][v[t].second] = 0;
	while(que.size()){
		P p = que.front(); que.pop();
		if(p.first == v[t+1].first && p.second == v[t+1].second) break;
		for(int i=0; i<4; i++){
			int nx = p.second + dx[i], ny = p.first + dy[i];

			if(0 <= nx && nx < w && 0 <= ny && ny < h && map[ny][nx] != 'X' && d[ny][nx] == INF){
				que.push(P(ny, nx));
				d[ny][nx] = d[p.first][p.second] + 1;
			}
		}
	}
	return d[v[t+1].first][v[t+1].second];
}

void solve(){
	scanf("%d %d %d", &h, &w, &n);
	for(int i=0; i<h; i++){
		scanf("%s", map[i]);
	}
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			if(map[j][i] == 'S') v[0] = (P(j, i));
		}
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<w; j++){
			for(int k=0; k<h; k++){
				if(map[k][j] - '0' == i+1){
					v[i+1] = (P(k, j));
				}
			}
		}
	}
	int ans = 0;
	for(int i=0; i<n; i++){
		ans += bfs(i);
	}
	printf("%d\n", ans);
}

int main(){
	solve();
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int HMAX=1000,WMAX=1000,INF=INT_MAX;
const int m[4][2]={{-1,0},{0,-1},{0,1},{1,0}}; 

char A[HMAX][WMAX+1];
int S[HMAX][WMAX];
deque<PII> Q;
int h,w,maxn;

void init()
{
	for(int i=0;i<h;i++)for(int j=0;j<w;j++)S[i][j]=INF;
}

int bfs(int sx,int sy)
{
	init();
	int now=1;
	S[sx][sy]=0;
	Q.push_back(make_pair(sx,sy));
	while(!Q.empty())
	{
		bool restart=false;
		int x=Q.front().first,y=Q.front().second;Q.pop_front();
		//printf("x=%d, y=%d, s=%d\n",x,y,S[x][y]);
		for(int i=0;i<4;i++)
		{
			int nx=x+m[i][0],ny=y+m[i][1],ns=S[x][y]+1;
			//printf("push: nx=%d ny=%d ns=%d\n",nx,ny,ns);
			if(nx<0||nx>=h)continue;
			if(ny<0||ny>=w)continue;
			if(A[nx][ny]=='X'||S[nx][ny]<=ns)continue;
			//printf("PASS\n");
			if(A[nx][ny]==(now+'0'))
			{
				//printf("end: nx=%d, ny=%d, ns=%d\n",nx,ny,ns);
				if(now>=maxn)return ns;
				++now;
				Q.clear();
				init();
				restart=true;
			}
			S[nx][ny]=ns;
			Q.push_back(make_pair(nx,ny));
			if(restart)break;
		}
	}
	//printf("NO MORE\n"); 
}

int main()
{
	int sx,sy;
	scanf("%d%d%d",&h,&w,&maxn);
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			scanf(" %c",&A[i][j]);
			if(A[i][j]=='S')sx=i,sy=j;
		}
	}
	printf("%d\n",bfs(sx,sy));
	return 0;
}