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 <stdio.h>
#include <queue>
#include <algorithm>
#define INF 200000000
using namespace std;
typedef pair<int, int > P;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int h, w, n, ans, dis[10000][1000];
char s[10000][10000];
int bfs(int sx, int sy, int ex, int ey)
{
	for(int i = 1; i <= h; i++)
		for(int j = 1; j <= w; j++)dis[i][j] = INF;
	queue<P> q;
	q.push(P(sx, sy));
	dis[sx][sy] = 0;
	while (q.size()){
		P p = q.front();q.pop();
		if(p.first == ex&&p.second == ey)break;
		for(int i = 0; i < 4; i++){
			int xx = p.first + dx[i], yy = p.second + dy[i];
			if(xx >= 1&&xx <= h&&yy >= 1&& yy <= w&&dis[xx][yy] == INF&&s[xx][yy]!='X'){
				dis[xx][yy] = dis[p.first][p.second] + 1;
				q.push(P(xx, yy));
			} 
		}
	}
	return ans+=dis[ex][ey];
}
int main(){
	//freopen("1.in" ,"r", stdin);
	int sx, sy, ex, ey;
	scanf("%d %d %d", &h, &w, &n);
	for(int i = 1; i <= h; i++)
		scanf("%s", s[i] + 1);
	for (int i = 1; i <= h; i++)
		for(int j = 1; j <= w; j++)
			if(s[i][j] == 'S')sx = i, sy = j;
	for (int k = 1; k <= n; k++)
		for(int i = 1; i <= h; i++)
			for(int j = 1; j <= w; j++){
				if(s[i][j] == '1' + k - 1){
					ex = i, ey = j;
					bfs(sx, sy, ex, ey);
					sx = ex;sy = ey;
				}
			}
	printf("%d\n", ans);

}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
#define MAX 1005

char JOI[MAX][MAX];
int h,w,n;
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};

pair<int, int> goal[10];
bool check(int a,int b){
    return (a>=1&&a<=h&&b>=1&&b<=w);
}

int step[MAX][MAX];
int bfs(pair<int, int> st,pair<int, int> en){
    queue<pair<int,int> > q;
    q.push(st);
    memset(step, -1, sizeof(step));
    step[st.first][st.second]=0;
    int stp=0;
    while(!q.empty()){
        pair<int, int> S=q.front(); q.pop();
        int x=S.first;  int y=S.second;
        stp=step[x][y];
        for(int i=0; i<4; i++){
            int cx=x+dx[i]; int cy=y+dy[i];
            if(check(cx, cy)){
                if(JOI[cx][cy]!='X'){
                    if(cx==en.first&&cy==en.second) return stp+1;
                    q.push(make_pair(cx, cy));
                    step[cx][cy]=stp+1;
                }
            }
        }
    }
}
int main(){
    cin>>h>>w>>n;
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            cin>>JOI[i][j];
            if(JOI[i][j]=='S') goal[0]=make_pair(i, j);
            else if(JOI[i][j]>='0'&&JOI[i][j]<='9'){
                int x=JOI[i][j]-'0';
                goal[x]=make_pair(i, j);
            }
        }
    }
    int ans=0;
    for(int i=0; i<n; i++){
        ans+=bfs(goal[i],goal[i+1]);
    }
    cout<<ans<<endl;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long  cheese[1001][1001];
long check[1001][1001];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){//
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
			
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(-1==cheese[y.a][y.b]){
			continue;
		}
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&check[y.a+q[i]][y.b+p[i]]!=1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
char field[1000][1000];
struct date{
    int x,y,cnt;
};
int main(){
	int h,w,n;
	cin >> h >> w >> n;
	date start;
	int ans = 0;
	for(int i = 0; i < h; i++){
	    cin >> field[i];
	    for(int j = 0; j < w; j++){
            if(field[i][j] == 'S'){
                start.x = j,start.y = i,start.cnt = 0;
            }
	    }
	}
	for(int i = 1; i <= n; i++){
	    queue<date> Q;
        Q.push(start);
        bool flg[1000][1000] = {{0}};
        while(!Q.empty()){
            date d = Q.front();
            Q.pop();
            if(field[d.y][d.x] == i + '0'){
                start.x = d.x,start.y = d.y,start.cnt = d.cnt;
                break;
            }
            if(flg[d.y][d.x])continue;
            flg[d.y][d.x] = 1;
            int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
            for(int j = 0; j < 4; j++){
                int px = d.x+dx[j],py = d.y + dy[j];
                if(px < 0 || py < 0 || px >= w || py >= h)continue;
                if(field[py][px] == 'X')continue;
                date next = {px,py,d.cnt+1};
                Q.push(next);
            }
        }
        ans = start.cnt;
	}

	cout << ans << endl;
	return 0;
}