Codeforces Round #149 (Div. 2) (bfs+STL)

来源:互联网 发布:mac下载os x yosemite 编辑:程序博客网 时间:2024/05/01 19:57

问题描述:

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.

Example

Input
5 7 6 1135 3 86 7 115 2 5
Output
4
Input
3 4 3 1033 1 44 5 93 10 10
Output
6
Input
1 1 2 1021 1 32 6 10
Output
-1
题目题意:题目给了我们起点和终点的坐标和n个可行区域,要求点只能在可行域内移动(可以向八个方向移动)。问最短路经,没有输出-1.

题目分析:做过BFS的应该可以马上意思到,需要广度优先搜索,但是因为题目给的图非常大,保存整个图肯定不行。其实我们在进行搜索的时候,利用主要是可行域,

其他不能走的地方(判断能否走或者干脆不保存)

我们用map<pair<int,int>,int> 来保存可行域就行了. pair<int,int> 保存的是可以走的点。

代码如下:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<map>#define ll long longusing namespace std;int Next[8][2]={{-1,-1},{0,-1},{1,-1},{1,0},{-1,0},{-1,1},{0,1},{1,1}};queue<pair<int,int> > que;map<pair<int,int>,int> m;ll ans;int sx,sy,ex,ey;void bfs(){    while (que.size()) que.pop();    pair<int,int> S;    S=make_pair(sx,sy);    m[S]=0;    que.push(S);    while (que.size()) {        pair<int,int> cur,next;        cur=que.front();        if (cur.first==ex&&cur.second==ey) { ans=m[cur]; return ;}        que.pop();        for (int i=0;i<8;i++) {            next.first=cur.first+Next[i][0];            next.second=cur.second+Next[i][1];            if (m.find(next)!=m.end()&&m[next]==-1) {//在可行域内且没走过               m[next]=m[cur]+1;//步数加一               que.push(next);            }        }    }}int main(){    int n;    scanf("%d%d%d%d",&sx,&sy,&ex,&ey);    scanf("%d",&n);    for (int i=0;i<n;i++) {        int row,l,r;        scanf("%d%d%d",&row,&l,&r);        for (int i=l;i<=r;i++) {//因为列区间里的每一个都可以走            m[make_pair(row,i)]=-1;//没有走过        }    }    ans=-1;    bfs();    printf("%lld\n",ans);    return 0;}












原创粉丝点击