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 <cstring>
using namespace std;
typedef struct pair<int,int> P;
const int MAXN=1000+2;
int h,w,n,tot,gx,gy,ab=1;
char fac[MAXN][MAXN];
int fo[MAXN][MAXN];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};

void bfs(){
	memset(fo,0,sizeof(fo));
	queue<P> Q;
	Q.push(P(gx,gy)),fo[gx][gy]=0;
	while(Q.size()){
		P p=Q.front(); Q.pop();
		if(fac[p.first][p.second]-'0'==ab){
			++ab,gx=p.first,gy=p.second,tot+=fo[gx][gy],fac[gx][gy]='.';
			break;
		}
		for(int i=0;i<4;++i){
			int nx=p.first+dx[i],ny=p.second+dy[i];
			if(nx<1 || nx>h || ny<1 || ny>w || fac[nx][ny]=='X' || fo[nx][ny]) continue;
			Q.push(P(nx,ny)),fo[nx][ny]=fo[p.first][p.second]+1;
		}
	}
	if(ab<=n) bfs();
	else return ;
}

int main(){
	cin>>h>>w>>n;
	for(int i=1;i<=h;++i)
		for(int j=1;j<=w;++j){
			cin>>fac[i][j];
			if(fac[i][j]=='S') gx=i,gy=j;
		}
	bfs();
	cout<<tot<<endl;
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string.h>
#include <utility>
#include <queue>

using namespace std;

int dis[1005][1005];
int dire[4][2] = {0,1,0,-1,1,0,-1,0};
int fla,n,h,w,sx,sy,cnt;
int power = 1;
char mmap[1005][1005];
pair<int,int> tem;
queue <pair <int,int> > q;

void bfs(int st,int ed){
    tem.first = st;
    tem.second = ed;
    q.push(tem);
    dis[st][ed] = 0;

    while(!q.empty()){
        tem= q.front();
        q.pop();

        if(power > n){
            break;
        }

        for(int i = 0; i < 4; i++){
            int ii = tem.first + dire[i][0];
            int jj = tem.second  + dire[i][1];

            if(ii >= 1 && ii <= h && jj >= 1 && jj <= w && mmap[ii][jj] != 'X' && dis[ii][jj] == 0x3f3f3f3f){
                if(mmap[ii][jj] - '0' == power){
                    power++;
                    cnt += dis[tem.first][tem.second] + 1;
                    memset(dis,0x3f,sizeof(dis));
                    dis[ii][jj] = 0;
                    while(!q.empty()){
                        q.pop();
                    }
                    q.push(pair<int , int> (ii,jj));
                    break;     
                }else{
                    dis[ii][jj] = dis[tem.first][tem.second] + 1;
                    q.push(pair<int,int>(ii,jj));
                }
            }
        }
    }

}

int main(void){

    scanf("%d %d %d",&h,&w,&n);
    for(int i = 1; i <= h; i++){
        getchar();
        for(int j = 1; j <= w; j++){
            scanf("%c",&mmap[i][j]);
            if(mmap[i][j] == 'S'){
                sx = i;
                sy = j;
            }
        }
    }

    memset(dis,0x3f,sizeof(dis));
    bfs(sx,sy);
    cout << cnt << endl;

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
//#include <map>
#include <iomanip>
#include <vector>
#include <queue>

using namespace std;

const int INF = 1000000000;

typedef pair<int, int> P;	//縦、横

int H, W, N;
char maze[1001][1001];
int sx, sy;
int gx, gy;
int d[1001][1001];		//各点までの最短距離の配列
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int cnt;


int bfs()
{
	queue<P> que;
	
	for (int i=0; i<H; i++)
	{
		for (int j=0; j<W; j++)
		{
			d[i][j] = INF;
		}
	}
	
	que.push(P(sy, sx));
	d[sy][sx] = 0;
	
	while (que.size())
	{
		P p = que.front();
		que.pop();
		if (p.first == gy && p.second == gx) break;
		
		for (int i=0; i<4; i++)
		{
			int nx = p.second + dx[i];
			int ny = p.first + dy[i];
			
			if (0<=nx && nx<W && 0<=ny && ny<H && maze[ny][nx]!='X' && d[ny][nx]==INF)
			{
				que.push(P(ny, nx));
				d[ny][nx] = d[p.first][p.second] + 1;
			}
		}
	}
	
	return d[gy][gx];
}


int main()
{
	//入力
	cin >> H >> W >> N;
	for (int i=0; i<H; i++)
	{
		cin >> maze[i];
	}
	
	//巣と１番目のチーズ工場（ゴール）の設定
	cnt = 0;
	for (int k=1; k<=N; k++)
	{
		for (int i=0; i<H; i++)
		{
			for (int j=0; j<W; j++)
			{
				if (k == 1)
				{
					if (maze[i][j] == 'S')
					{
						sx = j;
						sy = i;
					} else if (maze[i][j] == '1') {
						gx = j;
						gy = i;
					}
				} else {
					if (maze[i][j] == '1' + k - 2)
					{
						sx = j;
						sy = i;
					} else if (maze[i][j] == '1' + k - 1) {
						gx = j;
						gy = i;
					}
				}
			}
		}
		
		cnt += bfs();
	}
	
	cout << cnt << endl;
	
	/*
	cout << "cnt = " << cnt << endl;
	
	//テスト出力
	for (int i=0; i<H; i++)
	{
		cout << maze[i] << endl;
	}
	*/
	
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct P{
	int x,y;//縦,横
};

char f[2000][2000];//フィールド
int H,W,N;
const int INF=100000000;
P chpt[10];
int t[2000][2000];//縦、横
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

int main(){
	cin>>H>>W>>N;
	for(int i=0;i<H;i++){//縦
		cin>>f[i];
		for(int j=0;j<W;j++){//横
			if(f[i][j]=='S')	{chpt[0].x=i;chpt[0].y=j;}
			if(f[i][j]>48&&f[i][j]<58) {
				int n=f[i][j]-48;
				chpt[n].x=i;chpt[n].y=j;
			}
		}
	}
	int ans=0;//答えの時間
	for(int n=0;n<N;n++){//最短距離を求める
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				t[i][j]=INF;
			}
		}
		queue<P> nextp;
		P start={chpt[n].x,chpt[n].y};
		nextp.push(start);
		t[start.x][start.y]=0;
		
		while(!nextp.empty()){
			P cpt=nextp.front();
			nextp.pop();
			for(int i=0;i<4;i++){
				int nx=cpt.x+dx[i],ny=cpt.y+dy[i];
				if(nx>=0&&nx<H&&ny>=0&&ny<W&&f[nx][ny]!='X'&&t[nx][ny]==INF){
					t[nx][ny]=t[cpt.x][cpt.y]+1;
					P npt={nx,ny};
					nextp.push(npt);
				}
			}
		}
		ans+=t[chpt[n+1].x][chpt[n+1].y];
	}
	cout <<ans<<endl;
}