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

using namespace std;

typedef pair<int, int> pii;

// ?????°
constexpr int MAX_SCALE = 1010;
constexpr int INF = 1000;

// input
int height, width, max_search_num;
int sx, sy;
int gx, gy;
char maze[MAX_SCALE][MAX_SCALE];

// ???????????§??????????????¢?????????
int d[MAX_SCALE][MAX_SCALE];
queue<pii> relay_point;

int ans = 0;

int bfs(pii relay_pos, int num)
{
    // init
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            d[y][x] = INF;
            if (maze[y][x] == num + '0') {
                gx = x; gy = y;
            }
        }
    }

    // ??????????¨??????????
    queue<pii> que;
    que.push(pii(sx, sy));
    int dx[4] = {1, 0, 0, -1};
    int dy[4] = {0, -1, 1, 0};

    // start??°???
    d[relay_pos.second][relay_pos.first] = 0;

    // 1~9?????§?¨?
    pii q = relay_point.front(); relay_point.pop();

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

        if (p.first == q.first && p.second == q.second) {
            break;
        }

        for (int i = 0; i < 4; ++i) {
            int nx = p.first + dx[i], ny = p.second + dy[i];

            if (nx >= 0 && nx < width && ny >= 0 && ny < height && maze[nx][ny] != 'X' && d[nx][ny] == INF) {
                que.push(pii(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }

    ans += d[gy][gx];

    if (num != max_search_num) bfs(pii(gx, gy), num + 1);

    return ans;
}

void solve()
{
    int res = bfs(pii(sx, sy), 1);
    cout << res << endl;
}

int main()
{
    cin >> height >> width >> max_search_num;

    static int point = 1;

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            cin >> maze[y][x];
            if (maze[y][x] == 'S') {
                sx = x; sy = y;
            } else if (maze[y][x] == point + '0') {
                relay_point.push(pii(x, y));
                point++;
            }
        }
    }
    
    solve();
    
    return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<queue>
#include<algorithm>
#define INF 1000000
using namespace std;
typedef pair<int,int> P;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
bool map[1000][1000];
int x[10],y[10];
int ans=0;
int h,w;
int search(int n){
    int d[1000][1000];
    fill(d[0],d[0]+1000*1000,INF);
    int sx=x[n],sy=y[n];
    int tx=x[n+1],ty=y[n+1];
    queue<P> q;
    q.push(P(sx,sy));
    d[sx][sy]=0;
    while(!q.empty()){
        P p=q.front();
        q.pop();
        sx=p.first,sy=p.second;
        if(sx==tx&&sy==ty) return d[tx][ty];
        for(int i=0;i<4;i++){
            int X=sx+dx[i];
            int Y=sy+dy[i];
            if(map[X][Y]&&X>=0&&X<h&&Y>=0&&Y<w&&d[X][Y]==INF){
                q.push(P(X,Y));
                d[X][Y]=d[sx][sy]+1;
            }
        }
    }
    return d[tx][ty];
}
int main(){
    int n;
    scanf("%d %d %d",&h,&w,&n);
    fill(map[0],map[0]+1000*1000,false);
    getchar();
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            char c=getchar();
            if(c!='X'){
                map[i][j]=true;
                if(c=='S'){
                    x[0]=i,y[0]=j;
                }else{
                    int k=(int)c-48;
                    x[k]=i;
                    y[k]=j;
                }
            }
        }
        getchar();
    }
    for(int i=0;i<n;i++){
        int tmp=search(i);
        ans+=tmp;
    }
    printf("%d\n",ans);
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
/*(^(^(^(^(^o^)^)^)^)^) &#9602;&#9605;&#9607;&#9608;&#9619;&#9618;&#9617;('ω')&#9617;&#9618;&#9619;&#9608;&#9607;&#9605;&#9602; 初のBFS*/
typedef pair<int,int> p;
char x[1001][1001];
int c[10]={},d[10],y[1001][1001]={},a,b;
int h,w,n,i,j,now=1;
int bfs(){
	int dx[]={1,0,-1,0};
	int dy[]={0,1,0,-1};
	queue<p>orz;
	for(i=1;i<=h;i++){
	for(j=1;j<=w;j++){
		y[i][j]=10000000;
		if(x[i][j]=='S'){
			orz.push(p(i,j));
			printf("%d %d\n",i,j);
			y[i][j]=0;
		}
		if(x[i][j]-'0'==n){
			printf("%c\n",x[i][j]);
			a=i;
			b=j;
		}}}
	while(orz.size()){
		p w=orz.front();
		orz.pop();
		if(w.first==a&&w.second==b){
			break;
		}
		for(i=0;i<4;i++){
			int ax=w.first+dx[i];
			int ay=w.second+dy[i];
			if(ax>=1&&ax<=a&&ay>=1&&ay<=b&&y[ax][ay]!=10000000&&x[ax][ay]!='X'){
				if(c[now-1]==now-1){
				orz.push(p(ax,ay));
				y[ax][ay]=y[w.first][w.second]+1;
			}
				if(1<=x[ax][ay]-'0' && x[ax][ay]-'0'<=9){
					now++;
					c[now-1]=now-1;
				}
		}
	}
	}
	return y[a][b];
}
int main(){
scanf("%d %d %d",&h,&w,&n);
for(i=1;i<=h;i++){
scanf("%s",x[i]);
}
for(i=1;i<=h;i++){
for(j=w-1;j>=0;j++){
	x[i][j+1]=x[i][j];
}}
int ans=bfs();
printf("%d\n",ans);
return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
	int sum = 0, ans = 0;
	string s;
	vector<int> num;
	vector<char> joi;
	
	cin >> s;
	
	for(int i = 0; i < s.size(); i++){
		sum++;
		if(s[i] != s[i + 1]){
			num.push_back(sum);
			joi.push_back(s[i]);
			sum = 0;
		}
	}
	
	for(int i = 1; i < num.size(); i++){
		if(joi[i] == 'O' && joi[i - 1] == 'J' && joi[i + 1] == 'I'){
			if(num[i - 1] >= num[i] && num[i + 1] >= num[i]){
				ans = max(ans, num[i]);
			}
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long int ll;

int main()
{
	int h,w,n;
	string ss[100];
	int x[100][100];
	int X,Y;
	int ans=0;

	scanf("%d%d%d",&h,&w,&n);
	for(int i=0;i<h;i++)cin >> ss[i];

	for(int i=1;i<=n;i++){
		for(int j=0;j<h;j++){
			for(int k=0;k<w;k++){
				x[j][k]=10001;
			}
		}
		for(int j=0;j<h;j++){
			for(int k=0;k<w;k++){
				if(i==1){
					if(ss[j][k]=='S')x[j][k]=0;
				}
				else{
					if(ss[j][k]==(i-1)+'0')x[j][k]=0;
				}
				if(ss[j][k]==i+'0'){
					X=j,Y=k;
				}
			}
		}

		int z=0;
		while(x[X][Y]==10001){
			for(int j=0;j<h;j++){
				for(int k=0;k<w;k++){
					if(x[j][k]==z){
						if(j>=1&&ss[j-1][k]!='X')x[j-1][k]=min(x[j-1][k],z+1);
						if(k>=1&&ss[j][k-1]!='X')x[j][k-1]=min(x[j][k-1],z+1);
						if(j<h-1&&ss[j+1][k]!='X')x[j+1][k]=min(x[j+1][k],z+1);
						if(k<w-1&&ss[j][k+1]!='X')x[j][k+1]=min(x[j][k+1],z+1);
					}
				}
			}
			z++;
		}
		ans+=x[X][Y];
		/*for(int j=0;j<h;j++){
			for(int k=0;k<w;k++){
				printf("%d ",x[j][k]);
			}
			printf("\n");
		}
		printf("%d\n",x[X][Y]);*/
	}

	printf("%d\n",ans);
	return 0;
}