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.
InputThe 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.
OutputIf 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.
Example5 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
题目分析:做过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;}
- Codeforces Round #149 (Div. 2) (bfs+STL)
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #295 (Div. 2) B bfs
- Codeforces Round #375 (Div. 2) D bfs
- Codeforces Round #Pi (Div. 2) (STL专场)
- CodeForces-540C//Codeforces Round #301 (Div. 2)C BFS
- Codeforces Round #149 (Div. 2)
- Codeforces Round #149 (Div. 2)
- Codeforces Round #149 (Div. 2)
- Codeforces Beta Round #35 (Div. 2) C (BFS~水~)
- Codeforces Round #301 (Div. 2)C - Ice Cave-bfs
- Codeforces Round #346 (Div. 2) F bfs+并查集
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth (BFS)
- Codeforces Round #354 (Div. 2) D (BFS)
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(BFS)
- Codeforces Round #355D (Div. 2) 暴力+BFS
- Codeforces Round #361 (Div. 2) B BFS最短路
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- 编程检验ASCII码值与字符对应关系 P11
- 第九章0.1解读
- Codeforces Round #441 (Div.2)
- 算数运算符和算术表达式
- Cygwin 和MinGW 的区别与联系是怎样的?
- Codeforces Round #149 (Div. 2) (bfs+STL)
- Fault Injection——持续更新
- 游戏‘微信打飞机 第一课
- 关于win10下搭建kafka0.10的一些坑
- 阿里巴巴Java开发规约插件安装IDEA和Eclipse教程
- Java的单例及多例设计模式
- 第1章 引论
- scala Currying
- Android设备间通信(wifi连接)