CodeForces 242C King's Path(bfs+stl)

忘是亡心i 2022-06-07 07:56 270阅读 0赞

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 r**i, a**i, b**i (a**i ≤ b**i), denoting that cells in columns from number a**i to number b**i inclusive in the r**i-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 r**i, a**i, b**i (1 ≤ r**i, a**i, b**i ≤ 109, a**i ≤ b**i), denoting that cells in columns from number a**i to number b**i inclusive in the r**i-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

  1. 5 7 6 11
  2. 3
  3. 5 3 8
  4. 6 7 11
  5. 5 2 5

Output

  1. 4

Input

  1. 3 4 3 10
  2. 3
  3. 3 1 4
  4. 4 5 9
  5. 3 10 10

Output

  1. 6

Input

  1. 1 1 2 10
  2. 2
  3. 1 1 3
  4. 2 6 10

Output

  1. -1

题解:

题意:

给你一个棋盘的起点和终点坐标,你可以往八个方向走,然后下面是你可以走的点的横坐标x,和点纵坐标的范围[l,r],问最少多少步可以走到终点

思路:

比较水的一道bfs,注意点是范围很大,要用map,因为是二维坐标,用到pair,bfs搞的去就好了

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<stdio.h>
  4. #include<math.h>
  5. #include<string>
  6. #include<stdio.h>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<deque>
  12. #include<algorithm>
  13. #define ll long long
  14. #define INF 1008611111
  15. #define M (t[k].l+t[k].r)/2
  16. #define lson k*2
  17. #define rson k*2+1
  18. using namespace std;
  19. int dirx[8]={0,0,-1,1,1,1,-1,-1};
  20. int diry[8]={1,-1,0,0,1,-1,-1,1};
  21. struct node
  22. {
  23. int x,y;
  24. ll step;
  25. };
  26. int sx,sy,ex,ey;
  27. map<pair<int,int>,int>p;
  28. queue<node>q;
  29. int main()
  30. {
  31. int i,j,n,m,x,l,r,xx,yy;
  32. scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
  33. scanf("%d",&m);
  34. for(i=0;i<m;i++)
  35. {
  36. scanf("%d%d%d",&x,&l,&r);
  37. for(j=l;j<=r;j++)
  38. {
  39. pair<int,int>now;
  40. now.first=x;
  41. now.second=j;
  42. p[now]=1;
  43. }
  44. }
  45. pair<int,int>temp;
  46. temp.first=sx;
  47. temp.second=sy;
  48. p[temp]=0;
  49. node now,next;
  50. now.x=sx;
  51. now.y=sy;
  52. now.step=0;
  53. q.push(now);
  54. ll res=-1;
  55. while(!q.empty())
  56. {
  57. now=q.front();
  58. q.pop();
  59. for(i=0;i<8;i++)
  60. {
  61. temp.first=now.x+dirx[i];
  62. temp.second=now.y+diry[i];
  63. if(p[temp])
  64. {
  65. next.x=temp.first;
  66. next.y=temp.second;
  67. next.step=now.step+1;
  68. if(next.x==ex&&next.y==ey)
  69. {
  70. res=next.step;
  71. goto loop;
  72. }
  73. q.push(next);
  74. p[temp]=0;
  75. }
  76. }
  77. }
  78. loop:;
  79. printf("%d\n",res);
  80. return 0;
  81. }

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Codeforces 1204C

    题意略。 思路:我的想法是逐步地找出这个序列中的重要点,我要判断当前这个点能不能删去,就要看上一个重要点和当前这个点 i 在序列中的下一个点 i + 1之间的距离 是否是最

    相关 CodeForces1214C

    [CodeForces1214C][] 是个不是很难的题目. 首先考虑如果左右括号数量不匹配那么肯定无论如何都不能通过移动一个括号完成匹配. 否则,我们考虑,将所有

    相关 codeforces 567C

    mp2存放每个数当中间的数的次数,对每个数,如果有a\[i\]%k==0,那么ans就加上mp2\[a\[i\]/k\],mp表示这个数当第一个数的次数,对每个数,如果有a\[

    相关 CodeForces 455C

    题意:给定N,M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的,然后是Q次操作,操作分为两种,一种是查询城市x所在的联通集合中,最长的路为多长。二是连接两个联