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 <cctype>

constexpr int MAX=1000,INF=(int)1e9;

using P=std::pair<int,int>;

int h,w,n;

P poses[9],start;
bool board[MAX][MAX];

int d[MAX][MAX];

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

int bfs(P s,P g){
	for(int i=0;i<h;++i){
		for(int j=0;j<w;++j)
			d[i][j]=INF;
	}
	std::queue<P> que;
	d[s.second][s.first]=0;
	que.emplace(s);

	while(!que.empty()){
		P p=que.front();
		que.pop();
		if(p==g)
			return d[g.second][g.first];
		for(int i=0;i<4;++i){
			int nx=dx[i]+p.first;
			int ny=dy[i]+p.second;
			if(0<=nx&&nx<w&&
				0<=ny&&ny<h&&!board[ny][nx]&&d[ny][nx]==INF){
				d[ny][nx]=d[p.second][p.first]+1;
				que.emplace(nx,ny);
			}
		}
	}
	return -1;
}

int solve() {
	int ans=bfs(start,poses[0]);
	for(int i=1;i<n;++i)
		ans+=bfs(poses[i-1],poses[i]);
	return ans;
}

int main() {
	std::cin>>h>>w>>n;
	char ch;
	for(int i=0;i<h;++i){
		for(int j=0;j<w;++j){
			std::cin>>ch;
			if(ch=='X')
				board[i][j]=true;
			else if(ch=='S')
				start=P(j,i);
			else if(std::isdigit(ch))
				poses[atoi(&ch)-1]=P(j,i);
		}
	}
	std::cout<<solve()<<std::endl;
	return 0;
}

C++: #include<iostream>
#define INF 100000000
#include<queue>
#include<map>
using namespace std;
typedef pair<int,int> P;
int main(){
	int h,w,n,sx,sy,dx[]={1,-1,0,0},dy[]={0,0,1,-1};
	char s[1000][1000];
	
	cin>>h>>w>>n;
	for(int i=0;i<h;i++)
		cin>>s[i];
	for(int i=0;i<h;i++)
	for(int j=0;j<w;j++)
	if(s[i][j]=='S'){
		sx=j;
		sy=i;
	}
	int nx,ny,ans=0;
	char nex;
	for(int i=1;i<=n;i++){
		nex='0'+i;
		queue<P> que;
		que.push(P(sy,sx));
		map<P,int> mp;
		for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
		mp[P(i,j)]=INF;
		mp[P(sy,sx)]=0;
		while(!que.empty()){
			P p=que.front();
			que.pop();
			if(s[p.first][p.second]==nex){
				ans+=mp[P(p.first,p.second)];
				sy=p.first;
				sx=p.second;
				break;
			}
			for(int i=0;i<4;i++){
				nx=p.second+dx[i];
				ny=p.first+dy[i];
				if(nx>=0&&nx<w&&ny>=0&&ny<h&&s[ny][nx]!='X'&&mp[P(ny,nx)]>mp[P(p.first,p.second)]+1){
					que.push(P(ny,nx));
					mp[P(ny,nx)]=mp[P(p.first,p.second)]+1;
				}
			}
		}
		
	}
	
	cout<<ans<<endl;
	return 0;
}

C++: #include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<time.h>
#include<queue>
using namespace std;
#define INF 100000
char map[101][101];
int h,w,n;
int d1[4]={0,1,0,-1};
int d2[4]={1,0,-1,0};
int i,j,x[10],y[10];
int bfs(int num){
	int gy=y[num+1],gx=x[num+1];
	int d[101][101];
	for(int i=0;i<101;i++){
		for(int j=0;j<101;j++){
			d[i][j]=INF;
		}
	}
	queue<int> qx;queue<int> qy; 
	qx.push(x[num]);qy.push(y[num]);
	d[y[num]][x[num]]=0;
	while(qx.size()+qy.size()!=0){
		int ny,nx;
		ny=qy.front();qy.pop();
		nx=qx.front();qx.pop();
		if(ny==gy&&nx==gx){
			break;
		}
		for(int i=0;i<4;i++){
			int mx=nx+d1[i],my=ny+d2[i];
			if(0<=mx&&mx<w&&0<=my&&my<h&&map[my][mx]!='X'&&d[my][mx]==INF){
				d[my][mx]=d[ny][nx]+1;
				qx.push(mx);
				qy.push(my);
			}
		}
	}
	return d[gy][gx];
}
int main(){
	int ans=0;
	cin>>h>>w>>n;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>map[i][j];
			if(map[i][j]=='S'){
				y[0]=i;x[0]=j;
			}else if('0'<map[i][j]&&map[i][j]<='9'){
				y[(int)(map[i][j]-'0')]=i;
				x[(int)(map[i][j]-'0')]=j;
			}
		}
	}

	for(int i=0;i<=n;i++){
		ans+=bfs(i);
	}
	
	cout<<ans<<endl;
	return 0;
}

C++: // No.3

#include<iostream>
#include<map>
#include<queue>

#define INF 10000
using namespace std;

typedef pair<int ,int> P;

char map_data[1001][1001];
int data[1001][1001];
int W,H;

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int bfs (P s,P g){
	fill((int*) data,(int*)(data +1001),INF);
	data[s.first][s.second]=0;
	queue<P> que;

	que.push(s);
	while(que.size()){
		P p=que.front();que.pop();
		
		for(int i=0;i<4;i++){
			int nx=p.second+dx[i],ny=p.first+dy[i];
			if(0<=nx && nx<W && 0<=ny && ny<H && map_data[ny][nx]!='X' && data[p.first][p.second]+1<data[ny][nx]){
				que.push(P(ny,nx));
				data[ny][nx]=data[p.first][p.second]+1;
				
			}

		}

	}
return data[g.first][g.second];	
}

int main(){
int n;
cin>>W>>H>>n;

P ar[10];

for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	cin>>map_data[i][j];
	if(map_data[i][j]!='.' && map_data[i][j]!='X'){
		if(map_data[i][j]=='S')ar[0]=P(i,j);
		else{
		ar[map_data[i][j]-'0']=P(i,j);
		}
	}
	}
}
int ans=0;
for(int i=0;i<n;i++){
ans+=bfs(ar[i],ar[i+1]);
}

cout<<ans<<endl;
}

C++: #include "bits/stdc++.h"

using namespace std;

#define int long long

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

#define pb push_back
#define VDEL(v, n) v.erase(v.begin() + (n))
#define ALL(v) v.begin(), v.end()

#define INTMAX 2147483647
#define INF 200000000
#define TINT(str) atoi(str.c_str())
#define TSTR(num) to_string(num)

using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VII = vector<vector<int>>;

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

PII goal[10];
char k[1000][1000];
int x0, y0;
int H,W;

int BFS(int x, int y)
{
    int cost[1000][1000];

    REP(i, 1000)
    {
        REP(h, 1000)
        {
            cost[i][h] = INF;
        }
    }

    cost[x0][y0] = 0;

    queue<PII> q;

    q.push(make_pair(x0, y0));

    while(!q.empty())
    {
        PII now = q.front();
        q.pop();
        int nx = now.first;
        int ny = now.second; 

        REP(i, 4)
        {
            if(now.first + dx[i] >= H ||
            now.first + dx[i] < 0) continue;
            if(now.second + dy[i] >= W || 
            now.second + dy[i] < 0) continue;

            if(nx + dx[i] == x &&
            ny + dy[i] == y)
            {
                return cost[nx][ny] + 1;
            }

            if(k[nx + dx[i]][ny + dy[i]] != 'X')
            {
                if(cost[nx + dx[i]][ny + dy[i]] > 
                cost[nx][ny])
                {
                    cost[nx + dx[i]][ny + dy[i]] =
                        cost[nx][ny] + 1;
                    q.push({nx + dx[i], ny + dy[i]});
                }
            }
            else continue;
        }
    }
}

signed main()
{
    int N;
    cin >> H >> W >> N;

    char str[1001];
    REP(h, H)
    {
        scanf("%s", str);
        REP(w, W)
        {
            k[h][w] = str[w];
            if(k[h][w] == 'S')
            {
                x0 = h;
                y0 = w;
            }
            if(k[h][w] != '.' &&
                k[h][w] != 'X')
            {
                goal[k[h][w] - '1'] = make_pair(h, w);
            }
        }
    }
    int ans = 0;

    REP(i, N)
    {
        ans += BFS(goal[i].first, goal[i].second);
        x0 = goal[i].first;
        y0 = goal[i].second;
    }

    cout << ans << endl;

    return 0;
}