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 <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <utility>
using namespace std;
typedef pair <int,int> p;
char maze[1005][1005];
int d[1005][1005];
int dirx[4]={0,-1,0,1};
int diry[4]={1,0,-1,0};
int h,w,n,hard;
int si,sj;
const int INF=0x3f3f3f3f;
int tot=0;
void BFS(int ai,int aj)
{
	queue<p> que;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			d[i][j]=INF;
		}
	 } 
	 que.push(p(ai,aj));
	 d[ai][aj]=0;
	 while(!que.empty())
	 {
	 	p a=que.front();
	 	que.pop();
	 	int x=a.first,y=a.second;
	 	if(maze[x][y]-'0'==hard)
	 	{
	 		tot+=d[x][y];
	 		si=x;
	 		sj=y;
	 		break;
		  }
		  for(int i=0;i<4;i++)
		  {
		  	int dx=x+dirx[i],dy=y+diry[i];
		  	if(dx>=0&&dx<h&&dy>=0&&dy<w&&maze[dx][dy]!='X'&&d[dx][dy]==INF)
		  	{
		  		d[dx][dy]=d[x][y]+1;
		  		que.push(p(dx,dy));
			  }
		   } 
	  } 
}
int main()
{
	while(scanf("%d%d%d",&h,&w,&n)!=EOF)
	{
		getchar();
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				scanf("%c",&maze[i][j]);
				if(maze[i][j]=='S')
				si=i;sj=j;
			}getchar();
		}
		tot=0;
		hard=1;
		for(int i=0;i<n;i++)
		{
			BFS(si,sj);
			hard++;
		}cout<<tot<<endl;
	}return 0;
}

C++: #include<iostream>
using namespace std;
#include<stdio.h>
#include<queue>
int main(){
int h,w,n,i,j,k,time=0;
int m[102][102]={0};
pair<int,int> ht,s;
char c[2001];
queue<pair<int,int> > q,st;
scanf("%d%d%d",&h,&w,&n);
for(i=0;i<=h+1;i++){
	for(j=0;j<=w+1;j++){
		if(i==0||i==h+1||j==0||j==w+1){
		m[i][j]=10;
		}
	}
}
for(i=1;i<=h;i++){
	scanf("%s",&c);
	for(j=0;j<w;j++){
		if(c[j]=='S'){
			s.first=i;
			s.second=j+1;
			q.push(s);
			m[i][j+1]=11;
		}
		else if(c[j]=='X'){
			m[i][j+1]=10;
		}
		else if(c[j]=='1')
			m[i][j+1]=1;
		else if(c[j]=='2')
			m[i][j+1]=2;
		else if(c[j]=='3')
			m[i][j+1]=3;
		else if(c[j]=='4')
			m[i][j+1]=4;
		else if(c[j]=='5')
			m[i][j+1]=5;
		else if(c[j]=='6')
			m[i][j+1]=6;
		else if(c[j]=='7')
			m[i][j+1]=7;
		else if(c[j]=='8')
			m[i][j+1]=8;
		else if(c[j]=='9')
			m[i][j+1]=9;
	}
}
ht.first=1;
ht.second=0;
st.push(ht);
while(!q.empty()){
	s=q.front();
	ht=st.front();
	q.pop();
	st.pop();
	ht.second++;
	if(ht.first==m[s.first][s.second]){
		ht.first++;
	}
	if(m[s.first+1][s.second]!=10){
		s.first++;
		q.push(s);
		s.first--;
		st.push(ht);
	}
	if(m[s.first-1][s.second]!=10){
		s.first--;
		q.push(s);
		s.first++;
		st.push(ht);
	}
	if(m[s.first][s.second+1]!=10){
		s.second++;
		q.push(s);
		s.second--;
		st.push(ht);
	}
	if(m[s.first][s.second-1]!=10){
		s.second--;
		q.push(s);
		s.second++;
		st.push(ht);
	}
	ht.second--;
	if(ht.first==n+1){
			time=ht.second;
			break;
	}

}
printf("%d\n",time);
return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <queue>


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

#define HMAX 1000
#define WMAX 1000
#define NMAX 9
#define INF 100000000
#define EMPTY -1
#define OBSTACLE -2

int H, W, N;
int map[HMAX + 2][WMAX + 2];

int dirs_h[] = {0, 0, 1, -1};
int dirs_w[] = {1, -1, 0, 0};

int factory[NMAX + 1][2];

using namespace std;

int go(int sh, int sw, int eh, int ew) {
	queue<int> q_h;
	queue<int> q_w;
	q_h.push(sh);
	q_w.push(sw);

	int cost[HMAX][WMAX];
	rep(i, HMAX)
		rep(j, WMAX)
			cost[i][j] = INF;
	cost[sh][sw] = 0;
	int min = 0;
	while (!q_h.empty()) {
		int p_h = q_h.front();
		int p_w = q_w.front();

		q_h.pop();
		q_w.pop();
		
		rep(i, 4){
			int pn_h = p_h + dirs_h[i];
			int pn_w = p_w + dirs_w[i];

			if (map[pn_h][pn_w] != OBSTACLE) {
				if (map[pn_h][pn_w] == EMPTY && cost[pn_h][pn_w] == INF) {
					q_h.push(pn_h);
					q_w.push(pn_w);
				}else{
					if (min > cost[pn_h][pn_w] + 1)
						min = cost[pn_h][pn_w] + 1;
				}
			}
		}
		cost[p_h][p_w] = min;
		min = INF;
		if (p_h == eh && p_w == ew)
			break;
	}
	int ans = cost[eh][ew];
	return ans;
}

int main() {
	scanf("%d%d%d", &H, &W, &N);
	
	rep(i, H){
		rep(j, W){
			char m;
			scanf(" %c",&m);
			int m_i = m - '0';
			switch (m) {
				case 'S':
					factory[0][0] = i + 1;
					factory[0][1] = j + 1;
					map[i + 1][j + 1] = EMPTY;
				break;
				case 'X':
					map[i + 1][j + 1] = OBSTACLE;
				break;
				case '.':
					map[i + 1][j + 1] = EMPTY;
				break;
				default:
					int num = m - '0';
					factory[num][0] = i + 1;
					factory[num][1] = j + 1;
					map[i + 1][j + 1] = EMPTY;
				break;
			}
		}
	}

	rep(i, H + 2){
		map[i][0] = OBSTACLE;
		map[i][W + 1] = OBSTACLE;
	}

	rep(j, W + 2){
		map[0][j] = OBSTACLE;
		map[H + 1][j] = OBSTACLE;
	}
	cout << "loading complete." << endl;
	int sum = 0;
	rep(i, N) {
		sum += go(
			factory[i][0], factory[i][1],
			factory[i + 1][0], factory[i + 1][1]);
		cout << "reached factory " << i + 1 << endl;
	}

	cout << sum << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

int h, w, n;
char m[1010][1010];
int d[1010][1010];
bool f[1010][1010];

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

void init()
{
  for (int i = 0; i < h; i++){
    for (int j = 0; j < w; j++){
      d[i][j] = 1e9 + 9;
      f[i][j] = false;
    }
  }
}

int main()
{
  int sx, sy;

  cin >> h >> w >> n;
  for (int i = 0; i < h; i++){
    for (int j = 0; j < w; j++){
      cin >> m[i][j];
      if (m[i][j] == 'S') sx = j, sy = i;
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++){
    queue<pair<int, int> > q;
    q.push(make_pair(sy, sx));

    init();
    f[sy][sx] = true;
    d[sy][sx] = 0;
    while (!q.empty()){
      pair<int, int> nv = q.front();
      q.pop();

      if (m[nv.first][nv.second] == '0' + i){
        sy = nv.first;
        sx = nv.second;
        ans += d[sy][sx];
        while (!q.empty()) q.pop();
        break;
      }

      for (int j = 0; j < 4; j++){
        int ny = nv.first + dy[j];
        int nx = nv.second + dx[j];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
        if (f[ny][nx]) continue;
        if (m[ny][nx] == 'X') continue;
        d[ny][nx] = d[nv.first][nv.second] + 1;
        q.push(make_pair(ny, nx));
        f[ny][nx] = true;
      }
    }
  }

  cout << ans << endl;
}