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<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#define pr(x) cout<<#x<<" "<<x;
#define pl(x) cout<<#x<<" "<<x<<endl;
#include<math.h>
#include<algorithm>
using namespace std;
char maze[1005][1005];
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};
int d[1005][1005];
const int INF = 0x7f7f7f7f;
typedef pair <int ,int > p;
queue <p> que;
int h,w;
void chushihua(){
    for(int i = 0;i<1005;i++){
        for(int j = 0;j<1005;j++){
            d[i][j] = INF;
        }
    }
}
int bfs(int x,int y,int gx,int gy){
    chushihua();
    d[x][y] = 0;
    que.push(p(x,y));
    while(que.size()){
        p cp = que.front();
        que.pop();
        if(cp.first==gx&&cp.second==gy){break;}
        for(int i = 0;i<4;i++){
            int nx = cp.first + dx[i];
            int ny = cp.second + dy[i];
            if(nx>=0&&nx<h&&ny>=0&&ny<w&&maze[nx][ny]!='X'&&d[nx][ny]==INF){

               que.push(p(nx,ny));
               d[nx][ny] = d[cp.first][cp.second] + 1;

            }
        }
    }
    while(que.size()){que.pop();}
    return d[gx][gy];


}
int main(){
    int n;
    cin>>h>>w>>n;
    int indexmap[n+5][4];
    getchar();
    for(int i = 0;i<h;i++){
        scanf("%s",&maze[i]);
    }
    for(int i = 0;i<h;i++){
        for(int j = 0;j<w;j++){
            if(maze[i][j]=='S'){
                indexmap[0][0] = i;
                indexmap[0][1] = j;

            }
            else if((int)maze[i][j]>=49&&(int)maze[i][j]<=57){
                int index = maze[i][j]-'0';
                indexmap[index][0] = i;
                indexmap[index][1] = j;


            }
        }
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        ans += bfs(indexmap[i][0],indexmap[i][1],indexmap[i+1][0],indexmap[i+1][1]);
    }
    cout<<ans<<endl;

}

C++: #include<stdio.h>
#include<utility>
#include<queue>
using namespace std;

char maze[1000][1001];
void solve()
{
	bool flag[1000][1000]={0};
	int h,w,n,pow=1,ans=0;
	int h_route[4]={1,0,-1,0};
	int w_route[4]={0,1,0,-1};
	typedef pair <int,int> P;
	typedef pair<int ,P> P2;
	P data[10];


	scanf("%d %d %d\n",&h,&w,&n);
	
	for(int i=0;i<h;i++)//迷路読み込み
		for(int j=0;j<w+1;j++)
		{
			scanf("%c",&maze[i][j]);
			if(maze[i][j]=='S')
				data[0]=P(i,j);
			else if(maze[i][j] >='1' && maze[i][j] <='9')
				data[maze[i][j]-'0']=P(i,j);
		}

	for(int i=0;i<n;i++)
	{
		queue<P2> que;
		que.push(P2(ans,P(data[i].first,data[i].second)));
		while(!que.empty())
		{

			P2 temp = que.front();
			que.pop();
			if(temp.second.first==data[i+1].first && temp.second.second==data[i+1].second)
			{
				memset(flag,0,sizeof(flag));
				ans=temp.first;
				break;
			}
			for(int j=0;j<4;j++)
			{
				int Th=temp.second.first+h_route[j];
				int Tw=temp.second.second+w_route[j];
				if(Th>=0 && Th<h && Tw>=0 && Tw<w && maze[Th][Tw]!='X' && flag[Th][Tw]!=1)
				{
					que.push(P2(temp.first+1,P(Th,Tw)));
					flag[Th][Tw]=1;
				}
			}
		}
		
	}
	printf("%d\n",ans);
}

int main()
{
	solve();
}

C++: #include <iostream>
#include <utility>
#include <queue>
using namespace std;

typedef pair<int, int> Pair;
const int INF = 100000000;

int H, W;
char N;
const int MAX_H = 1000, MAX_W = 1000;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
int d[MAX_H][MAX_W];
int time;

void bfs() {
    queue<Pair> que;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            d[i][j] = INF;
        }
    }
    char muscle = '1';
    que.push(Pair(sx,sy));
    d[sx][sy] = 0;
    while (que.size()) {
        Pair p  = que.front(); que.pop();
        if (muscle-1 == N) break;
        for (int i = 0; i < 4; i++) {
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if (0 <= nx && nx < H && 0 <= ny && ny < W && plant[nx][ny] != 'X' && d[nx][ny] == INF) {
                que.push(Pair(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
            if (muscle == plant[nx][ny]) {
                while(que.size()) {que.pop();}
                que.push(Pair(nx, ny));
                muscle = muscle + 1;
                time += d[nx][ny];
                for (int i = 0; i < H; i++) {
                    for (int j = 0; j < W; j++) {
                        d[i][j] = INF;
                    }
                }
                d[nx][ny] = 0;
                break;
            }
        }
    }
}

void solve() {
    time = 0;
    bfs();
    cout << time << endl;
}

int main () {
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> plant[i][j];
            if(plant[i][j] == 'S') {sx = i; sy = j;}
        }
    }
    solve();
}

C++: #include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node{
	int x,y,z;
};
node p,next1;
char a[1010][1010];
int n,m,k,b[1010][1010];
int d[4][2]={0,1,0,-1,1,0,-1,0};
void bfs(int &x,int &y,int z,int &ans){
	p.x=x;
	p.y=y;
	p.z=0;
	queue<node> q;
	while(!q.empty())
		q.pop();
	q.push(p);
	while(!q.empty()){
		p=q.front();
		q.pop();	 
		for(int i=0;i<4;i++){
			next1.x=p.x+d[i][0];
			next1.y=p.y+d[i][1];
			if(a[next1.x][next1.y]!='X'&&next1.x>0&&next1.x<=n&&next1.y>0&&next1.y<=m&&b[next1.x][next1.y]==0){
				if(a[next1.x][next1.y]==z+48){
					ans+=p.z+1;
					x=next1.x;
					y=next1.y;
					return;
				}
				next1.z=p.z+1;\
				b[next1.x][next1.y]=1;
				q.push(next1);
			}
		}
		
	}
}
int main(){
	int i,j,x,y,ans;
	scanf("%d%d%d",&n,&m,&k);
	for(i=1;i<=n;i++){
		scanf("%s",a[i]);
		for(j=strlen(a[i]);j>0;j--){
			a[i][j]=a[i][j-1];
			if(a[i][j]=='S'){
				x=i;
				y=j;
			}
		}
		a[i][0]='0';
	}
	ans=0;
	for(i=1;i<=k;i++){
		memset(b,0,sizeof(b));
		b[x][y]=1;
		bfs(x,y,i,ans);
	}
	printf("%d\n",ans);
	return 0;
}