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 <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
using namespace std;

int H,W,N;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char MAP[1000+1][1000+1];
int f[1000+1][1000+1];
int d[1000+1][1000+1];
int main(){
	cin >> H >> W >> N;
	queue<char> qux;
	queue<char> quy;
	vector<int> ans(N+1);
	for(int i=1; i<H+1; ++i){
		for(int j=1; j<W+1; ++j){
			cin >> MAP[i][j];
			f[i][j]=0;
			if(MAP[i][j]=='S'){
				qux.push(i);
				quy.push(j);
				f[i][j]=1;
				d[i][j]=0;
			}
		}
	}
	int x;
	int y;
	for(int i=1; i<N+1; ++i){
		while(1/*qux.size()&&quy.size()*/){
			x=qux.front();
			y=quy.front();
			//cout << x << " " << y << " " << d[x][y] << "\n";
			qux.pop();
			quy.pop();
			//if('0'<=MAP[x][y]&&MAP[x][y]<='9'){
				//if(((int)MAP[x][y]-'0')==i) {
			if(atoi(&MAP[x][y])==i){
					ans[i]=d[x][y];
					for(int j=1; j<H+1; ++j){
						for(int k=1; k<W+1; ++k){
							f[i][j]=0;
						}
					}
					d[x][y]=0;
					break;
				}
			//}
			for(int j=0; j<4; ++j){
				int nx=x+dx[j];
				int ny=y+dy[j];
				if(1<=nx&&nx<=H&&1<=ny&&ny<=W&&MAP[nx][ny]!='X'&&f[nx][ny]==0){
					qux.push(nx);
					quy.push(ny);
					f[nx][ny]=1;
					d[nx][ny]=d[x][y]+1;
				}
			}
		}
	}
	int ANS=0;
	for(int i=1; i<N+1; ++i){
		ANS+=ans[i];
	}
	cout << ANS << "\n";
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>

typedef std::pair<int,int> Point;

int x,y;
std::vector<std::vector<char> >v;
std::vector<std::vector<int> >count;

int bfs(int sx,int sy,int gx,int gy){
	for(int i=0;i<count.size();i++){
		for(int j=0;j<count[i].size();j++){
			count[i][j]=-1;
		}
	}
	std::queue<Point>que;
	que.push(Point(sx,sy));
	count[sy][sx]=0;

	while(que.size()){
		Point po=que.front();
		que.pop();
		if(po.first==gx&&po.second==gy)break;

		int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
		for(int i=0;i<4;i++){
			int nx=po.first+dx[i],ny=po.second+dy[i];
			if(nx>=0&&nx<x&&ny>=0&&ny<y&&v[ny][nx]!='X'&&count[ny][nx]==-1){
				que.push(Point(nx,ny));
				count[ny][nx]=count[po.second][po.first]+1;
			}
		}
	}
	return count[gy][gx];
}

int main(){
	int n,res=0;
	std::cin>>y>>x>>n;
	std::vector<Point>factory(n+1);
	for(int i=0;i<y;i++){
		v.push_back(std::vector<char>());
		count.push_back(std::vector<int>());
		for(int j=0;j<x;j++){
			char a;
			std::cin>>a;
			if(a=='S')factory[0]=Point(j,i);
			if(a!='S'&&a!='X'&&a!='.')factory[a-'0']=Point(j,i);
			v[i].push_back(a);
			count[i].push_back(-1);
		}
	}
	for(int i=0;i<n;i++)res+=bfs(factory[i].first,factory[i].second,factory[i+1].first,factory[i+1].second);
	std::cout<<res<<std::endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
#define INF 9999
typedef pair<int,int> P;
int w;//???
int h;//???
int num;//??\????????° 
P pos;//???????????? 
char fty[1005][1005]; 
P f_pos[10];
int step;
int mp[1005][1005];
void bfs(int x,int y){
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			mp[i][j]=INF;
		}
	}
	mp[pos.first][pos.second]=0;
	
	int dx[4]={1,0,-1,0};
	int dy[4]={0,1,0,-1};
	queue<P> que;
	que.push(pos);
	while(que.size()){
		P s = que.front();
		que.pop();
		if(s.first==x&&s.second==y) break;
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				int nx = s.first+dx[i];
				int ny = s.second+dy[i];
				if(nx>=0&&nx<h&&ny>=0&&ny<w&&mp[nx][ny]==INF&&fty[nx][ny]!='X'){
					mp[nx][ny]=mp[s.first][s.second]+1;
					que.push(P(nx,ny));
				}
			}
		}
	}
	step+=mp[x][y]; 
	pos.first=x;
	pos.second=y;
}
int main(){
	cin>>h>>w>>num;
	step=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>fty[i][j];
			for(int k=1;k<=num;k++){
				if(fty[i][j]-48==k){
					f_pos[k].first=i;
					f_pos[k].second=j;
					break;
				}
				
			}
			if(fty[i][j]=='S'){
				pos.first=i;
				pos.second=j;
			}
		}
	}
	step=0;
	for(int k=1;k<=num;k++){
		bfs(f_pos[k].first,f_pos[k].second);
	}
	cout<<step<<endl;
}

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

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

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

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

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

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

    while(que.size()) {
        P p = que.front(); que.pop();
        if (p.first == gx && p.second == gy) break;

        for (int i = 0; i < 4; i++) {
            int nx = p.first + dx[i], ny = p.second + 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.second][p.first] + 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] = P(k, i);
            }
            if (isdigit(M[i][k])) {
                F[M[i][k] - '0'] = P(k, i);
            }
        }
    }

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

    cout << ans << endl;

    return 0;
}