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 <vector>
#include <iostream>

const int STEP_NUM = 4;
const int STEP[STEP_NUM][STEP_NUM] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

const int MAX_SIZE = 1000;
char field[MAX_SIZE + 1][MAX_SIZE + 1];

const int MAXN = 10;
int locationsX[MAXN];
int locationsY[MAXN];
 
int H, W, N;

int get_shortest_distance(int current_HP)
{
	int sx = locationsX[current_HP-1];
	int sy = locationsY[current_HP-1];
	int ex = locationsX[current_HP];
	int ey = locationsY[current_HP];

	bool is_visit[MAX_SIZE + 1][MAX_SIZE + 1]={};

	std::vector<std::pair<int,int>> frontier;
	frontier.push_back(std::pair<int,int>{sx,sy});
	int nx,ny;
	for(int step=0;step<W*H;step++)
	{
		std::vector<std::pair<int,int>> next_frontier;
		for(auto p:frontier)
		{
			if(p.first==ex && p.second==ey)	return step;
			is_visit[p.first][p.second] = true;
			for(int i=0;i<STEP_NUM;i++)
			{
				nx = p.first+STEP[i][0];
				ny = p.second+STEP[i][1];
				if(nx<0 || H<=nx) 		continue;
				if(ny<0 || W<=ny) 		continue;
				if(field[nx][ny]=='X') 	continue;
				if(is_visit[nx][ny]) 	continue;
				next_frontier.push_back(std::make_pair(nx,ny));
			}
		}
		frontier = next_frontier;
	}
	return -1;
}

int solve()
{
	int total_step = 0;
	for(int i=1;i<N+1;i++)
		total_step += get_shortest_distance(i);
	return total_step;
}

int main()
{
	scanf("%d %d %d", &H, &W, &N);
	for(int i=0; i<H; i++)
		scanf("%s", field[i]);
	for(int i=0; i<H; i++)
		for(int j=0; j<W; j++)
		{
			int HP=-1;
			if(field[i][j]=='S')	HP=0;
			if(field[i][j]=='1')	HP=1;
			if(field[i][j]=='2')	HP=2;
			if(field[i][j]=='3')	HP=3;
			if(field[i][j]=='4')	HP=4;
			if(field[i][j]=='5')	HP=5;
			if(field[i][j]=='6')	HP=6;
			if(field[i][j]=='7')	HP=7;
			if(field[i][j]=='8')	HP=8;
			if(field[i][j]=='9')	HP=9;
			if(HP!=-1)
			{
				locationsX[HP]=i;
				locationsY[HP]=j;
			}
		}
	printf("%d\n", solve());
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int,int> _P;
typedef pair<_P,int> P;
int W,H,N;
_P fact[10];
char map[1000][1001];
bool vst[1000][1000];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int bfs(int n) {
	memset(vst,0,sizeof(vst));
	char chr=n+1+'0';
	queue<P> que;
	que.push(P(fact[n],0));
	while(!que.empty()) {
		P p=que.front();que.pop();
		if(map[p.first.second][p.first.first]==chr) {
			return p.second;
		}
		for(int i=0;i<4;i++) {
			int nx=p.first.first+dx[i];
			int ny=p.first.second+dy[i];
			if(0>nx||nx>=W||0>ny||ny>=H)continue;
			if(map[ny][nx]=='X'||
				vst[nx][ny]==true)continue;
			vst[nx][ny]=true;
			que.push(P(_P(nx,ny),p.second+1));
		}
	}
	return 0;
}
int main() {
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++) {
		scanf("%s",map[i]);
	}
	for(int i=0;i<=N;i++) {
		for(int j=0;j<H;j++) {
			char *cp=strchr(map[j],i==0?'S':(i+'0'));
			if(cp!=NULL) {
				fact[i].first=(cp-map[j]);
				fact[i].second=j;
				break;
			}
		}
	}
	int res=0;
	for(int i=0;i<N;i++) {
		res+=bfs(i);
	}
	printf("%d\n",res);
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include <algorithm>
#include<map>
#define MAX 1001
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;

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

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

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

    if(cost[x][y]<p.co){
      if(hp[x][y]==p.hp){
	continue;
      }
    }
    cost[x][y]=p.co;
    hp[x][y]=p.hp;


    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(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;

      }

      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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

const int INF = 1e9;
const vector<int> dx = {1, 0, -1,  0};
const vector<int> dy = {0, 1,  0, -1};

int h, w, n, sx, sy;
vector<string> board;

int walk(char goal) {
	vector<vector<int>> stat(h, vector<int>(w, INF));
	stat[sy][sx] = 0;

	queue<pair<int, int>> que;
	que.push(make_pair(sx, sy));

	while (!que.empty()) {
		auto p = que.front();
		que.pop();

		int x = p.first;
		int y = p.second;

		for (int i = 0; i < 4; i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
				continue;
			} else if (stat[ny][nx] != INF || board[ny][nx] == 'X') {
				continue;
			}

			stat[ny][nx] = stat[y][x] + 1;
			if (board[ny][nx] == goal) {
				sx = nx;
				sy = ny;
				return stat[ny][nx];
			}
			que.push(make_pair(nx, ny));
		}
	}
	return -1;
}


int main() {
	cin >> h >> w >> n;
	board.assign(h, "");
	for (int i = 0; i < h; i++) {
		cin >> board[i];
	}

	sx = sy = -1;

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (board[i][j] == 'S') {
				sx = j;
				sy = i;
				break;
			}
		}
		if (sx != -1) {
			break;
		}
	}

	int steps = 0;
	for (int i = 1; i <= n; i++) {
		steps += walk(i + '0');
		// cout << "step to " << i << ": " << steps << endl;
	}
	cout << steps << endl;
	return 0;
}