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<bits/stdc++.h>
using namespace std;
struct ma
{
	int x;
	int y;
};
int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 };
char m[1200][1200];
int map[1200][1200];
int main()
{
	int ans = 0;
	int tate, yoko;
	int n;
	cin >> tate >> yoko >> n;
	
	ma sta;
	for (int i = 0; i < tate; i++)
	{
		for (int k = 0; k < yoko; k++)
		{
			cin >> m[i][k];
			if (m[i][k] == 'S')
			{
				sta.x = k;
				sta.y = i;
			}
		}
	}
	int HP = 1;
	queue<ma> hoge;
	for (int i = 0; i < n; i++)
	{
		hoge.push(sta);
		
		memset(map, -1, sizeof(map));
		map[sta.y][sta.x] = 0;
		while (!hoge.empty())
		{
			ma now;
			now = hoge.front();
			hoge.pop();
			for (int k = 0; k < 4; k++)
			{
				if (now.y + dy[k] >= tate || now.x + dx[k] >= yoko)continue;
				if (now.y + dy[k] < 0 || now.x + dx[k] < 0)continue;
				if (m[now.y + dy[k]][now.x + dx[k]] != 'X')
				{
					if (map[now.y + dy[k]][now.x + dx[k]] != -1)
					{
						continue;
					}
					map[now.y + dy[k]][now.x + dx[k]] = map[now.y][now.x] + 1;
					if (m[now.y + dy[k]][now.x + dx[k]] - '0' == HP)
					{
						m[now.y + dy[k]][now.x + dx[k]] = '.';
						ans += map[now.y + dy[k]][now.x + dx[k]];
						while (!hoge.empty())hoge.pop();
						HP++;
						sta.x = now.x + dx[k];
						sta.y = now.y + dy[k];
						break;
					}
					else
					{
						ma hage;
						hage.x = now.x + dx[k];
						hage.y = now.y + dy[k];
						hoge.push(hage);
					}
				}
			}

		/*	for (int i = 0; i < tate; i++)
			{
				for (int k = 0; k < yoko; k++)
				{
					cout << map[i][k] << " ";
				}
				cout << endl;
			}
			cout << endl;*/
		}
	}
	cout << ans << endl;

}

C++: #include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int H,W,N;
const int MAX_H=100;
const int MAX_W=100;
string S[MAX_H];
int ans=0;
int X[10],Y[10];
int D[MAX_W][MAX_H];
int cou;
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
int xs,ys,xg,yg;
int E[MAX_W][MAX_H];
int solve(){
	cou=0;
	while(D[xg][yg]==-1){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				E[j][i]=D[j][i];
			}
		}
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(D[j][i]==cou){
					for(int k=0;k<4;k++){
						if(j+dx[k]>=0&&j+dx[k]<W&&i+dy[k]>=0&&i+dy[k]<H){
							if(D[j+dx[k]][i+dy[k]]==-1&&S[i+dy[k]][j+dx[k]]!='X'){
								E[j+dx[k]][i+dy[k]]=cou+1;
							}
						}
					}
				}
			}
		}
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)D[j][i]=E[j][i];
		cou++;
	    }
	return D[xg][yg];
}

int main()
{
	scanf("%d%d%d",&H,&W,&N);
	for(int i=0;i<H;i++)cin >> S[i];
	for(int i=0;i<W;i++){
		for(int j=0;j<H;j++){
			if(S[j][i]!='X'&&S[j][i]!='.'){
				if(S[j][i]=='S'){
					X[0]=i;Y[0]=j;
				}
				else{
					X[S[j][i]-'0']=i;
					Y[S[j][i]-'0']=j;
				}
			}
		}
	}
	for(int ti=0;ti<N;ti++){
		for(int i=0;i<W;i++){
		    for(int j=0;j<H;j++){
			    D[i][j]=-1;
		    }
	    }
		xs=X[ti];
		ys=Y[ti];
		xg=X[ti+1];
		yg=Y[ti+1];
		D[xs][ys]=0;
		ans+=solve();
	}
	printf("%d\n",ans);
	return 0;
}

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

using namespace std;

struct State {
    int step;
    int x;
    int y;

    State() {
        step = 0;
        x = 0;
        y = 0;
    }

    State(int step, int x, int y): step(step), x(x), y(y) {}

    ~State(){}
};

const int MAX_N = 1000;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, { -1, 0}};
char board[MAX_N][MAX_N];
int W, H, N;
State pos[10];

void init();
void input();
void solve();
int bfs(State from, State to);
void findPossition();

int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    init();
    input();
    solve();
    return 0;
}

void init() {
}

void input() {
    scanf("%d %d %d", &H, &W, &N);

    for(int i = 0; i < H; i++) {
        scanf("%s", board[i]);
    }
}


void solve() {
    int moves = 0;
    findPossition();

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

    printf("%d\n", moves);
}

int bfs(State from, State to) {
    queue<State> que;
    bool visit[H][W];
    State cur = from;

    for(int i = 0; i < H; i++) {
        memset(visit[i], false, sizeof(visit[i]));
    }

    que.push(cur);
    visit[cur.x][cur.y] = true;

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

        for(int i = 0; i < 4; i++) {
            int x = cur.x + dir[i][0];
            int y = cur.y + dir[i][1];

            if(x < 0 || x >= H || y < 0 || y >= W || visit[x][y] || 'X' == board[x][y]) {
                continue;
            }

            que.push(State(cur.step + 1, x, y));
            visit[cur.x][cur.y] = true;

            if(x == to.x && y == to.y) {
                return cur.step + 1;
            }
        }
    }

    return 0;
}

void findPossition() {
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            int c = board[i][j];

            if('S' == c) {
                pos[0] = State(0, i, j);
            }

            if(isdigit(c)) {
                pos[c - '0'] = State(0, i, j);
            }
        }
    }
}

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

using namespace std;

typedef struct
{
	int x, y;
}P;

int w, h, n, dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
char s[1002][1002];
int d[1002][1002];
P a[10], p, q;

int bfs(P st, P ed)
{
	int i, j;
	for (i = 0; i < h; i++)
		for (j = 0; j < w; j++)
			d[i][j] = -1;
	queue<P>que;
	que.push(st);
	d[st.x][st.y] = 0;	
	while (!que.empty())
	{
		p = que.front();
		que.pop();
		if (p.x == ed.x && p.y == ed.y)
			break;	
		for (i = 0; i < 4; i++)
		{
			q.x = dir[i][0] + p.x;
			q.y = dir[i][1] + p.y;
			if (q.x >= 0 && q.x < h && q.y >= 0 && q.y < w && s[q.x][q.y] != 'X' && d[q.x][q.y] == -1)
			{				
				que.push(q);
				d[q.x][q.y] = d[p.x][p.y] + 1;
			}
		}
	}
	return d[p.x][p.y];
}

int main()
{
	int i, j, sum;
	scanf("%d%d%d", &h, &w, &n);
	for (i = 0; i < h; i++)
	{
		scanf("%s", s[i]);
		for (j = 0; j < w; j++)
			if (s[i][j] == 'S')
			{
				a[0].x = i;
				a[0].y = j;
			}
			else if (s[i][j] != '.')
			{
				sum = s[i][j] - '0';
				a[sum].x = i;
				a[sum].y = j;
			}
	}
	for (i = sum = 0; i < n; i++)
		sum += bfs(a[i], a[i + 1]);
	printf("%d\n", sum);
	return 0;
}