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 <vector>
#include <math.h>
using namespace std;

int H, M, N;

int go[4][2];

bool K[1002][1002];

int score[1002][1002];

int C[11][2];

int ans = 0;

void saiki(int sx, int sy, int s){
  if(K[sx][sy]){
    if(score[sx][sy] > s){
      score[sx][sy] = s;
      for(int i = 0; i < 4; i++){
        saiki(sx + go[i][0], sy + go[i][1], s + 1);
      }
    }
  }
}

int main(){
  
  go[0][0] = 1;
  go[0][1] = 0;
  go[1][0] = 0;
  go[1][1] = -1;
  go[2][0] = -1;
  go[2][1] = 0;
  go[3][0] = 0;
  go[3][1] = 1;
  
  for(int i = 0; i <= H + 1; i++){
    for(int j = 0; j <= M + 1; j++){
      score[i][j] = 2147483640;
      K[i][j] = false;
    }
  }
  
  scanf("%d%d%d", &H, &M, &N);
  
  for(int i = 0; i < H; i++){
    char str[M + 1];
    scanf("%s", str);
    for(int j = 0; j < M; j++){
      if(str[j] == '.'){
        K[i + 1][j + 1] = true;
      }else if(str[j] == 'X'){
        K[i + 1][j + 1] = false;
      }else if(str[j] == 'S'){
        C[0][0] = i + 1;
        C[0][1] = j + 1;
        K[i + 1][j + 1] = true;
      }else{
        C[str[j] - '0'][0] = i + 1;
        C[str[j] - '0'][1] = j + 1;
        K[i + 1][j + 1] = true;
      }
    }
  }
  
  for(int i = 0; i < N; i++){
    for(int j = 1; j <= H; j++){
      for(int k = 1; k <= M; k++){
        if(K[j][k]){
          score[j][k] = 2147483640;
        }else{
          score[j][k] = -2147483640;
        }
      }
    }
    saiki(C[i + 1][0], C[i + 1][1], 0);
    int aa = score[C[i][0]][C[i][1]];
    printf("%d\n", aa);
    ans += aa;
  }
  
  printf("%d\n", ans);
  
  return 0;
}

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

using namespace std;
using ll = long long;
const ll INF = 1e9;

int H, W, N;
vector<vector<int>> F(10);
string M[1001];

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

int bfs(int sx, int sy, int gx, int gy) {
    int d[1001][1001];
    queue<vector<int>> que;
    for(int i = 0; i < H; i++) for (int j = 0; j < W; j++) d[i][j] = INF;

    que.push({sx, sy});
    d[sy][sx] = 0;

    while(que.size()) {
        vector<int> p = que.front(); que.pop();
        if (p[0] == gx && p[1] == gy) break;

        for (int i = 0; i < 4; i++) {
            int nx = p[0] + dx[i], ny = p[1] + dy[i];
            if (M[ny][nx] != 'X' && nx >= 0 && nx < W && ny >= 0 && ny < H && d[ny][nx] == INF) {
                que.push({nx, ny});
                d[ny][nx] = d[p[1]][p[0]] + 1;
            }
        }
    }

    return d[gy][gx];
}

int main() {
    ll ans = 0;
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        cin >> M[i];
        for (int k = 0; k < W; k++) {
            if (M[i][k] == 'S') {
                F[0] = {k, i};
            }
            if (isdigit(M[i][k])) {
                F[M[i][k] - '0'] = {k, i};
            }
        }
    }

    for (int i = 0; i < N; i++) {
        ans += bfs(F[i][0], F[i][1], F[i+1][0], F[i+1][1]);
    }

    cout << ans << endl;

    return 0;
}

C++: #include <cstdio>
#include <queue>
#include <cctype>
#include <vector>

using namespace std;

struct Position {
    int h, w, time, strength;
    Position (int h, int w, int t, int s): h(h), w(w), time(t), strength(s) {}
};

const int max_h = 1005;
const int max_w = 1005;
int g_h, g_w, g_strength, g_max_strength, si, sj;
char mat[max_h][max_w];
pair<int, int> factories[20];
int dh[] = {0, 1, 0, -1};
int dw[] = {1, 0, -1, 0};

int main() {
    scanf("%d%d%d", &g_h, &g_w, &g_max_strength);
    for (int i = 0; i < g_h; i++) {
        getchar();
        for (int j = 0; j < g_w; j++) {
            mat[i][j] = getchar();
            if (mat[i][j] == 'S') {
                si = i;
                sj = j;
                mat[i][j] = '.';
            } else if (isdigit(mat[i][j])) {
                factories[mat[i][j]-'0'].first = i;
                factories[mat[i][j]-'0'].second = j;
                mat[i][j] = '.';
            }
        }
    }
    queue<Position> q;
    g_strength = 1;
    q.push(Position(si, sj, 0, 1));
    int result = -1;
    while (!q.empty()) {
        Position p = q.front();
        q.pop();
        if (p.strength < g_strength) continue;
        pair<int, int> f = factories[p.strength];
        if (p.h == f.first && p.w == f.second) {
            if (p.strength == g_max_strength) {
                result = p.time;
                break;
            }
            p.strength++;
            g_strength++;
        }
        mat[p.h][p.w] = p.strength+'0';
        for (int i = 0; i < 4; i++) {
            int w = p.w + dw[i];
            int h = p.h + dh[i];
            if (w >= 0 && w < g_w && h >= 0 && h < g_h &&
                    (mat[h][w] == '.' || (isdigit(mat[h][w]) && p.strength+'0' > mat[h][w]))) {
                q.push(Position(h, w, p.time+1, p.strength));
            }
        }
    }
    printf("%d\n", result);
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;

char ma[1005][1005];int vis[1005][1005];
int w,h,n;

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

struct node
{
    int x,y,time;
};
node start;

void bfs(char ed)
{
    queue<node> que;
    que.push(start);
    vis[start.x][start.y]=1;
    while(!que.empty())
    {
        node no=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int x=no.x+dx[i];
            int y=no.y+dy[i];
            if(vis[x][y]||x>=h||x<0||y>=w||y<0||ma[x][y]=='X')
                continue;
            if(ma[x][y]==ed)
            {
                start.x=x;
                start.y=y;
                start.time=no.time+1;
                return ;
            }
            vis[x][y]=1;
            node no1;
            no1.x=x;no1.y=y;no1.time=no.time+1;
            que.push(no1);
        }
    }
}

int main()
{
    while(~scanf("%d %d %d", & h, & w, & n))
    {
        for(int i=0;i<h;i++)
        {
            scanf("%s",&ma[i]);
            for(int j=0;j<w;j++)
                if(ma[i][j]=='S')
                {
                    start.x=i;
                    start.y=j;
                    start.time=0;
                }

        }
            for(int i=0;i<n;i++)
            {
                memset(vis,0,sizeof(vis));
                bfs(i+'1');
            }

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