Codeforces-242C:King's Path(BFS+map)
来源:互联网 发布:freebsd 在线安装软件 编辑:程序博客网 时间:2024/06/05 17:11
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
#include<bits/stdc++.h>using namespace std;const int MAX=1e5+10;map<pair<int,int>,int>v,s;int d[8][2]={-1,0,0,-1,1,0,0,1 ,-1,-1 ,1,1, 1,-1, -1, 1};int sx,sy,ex,ey,n;void bfs(){queue<int>p;v[make_pair(sx,sy)]=1;p.push(sx);p.push(sy);while(!p.empty()){int nx=p.front();p.pop();int ny=p.front();p.pop();for(int i=0;i<8;i++){int x=nx+d[i][0];int y=ny+d[i][1];if(s[make_pair(x,y)]==0)continue;if(v[make_pair(x,y)]!=0)continue;v[make_pair(x,y)]=v[make_pair(nx,ny)]+1;p.push(x);p.push(y);}}}int main(){s.clear();v.clear();scanf("%d%d%d%d",&sx,&sy,&ex,&ey);scanf("%d",&n);for(int i=1;i<=n;i++){int x,l,r;scanf("%d%d%d",&x,&l,&r);for(int j=l;j<=r;j++)s[make_pair(x,j)]=1;}bfs();if(v[make_pair(ex,ey)]>0)cout<<v[make_pair(ex,ey)]-1<<endl;else cout<<-1<<endl;return 0;}
- Codeforces-242C:King's Path(BFS+map)
- Codeforces 242C King‘s Path(BFS+STL)
- CodeForces 242C King's Path(bfs+stl)
- 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 776 C Molly's Chemicals】+ 前缀和 + map
- 【Codeforces 776 C. Molly's Chemicals】+ map + 前缀和
- 【Codeforces】C. Ice Cave(bfs)
- Codeforces 590C Three States(bfs)
- Ubuntu 下安装配置 Jenkins
- 合作协同进化算法概述(Cooperative Coevolution)
- 南阳理工oj 746 整数划分(四) 区间dp
- 微信测试号首次开发总结
- python 列表推导式
- Codeforces-242C:King's Path(BFS+map)
- 10.14 c语言基础
- python基础(2) 面向对象
- 第五周 项目3
- python 三元运算符
- LeetCode88 Merge Sorted Array
- UVALive 6258 Non-boring sequences
- Word Ctrl+V 复制多次
- 指针转换