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>
using namespace std;
int h,w,n,ans=0;
char c[1002][1002];
pair<int,int> a[12],pi;
bool f[1002][1002];

int dist(pair<int,int> s,pair<int,int> g){
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)f[i][j]=false;
	//cout<<s.first<<","<<s.second<<" "<<g.first<<","<<g.second<<endl;
	queue<pair<pair<int,int>,int>> que;
	pair<pair<int,int>,int> p;
	p.first=s; p.second=0;
	que.push(p);
	while(que.size()>0){
		p=que.front(); que.pop();
		if(p.first==g)return p.second;
		f[p.first.first][p.first.second]=true;
		//cout<<p.first.first<<","<<p.first.second<<endl;
		//if(p.first.first==g.first&&p.first.second==g.second)return p.second;
		if(c[p.first.first-1][p.first.second]!='X'&&!f[p.first.first-1][p.first.second]){p.first.first--; p.second++; que.push(p); p.first.first++; p.second--; f[p.first.first-1][p.first.second]=true;}
		if(c[p.first.first+1][p.first.second]!='X'&&!f[p.first.first+1][p.first.second]){p.first.first++; p.second++; que.push(p); p.first.first--; p.second--; f[p.first.first+1][p.first.second]=true;}
		if(c[p.first.first][p.first.second-1]!='X'&&!f[p.first.first][p.first.second-1]){p.first.second--; p.second++; que.push(p); p.first.second++; p.second--; f[p.first.first][p.first.second-1]=true;}
		if(c[p.first.first][p.first.second+1]!='X'&&!f[p.first.first][p.first.second+1]){p.first.second++; p.second++; que.push(p); p.first.second--; p.second--; f[p.first.first][p.first.second+1]=true;}
	}
	return -1;
}
	

int main(){
	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)c[i][j]='X';
	for(int i=1;i<=h;i++){
		scanf("\n");
		for(int j=1;j<=w;j++){
			scanf("%c",&c[i][j]);
			if('1'<=c[i][j]&&c[i][j]<='9'){
				a[c[i][j]-48].first=i;
				a[c[i][j]-48].second=j;
			}
			if(c[i][j]=='S'){
				a[0].first=i; a[0].second=j;
			}
		}
	}
		/*for(int i=1;i<=h;i++){	
			for(int j=1;j<=w;j++){
				printf("%c",c[i][j]);
			}
			printf("\n");
		}*/
	for(int i=1;i<=n;i++){
		//cout<<i<<endl;
		ans+=dist(a[i-1],a[i]);
	}
	printf("%d\n",ans);
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <queue>

using namespace std;

#define rep(i, n) for (int i=0; i<int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_W 1010
#define MAX_H 1010

int H, W, N;
char A[MAX_H][MAX_W];
vector<bool**> global;

pair<int, int> find_char(char c) {
  rep(y, H) {
    rep(x, W) {
      if (A[y][x] == c) return make_pair(x, y);
    }
  }
  return make_pair(-1, -1);
}

bool visited[MAX_W][MAX_H];
int bfs(int start_x, int start_y, int target_x, int target_y) {
  queue<int> q;
  rep(i, W) rep(j, H) visited[i][j] = false;

  q.push(start_x); q.push(start_y); q.push(0);
  while (!q.empty()) {
    int x  = q.front(); q.pop();
    int y  = q.front(); q.pop();
    int dist = q.front(); q.pop();

    char m = A[y][x];
    if (x == target_x && y == target_y) return dist;

    visited[y][x] = true;
    int dx[] = {0, 1, 0, -1};
    int dy[] = {-1, 0, 1, 0};
    rep(i, 4) {
      rep(j, 4) {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if (tx >= 0 && tx < W && ty >= 0 && ty < H && A[ty][tx] != 'X') {
          if (visited[ty][tx]) continue;
          q.push(tx);
          q.push(ty);
          q.push(dist + 1);
        }
      }
    }
  }
  return INT_MIN;
}

int main() {
  cin >> H >> W >> N;
  rep(i, H) cin >> A[i];

  // ????§???????
  auto pos_s = find_char('S');
  int start_x = pos_s.first;
  int start_y = pos_s.second;

  int ans = 0;
  for (int i=1; i<=N; i++) {
    auto pos = find_char((char)((int)'0'+i));
    int tx = pos.first;
    int ty = pos.second;
    ans += bfs(start_x, start_y, tx, ty);

    start_x = tx;
    start_y = ty;
  }
  cout << ans << "\n";
  return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

void zero();

struct p{
	int x, y, cnt;
};
int H, W, N;
int tizu[1001][1001];
bool flag[1001][1001] = { false };
int main(){
	queue<p> que;
	char a;
	char h[10] = "123456789";
	cin >> H >> W >> N;
	for (int i = 0; i < H; i++){
		for (int j = 0; j < W; j++){
			cin >> a;
			if (a == 'S'){
				tizu[i][j] = 0;
				p temp;
				temp.x = j;
				temp.y = i;
				temp.cnt = 0;
				que.push(temp);
			}
			else if (a == 'X'){
				tizu[i][j] = -1;
			}
			else if (a == '.'){
				tizu[i][j] = 0;
			}
			else {
				for (int k = 0; k < 9; k++){
					if (h[k] == a){
						tizu[i][j] = k + 1;
					}
				}

			}
			//cout << tizu[i][j];
		}
		//cout << endl;
	}
	int max = 1;
	int step = 0;
	while (!que.empty()){
		p temp = que.front();
		que.pop();
		if (tizu[temp.y][temp.x] == max){
			max++;
			while (!que.empty()) que.pop();
			if (max - 1 == N) {
				max = temp.cnt;
				break;
			}
			zero();
		}
		if (flag[temp.y][temp.x] == true) continue;
		flag[temp.y][temp.x] = true;
		for (int i = 0; i < 4; i++){
			int dx[4] = { 1, -1, 0, 0 };
			int dy[4] = { 0, 0, 1, -1 };
			int yy = temp.y + dy[i];
			int xx = temp.x + dx[i];
			if (yy > -1 && xx > -1 && yy < H && xx < W && tizu[yy][xx] != -1){
				p tt;
				tt.y = yy;
				tt.x = xx;
				tt.cnt = temp.cnt + 1;
				que.push(tt);
			}
		}
	}
	cout << max << endl;
	return 0;
}
void zero(){
	for (int i = 0; i < H; i++){
		for (int j = 0; j < W; j++){
			flag[i][j] = false;
		}
	}
}