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<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<memory>
#define FOR(i,R) for(int i=0;i<R;i++)
#define ZERO(a) memset(a,0,sizeof(a))
using namespace std;
typedef pair<int,int> P;

const int INF=10000;

char maze[1024][1024];
int H,W,N;
int sx,sy;
int gx,gy;

int e[16][3];
int d[1024][1024];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int bfs(){
	queue<P> que;
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)d[i][j]=INF;
	que.push(P(sx,sy));
	d[sx][sy]=0;
	
	while(que.size()){
		P p=que.front(); que.pop();
		if(p.first==gx&&p.second==gy)break;
		for(int i=0;i<4;i++){
			int nx=p.first+dx[i],ny=p.second+dy[i];
			if(0<=nx&&nx<H&&0<=ny&&ny<W&&maze[nx][ny]!='X'&&d[nx][ny]==INF){
				que.push(P(nx,ny));
				d[nx][ny]=d[p.first][p.second]+1;
			}
		}
	}
	return d[gx][gy];
}

int main(){
	int i,j,k=0,t=0;
	cin>>H>>W>>N;
	FOR(i,H)FOR(j,W){
		cin>>maze[i][j];
		if(maze[i][j]=='S'){
			sx=i;sy=j;
		}else if('0'<=maze[i][j]&&maze[i][j]<='9'){
			k=maze[i][j]-'0';
			e[k][0]=i;e[k][1]=j;
		}
	}
	
	FOR(i,N){
		if(i==0){
			gx=e[1][0];
			gy=e[1][1];
		}else{
			sx=e[i][0];
			sy=e[i][1];
			gx=e[i+1][0];
			gy=e[i+1][1];
		}
		t+=bfs();
	}
	cout<<t<<endl;
}

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

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)

typedef pair<int, int> P;

int H, W, N;

char v[1001][1001];
bool visit[1001][1001];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int bfs(P s, P g){
	memset(visit, 0, sizeof(visit));
	queue<pair<int, P> > q;
	q.push(make_pair(0, s));
	int res = 0;
	while(!q.empty()){
		P p = q.front().second;
		int d = q.front().first;
		q.pop();

		if(visit[p.second][p.first]) continue;
		visit[p.second][p.first] = true;
		if(p == g) return d;

		rep(i, 4){
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
			if(v[ny][nx] == 'X') continue;
			q.push(make_pair(d+1, make_pair(nx, ny)));
		}
	}

	return -1;
}

int main(){
	cin >> H >> W >> N;
	vector<P> pos(10);
	rep(i, H) rep(j, W){
		cin >> v[i][j];
		if(v[i][j] == 'S') pos[0] = make_pair(j, i);
		for(int k = '1'; k <= '9'; k++){
			if(v[i][j] == k) pos[k-'0'] = make_pair(j, i);
		}
	}
	int ans = 0;
	rep(i, N){
		ans += bfs(pos[i], pos[i+1]);
	}
	cout << ans << endl;

	return 0;
}

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

using namespace std;

int W, H, N;

struct Vector2{
	int x;
	int y;
};

bool exist(int map[2000][2000], Vector2 p, int step){
	if(p.x < 0 || p.y < 0 || p.x >= W || p.y >= H){
		return false;
	}
	if(map[p.y][p.x] > step + 1){
		map[p.y][p.x] = step + 1;
		return true;
	}
	return false;
}

int map[2000][2000];

int main(){
	
	string map_[2000];
	
	cin >> H >> W >> N;
	
	for(int i = 0; i < H; i++){
		cin >> map_[i];
	}
	
	int ans = 0;
	
	Vector2 start, goal, punct, p, p1;
	
	punct.x = -1;
	punct.y = -1;
	
	queue<Vector2> que;
	
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(map_[i][j] == 'S'){
				start.x = j;
				start.y = i;
				map[i][j] = 0;
			}
		}
	}
	
	for(int loop = 0; loop < N; loop++){
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				if(map_[i][j] == 'X'){
					map[i][j] = -1;
				}else if(map_[i][j] == '0' + loop){
					start.x = j;
					start.y = i;
					map[i][j] = 0;
				}else if(map_[i][j] == '0' + loop + 1){
					goal.x = j;
					goal.y = i;
					map[i][j] = 1 << 30;
				}else{
					map[i][j] = 1 << 30;
				}
			}
		}
		que.push(start);
		que.push(punct);
		
		bool checker = false;
		
		for(int step = 0; step < 3000; step++){
			while(true){
				p = que.front();
				que.pop();
				if(p.x == punct.x && p.y == punct.y){
					que.push(punct);
					break;
				}else if(p.x == goal.x && p.y == goal.y){
					checker = true;
					break;
				}
				p1.x = p.x - 1;
				p1.y = p.y;
				if(exist(map, p1, step)){
					que.push(p1);
				}
				p1.x = p.x + 1;
				p1.y = p.y;
				if(exist(map, p1, step)){
					que.push(p1);
				}
				p1.x = p.x;
				p1.y = p.y - 1;
				if(exist(map, p1, step)){
					que.push(p1);
				}
				p1.x = p.x;
				p1.y = p.y + 1;
				if(exist(map, p1, step)){
					que.push(p1);
				}
			}
			if(checker){
				ans += step;
				break;
			}
		}
		
		while(!que.empty()){
			que.pop();
		}
		
		/*
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				if(map[i][j] == 1 << 30){
					printf("0 ");
				}else{
					printf("%d ", map[i][j]);
				}
			}
			printf("\n");
		}
		*/
	}
	
	printf("%d\n", ans);
	
	return 0;
}

C++: // AOJ 0558
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>
#include<vector>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

typedef pair<int,int> P;

const int HMAX = 1008, WMAX = 1008;
char city[HMAX][WMAX];
int H, W, N;

P search(char c) {
  REP(y,H) REP(x,W) {
    if (city[x][y] == c) {
      return make_pair(x,y);
    }
  }
}


bool legal(int x, int y) {
  return 0 <= x and x < W and 0 <= y and y < H;
}

typedef pair<int,P> Dat;

int distance(char start, char goal) {
  P startp = search(start), goalp = search(goal);
  priority_queue<Dat,vector<Dat>,greater<Dat> > q;
  set<P> s;
  q.push(Dat(0,startp));
  s.insert(startp);
  while (!q.empty()) {
    Dat d = q.top(); q.pop();
    P p = d.second;
    if (p == goalp) return d.first;
    int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
    REP(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      P np = P(nx,ny);
      if (legal(nx,ny) and city[nx][ny] != 'X' 
          and s.find(np) == s.end()) {
        q.push(Dat(d.first + 1, np));
        s.insert(np);
      }
    }
  }
  throw 1;
}

int solve() {
  char current = 'S';
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += distance(current, '0' + i);
    current = '0' + i;
  }
  return ans;
}

int main() {
  cin >> H >> W >> N;
  REP(y, H) {
    string row;
    cin >> row;
    REP(x, W) {
      city[x][y] = row[x];
    }
  }

  cout << solve() << endl;
}