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>
#define fi first
#define se second
using namespace std;
int dy[4]={0,0,-1,1},dx[4]={1,-1,0,0};
int X,Y,N;
char field[1000][1001];
pair<int,int> time1[1000][1001];
struct point{
    int y,x,t,n;
    point(int y_,int x_,int t_,int n_){
        y=y_;x=x_;t=t_;n=n_;
    }
};
int bfs(int y,int x);
int main(){
    cin>>Y>>X>>N;
    int rep;
    for(int i=0;i<Y;i++)cin>>field[i];
    for(int i=0;i<Y;i++){
        for(int j=0;j<X;j++){
            time1[i][j].fi=time1[i][j].se=-1;
        }
    }
    for(int i=0;i<Y;i++){
        bool flag=false;
        for(int j=0;j<X;j++){
            if(field[i][j]=='S'){
                rep=bfs(i,j);
                flag=true;
                break;
            }
        }
        if(flag)break;
    }
    cout<<rep<<endl;
    return 0;

}

int bfs(int y,int x){
    point start=point(y,x,0,0);
    queue<point>Q;
    Q.push(start);
    while(Q.size()){

        point a=Q.front();
        Q.pop();
        if(a.y<0 || a.y>=Y ||a.x<0||a.x>=X)continue;

        if(field[a.y][a.x]=='X')continue;

        if(a.n+1==field[a.y][a.x]-'0')a.n++;

        if(a.n>time1[a.y][a.x].se){
            time1[a.y][a.x].fi=a.t;
            time1[a.y][a.x].se=a.n;
        }else if(a.n==time1[a.y][a.x].se&&a.t<time1[a.y][a.x].fi){
            time1[a.y][a.x].fi=a.t;
        }else continue;

        if(a.n==N){
            return a.t;
        }


        for(int i=0;i<4;i++){
            point next=point(a.y+dy[i],a.x+dx[i],a.t+1,a.n);
            Q.push(next);
        }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 1e6+5
using namespace std;
struct p{
    int x;
    int y;
};

int h,w,n;
char ma[1005][1005];
int t[1005][1005];
int dir[4][2]={ { -1, 0 },{ 1, 0 },{ 0, -1 },{ 0, 1 }};
int f[16][2];
typedef pair<int, int> P;
int bfs(int sx,int sy,int ex,int ey)
{
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            t[j][i] = -1;
        }
    }
    queue<P> que;
    que.push(P(sx,sy));
    t[sx][sy]=0;
    while(!que.empty())
    {
         P p=que.front();
         que.pop();
         if(p.first==ex&&p.second==ey)
            break;
         for(int i=0;i<4;i++)
         {
             int nx=p.first+dir[i][0];
             int ny=p.second+dir[i][0];
             if(0 <= nx && nx < w && 0 <= ny && ny < h && ma[nx][ny] != 'X' && t[nx][ny] == -1)
            {
                    que.push(P(nx,ny));
                    t[nx][ny]=t[p.first][p.second]+1;
            }
         }
    }
    return t[ex][ey];
}
int main()
{
    int h,w,n;
    while(scanf("%d %d %d",&h,&w,&n)!=EOF)
    {
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++){
                cin>>ma[j][i];
                if(ma[j][i]=='S')
                 {
                     f[0][0] = j;
                    f[0][1] = i;
                    ma[j][i] = '.';
                 }
                else if (isdigit(ma[j][i]))
                {
                    int x = ma[j][i] - '0';
                    f[x][0] = j;
                    f[x][1] = i;
                }
            }
        int step=0;
        for(int i=0;i<n;i++)
        {
            step+=bfs(f[i][0],f[i][1],f[i+1][0],f[i+1][1]);
        }
        cout<<step<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stdlib.h>
#include <queue>
#define MAX_NUM 1010
using namespace std;
int H, W, N;
bool flag[MAX_NUM][MAX_NUM] = {false};

class Node
{
public:
	Node();
	Node(int x, int y, int step);
	~Node();
	int x;
	int y;
	int step;
};

Node::Node()
{
}

Node::Node(int x, int y, int step) {
	this->x = x;
	this->y = y;
	this->step = step;
}

Node::~Node()
{
}
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int BFS(char map[][MAX_NUM], int sx, int sy, int ex, int ey) {
	queue<Node*> que;
	que.push(new Node(sx, sy, 0));
	flag[sx][sy] = true;
	while (!que.empty())
	{
		Node* cur = que.front();
		que.pop();
		if (cur->x == ex && cur->y == ey)
		{
			return cur->step;
		}
		else
		{
			for (int i = 0; i < 4; i++)
			{
				int k = cur->x + dir[i][0];
				int v = cur->y + dir[i][1];
				if (k>=0&&k<H&&v>=0&&v<W&&map[k][v]!='X'&&!flag[k][v])
				{
					que.push(new Node(k, v, cur->step + 1));
					flag[k][v] = true;
				}
			}
		}
	}
	return -1;

}

int main()
{
	int sx, sy, arr[10][2];
	char map[MAX_NUM][MAX_NUM];
	while (cin >> H >> W >> N && H && W)
	{
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> map[i][j];
				if (map[i][j] == 'S')
				{
					map[i][j] = '.';
					sx = i;
					sy = j;
				}
				if (isdigit(map[i][j]))
				{
					int index = map[i][j] - '0';
					arr[index][0] = i;
					arr[index][1] = j;
				}
			}
		}
		memset(flag, false, sizeof(flag));
		int res = 0;
		res += BFS(map, sx, sy, arr[1][0], arr[1][1]);
		for (int i = 1; i+1 <= N; i++)
		{
			memset(flag, false, sizeof(flag));
			res += BFS(map, arr[i][0], arr[i][1], arr[i + 1][0], arr[i + 1][1]);
		}
		cout << res << endl;
	}
	return 0;
}