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>
#define inf 100000
using namespace std;

typedef pair<int, int> P;
int h, w, n, sgy[10], sgx[10], dy[4]={0,1,0,-1}, dx[4]={1,0,-1,0}, d[1000][1000];
char s[1000][1000];

void bfs(void)
{
	int ans=0;
	for (int i = 0; i < n; ++i)
	{
		queue<P> que;
		for (int j = 0; j < h; ++j) for (int k = 0; k < w; ++k) d[j][k] = inf;
		que.push(P(sgy[i],sgx[i]));
		d[sgy[i]][sgx[i]]=0;
		while(que.size())
		{
			P p=que.front();
			if(p.first==sgy[i+1]&&p.second==sgx[i+1]) break;
			que.pop();
			for (int j = 0; j < 4; ++j)
			{
				int ny = p.first+dy[j], nx = p.second+dx[j];
				if(ny>=0&&ny<h&&nx>=0&&nx<w&&s[ny][nx]!='X'&&d[ny][nx]==inf)
				{
					que.push(P(ny, nx));
					d[ny][nx]=d[p.first][p.second]+1;
				}
			}
		}
		ans += d[sgy[i+1]][sgx[i+1]];
	}
	cout << ans << endl;
}

int main(int argc, char const *argv[])
{
	cin >> h >> w >> n;
	for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j){
		cin >> s[i][j];
		if(s[i][j]=='S') sgy[0]=i, sgx[0]=j;
		for (int k = 0; k < n; ++k)
		{
			if(s[i][j]=='1'+k) sgy[k+1]=i, sgx[k+1]=j;
		}
	}
	bfs();
	return 0;
}

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

using namespace std;

const int INF = 10000000;
const int MAX_N = 1004;
const int MAX_M = 1004;
char map[MAX_N][MAX_M];
int n, m, k, dis[MAX_N][MAX_M];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

struct state {
    int x, y;
} p[12];

int bfs(int sx, int sy, int gx, int gy) {
    for (int i = 0; i<n; i++)
        for (int j = 0; j<m; j++)
            dis[i][j] = INF;
    queue<state>que;
    state start;
    start.x = sx, start.y = sy;
    dis[start.x][start.y] = 0;
    que.push(start);
    while (que.size()) {
        state cur = que.front();
        que.pop();
        if (cur.x == gx && cur.y == gy)return dis[cur.x][cur.y];
        for (int i = 0; i < 4; i++) {
            int nx = cur.x + dx[i], ny = cur.y + dy[i];
            if (dis[nx][ny] == INF && map[nx][ny] != 'X') {
                dis[nx][ny] = dis[cur.x][cur.y] + 1;
                state news;
                news.x = nx, news.y = ny;
                que.push(news);
            }
        }
    }
}

void solve() {
    int ans = 0;
    for (int i = 0; i <= k - 1;i++) {
        ans += bfs(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y);
    }
    printf("%d\n", ans);
}

int main() {
    
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0;i<n; i++)
        scanf("%s",map[i]);
    for (int i = 0; i <n; i++)
        for (int j = 0; j < m; j++) {
            if (map[i][j] == 'S')
                p[0].x = i, p[0].y = j;
            if (map[i][j] != 'X' && map[i][j] != 'S') {
                int id = map[i][j] - '0';
                p[id].x = i;
                p[id].y = j;
            }
        }
    solve();
    return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main()
{
	int h,w,n,count=0,a=0;
	char s[100][100];
	cin >> h >> w >> n;
	//pair<int,int> p;
	//queue<pair<int,int>> q;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin >> s[i][j];
		}
	}
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(s[i][j]-a>1){
				//q.push(make_pair(i,j));
				//count++;
			}
			else if(s[i][j]=='.'){
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

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

using namespace std;

typedef pair<int,int> P;
queue<P> q;
int sx,sy;
int gx,gy;
int g;
char field[1000][1000];
int d[1000][1000];
int H,W,N;
int hp=1;
int ans=0;

int bfs(){
	memset(d,-1,sizeof(d));
	P p;
	q.push(P(sx,sy));
	d[sx][sy]=0;
	while(q.size()){
		if(q.front().first==gx&&q.front().second==gy) break;
		p=q.front();
		q.pop();
		for(int i=-1;i<=1;i++){
			for(int j=-1;j<=1;j++){
				if((i==0||j==0)&&(i!=0||j!=0)){
					if(p.first+i>=0&&p.first+i<=H-1&&p.second+j>=0&&p.second+j<=W-1&&d[p.first+i][p.second+j]<0){
						if(field[p.first+i][p.second+j]!='X'){
							q.push(P(p.first+i,p.second+j));
							d[p.first+i][p.second+j]=d[p.first][p.second]+1;
						}
					}
				}
			}
		}
		
	}
	return d[gx][gy];
}
int main(){
	cin>>H>>W>>N;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin>>field[i][j];
		}
	}
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(field[i][j]=='S')
			{
				sx=i;
				sy=j;
			}
			if(field[i][j]==49)
			{
				gx=i;
				gy=j;
			}
		}
	}
	ans+=bfs();
	for(int i=0;i<N-1;i++){
		for(int j=0;j<H;j++){
			for(int k=0;k<W;k++){
				if(field[j][k]==49+i)
				{
					sx=j;
					sy=k;
				}
				if(field[j][k]==50+i)
				{
					gx=j;
					gy=k;
				}
			}
		}
		while(q.size()){
			q.pop();
		}
		memset(d,-1,sizeof(d));
		ans+=bfs();
	}

	cout<<ans<<endl;
}

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

using namespace std;

typedef pair<int, int> p;

int w, h, n, result, steps, ripple[1000][1000];
char map[1000][1000];
bool visited[1000][1000];
p start, last_start, goal, cur;
queue<p> myqueue;

void init(int in){
    start = last_start;

    while(!myqueue.empty()) myqueue.pop();

    for(int i=0; i<w; ++i){
        for(int j=0; j<h; ++j){
            ripple[i][j] = -1;
            if(map[i][j] == 'X'){
                visited[i][j] = true;
            }else if(map[i][j] == '0'+in){
                goal = make_pair(i, j);
                visited[i][j] = false;
            }else{
                visited[i][j] = false;
            }
        }
    }
    ripple[start.first][start.second] = 0;
    return ;
}

int main(){
    cin >> w >> h >> n;

    for(int i=0; i<w; ++i){
        for(int j=0; j<h; ++j){
            cin >> map[i][j];
            if(map[i][j] == 'S')
                last_start = make_pair(i, j);
        }
    }

    result = 0;
    for(int i=1; i<=n; ++i){
        init(i);
        myqueue.push(start);

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

            if(cur == goal){
                break;
            }

            visited[cur.first][cur.second] = true;

            int x = cur.first, y = cur.second;
            if(x-1 >= 0 && !visited[x-1][y]){
                myqueue.push(make_pair(x-1, y));
                ripple[x-1][y] = ripple[cur.first][cur.second]+1;
            }
            if(y-1 >= 0 && !visited[x][y-1]){
                myqueue.push(make_pair(x, y-1));
                ripple[x][y-1] = ripple[cur.first][cur.second]+1;
            }
            if(x+1 < w && !visited[x+1][y]){
                myqueue.push(make_pair(x+1, y));
                ripple[x+1][y] = ripple[cur.first][cur.second]+1;
            }
            if(y+1 < h && !visited[x][y+1]){
                myqueue.push(make_pair(x, y+1));
                ripple[x][y+1] = ripple[cur.first][cur.second]+1;
            }
        }
        result += ripple[goal.first][goal.second];
        last_start = goal;
    }
    cout << result << endl;
    return 0;
}