CodeForces 242C King's Path(bfs+stl)
来源:互联网 发布:昆明暴恐 中国公知 编辑:程序博客网 时间:2024/06/09 19:39
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.
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.
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.
5 7 6 1135 3 86 7 115 2 5
4
3 4 3 1033 1 44 5 93 10 10
6
1 1 2 1021 1 32 6 10
-1
题解:
题意:
给你一个棋盘的起点和终点坐标,你可以往八个方向走,然后下面是你可以走的点的横坐标x,和点纵坐标的范围[l,r],问最少多少步可以走到终点
思路:
比较水的一道bfs,注意点是范围很大,要用map,因为是二维坐标,用到pair,bfs搞的去就好了
代码:
#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>#include<algorithm>#define ll long long#define INF 1008611111#define M (t[k].l+t[k].r)/2#define lson k*2#define rson k*2+1using namespace std;int dirx[8]={0,0,-1,1,1,1,-1,-1};int diry[8]={1,-1,0,0,1,-1,-1,1};struct node{ int x,y; ll step;};int sx,sy,ex,ey;map<pair<int,int>,int>p;queue<node>q;int main(){ int i,j,n,m,x,l,r,xx,yy; scanf("%d%d%d%d",&sx,&sy,&ex,&ey); scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d%d%d",&x,&l,&r); for(j=l;j<=r;j++) { pair<int,int>now; now.first=x; now.second=j; p[now]=1; } } pair<int,int>temp; temp.first=sx; temp.second=sy; p[temp]=0; node now,next; now.x=sx; now.y=sy; now.step=0; q.push(now); ll res=-1; while(!q.empty()) { now=q.front(); q.pop(); for(i=0;i<8;i++) { temp.first=now.x+dirx[i]; temp.second=now.y+diry[i]; if(p[temp]) { next.x=temp.first; next.y=temp.second; next.step=now.step+1; if(next.x==ex&&next.y==ey) { res=next.step; goto loop; } q.push(next); p[temp]=0; } } } loop:; printf("%d\n",res); return 0;}
- Codeforces 242C King‘s Path(BFS+STL)
- CodeForces 242C King's Path(bfs+stl)
- Codeforces-242C:King's Path(BFS+map)
- Codeforces-242C-King's Path(bfs)
- codeforces 242C King's Path STL库
- [CF 242C][BNUOJ 26638] King's Path [最短路]
- Codeforces 3A-Shortest path of the king(BFS打印路径)
- Codeforces 3A Shortest path of the king BFS
- Codeforces 3A Shortest path of the king(BFS???NO!!)
- codeforces 3-A. Shortest path of the king(暴力)
- Codeforces 30D King's Problem? 模拟
- codeforces 30D King's Problem? 数学
- Codeforces Gym 100792K King's Rout
- Codeforces Gym 101142 K. King's Heir
- 【Codeforces】C. Ice Cave(bfs)
- Codeforces 590C Three States(bfs)
- CodeForces 540 C Ice Cave (BFS)
- codeforces 616C The Labyrinth(bfs)
- Vue, App与我(十三)
- python堆
- SQL语句范例之autotrace和hints
- angularjs公告墙
- leetcode_669.Trim a Binary Search Tree?待解决
- CodeForces 242C King's Path(bfs+stl)
- Spring框架中AOP设计基本原理
- spark之action算子
- [Java]stop()不能用,如何停止线程?
- 最后冲刺—信息系统开发与管理
- okhttp上传文件
- Python chapter 5 learning notes
- Docker学习笔记
- NOIP 2008 Senior 4