King's Path(CF-242C)

Problem Description

The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).

You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.

Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.

Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.

Input

The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.

The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.

It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.

Output

If there is no path between the initial and final position along allowed cells, print -1.

Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.

Examples

Input

5 7 6 11
3
5 3 8
6 7 11
5 2 5

Output

4

Input

3 4 3 10
3
3 1 4
4 5 9
3 10 10

Output

6

Input

1 1 2 10
2
1 1 3
2 6 10

Output

-1

Note

In the first sample the values of f are as follows: f(1, 2) = 1 + 2 + 2 = 5, f(1, 3) = 1 + 3 + 2 = 6 and f(2, 3) = 2 + 3 = 5. So the difference between maximum and minimum values of f is 1.

In the second sample the value of h is large, so it's better for one of the sub-sequences to be empty.

题意:给出一个 1E9*1E9 的图,以及两个点的坐标 (x1,y1)、(x2,y2),再给出 n 行数,每行有 r、a、b 三个数,代表第 r 行的第 a~b 列是可以走的,现在要从 (x1,y1) 到 (x2,y2),每次能走相邻的 8 个格子,问最小步数,如果不能到达,输出 -1

思路:

如果不考虑图的大小的话,是一个十分基础的 bfs 题,但图的范围到 1E9,无法开那么大的数组

用 map+pair,这样一来,即可将 pair 表达的点在 map 中映射为步数,将所有可走的点都标记为 -1,如果这个点为 -1 才可以走,然后进行 bfs 即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define Pair pair<int,int>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

#define x first
#define y second
map<Pair,int> mp;
int bfs(Pair start,Pair endd){
    queue<Pair> Q;
    Q.push(start);
    mp[start]=0;
    while(!Q.empty()){
        Pair now=Q.front();
        Q.pop();
        if(now.x==endd.x&&now.y==endd.y)
            return mp[now];
        for(int i=0;i<8;i++){
            int nx=now.x+dx[i];
            int ny=now.y+dy[i];
            Pair next(nx,ny);

            if(mp[next]==-1){
                mp[next]=mp[now]+1;
                Q.push(next);
            }
        }
    }
    return -1;
}
int main() {
    Pair start,endd;
    int n;
    scanf("%d%d%d%d",&start.x,&start.y,&endd.x,&endd.y);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int r,a,b;
        scanf("%d%d%d",&r,&a,&b);
        for(int i=a;i<=b;i++){
            Pair temp(r,i);
            mp[temp]=-1;
        }
    }

    int res=bfs(start,endd);
    printf("%d\n",res);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值