king's path

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

 

#include <iostream>
#include<map>
#include<stdio.h>
#include<cstring>
#include<queue>

using namespace std;
int x0,y0,x1,y1;//起點 終點
pair<int,int>p;//用來存所有能走的點
map<pair<int,int>,int>m;//能走的點,是否能走
int Move[8][2]={//方向
1,1,
1,-1,
-1,1,
-1,-1,
0,1,
1,0,
-1,0,
0,-1};
int bfs()
{
    queue<pair<int,int> >q;
    p.first=x0;
    p.second=y0;
    m[p]=0;//将起点放入,并标记为已走过
    q.push(p);
    while(!q.empty()){
        p=q.front();
        q.pop();
        if(p.first==x1&&p.second==y1){//如果到达终点,返回步数
            return m[p];
        }
        for(int i=0;i<8;i++){
            pair<int,int> p1;
            p1.first=p.first+Move[i][0];
            p1.second=p.second+Move[i][1];
            if(m[p1]==-1&&m.count(p1)){//如果p1点可以走,并且没有走过,
                m[p1]=m[p]+1;//将上个点的步数加一,为当前点所走的步数
                q.push(p1);//将当前点放入队列
            }
        }
    }
    return -1;//如果没有走到终点,则返回-1,即无法走到终点
}
int main()
{
    while(scanf("%d %d %d %d",&x0,&y0,&x1,&y1)!=EOF){
        m.clear();
        int T,l,a,b;
        scanf("%d",&T);
        while(T--){
            scanf("%d %d %d",&l,&a,&b);
            for(int i=a;i<=b;i++){//将l行的a到b点一次标记为可走,
                p.first=l;
                p.second=i;
                m[p]=-1;
            }
        }
        if(!m.count(make_pair(x1,y1))) cout<<"-1"<<endl;
        else cout<<bfs()<<endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值