Codeforces Gym 100187 E. Two Labyrinths (双图BFS找共同最短路)
来源:互联网 发布:淘宝内裤真人秀图片 编辑:程序博客网 时间:2024/04/28 08:50
A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free cells sharing a side.
Constantine and Mike are the world leaders of composing the labyrinths. Each of them has just composed one labyrinth of sizen × m, and now they are blaming each other for the plagiarism. They consider that the plagiarism takes place if there exists such a path from the upper-left cell to the lower-right cell that is the shortest for both labyrinths. Resolve their conflict and say if the plagiarism took place.
In the first line two integers n and m (1 ≤ n, m ≤ 500) are written — the height and the width of the labyrinths.
In the next n lines the labyrinth composed by Constantine is written. Each of thesen lines consists ofm characters. Each character is equal either to «#», which denotes a wall, or to «.», which denotes a free cell.
The next line is empty, and in the next n lines the labyrinth composed by Mike is written in the same format. It is guaranteed that the upper-left and the lower-right cells of both labyrinths are free.
Output «YES» if there exists such a path from the upper-left to the lower-right cell that is the shortest for both labyrinths. Otherwise output «NO».
3 5......#.#...... .....#.#.#.....
NO
3 5......#.##..... .....##.#......
YES
题意:给你两个图,问两个图从(0,0)到(n-1,m-1)的最短路径是否相同
思路:用三次BFS,第一次寻找第一个图的最短路,第二次找第二个图的最短路,第三次查看两个图的最短路径是否相同。
这道题让我知道细心是多么重要,WA了10次,在同一组数据上,和傻逼一样查错查了一个半小时,换了几个姿势,最后发现就因为标记数组的大小写错了,就是开数组的时候少按了一个5,一个5,一个5啊,当时找的想撞墙了。。找出来之后简直是想吐血啊啊啊啊啊。。。
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#define MAXN 1010000#define LL long long#define ll __int64#include<iostream>#include<algorithm>#define INF 0x7fffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)using namespace std;LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}LL lcm(LL a,LL b){return a/gcd(a,b)*b;}ll powmod(ll a,LL b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}//headchar map[3][555][555];int v[3][555][555];//就是这里,写成了v[3][555][55]int vis[555][555];int cnt[3];int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int n,m;int bz;struct s{int x,y,step;};int check(int kk,int xx,int yy){if(xx<0||xx>=n||yy<0||yy>=m)return 0;if(v[kk][xx][yy]||map[kk][xx][yy]=='#')return 0;return 1;}queue<s>q;void bfs(int k){while(!q.empty())q.pop();s a,b;a.x=0;a.y=0;a.step=0;v[k][0][0]=1;q.push(a);while(!q.empty()){a=q.front();q.pop();if(a.x==n-1&&a.y==m-1&&a.step<cnt[k]){cnt[k]=a.step;continue;}for(int i=0;i<4;i++){b.x=a.x+dir[i][0];b.y=a.y+dir[i][1];if(check(k,b.x,b.y)){b.step=a.step+1;v[k][b.x][b.y]=1;//printf("debug\n");q.push(b);}}}}void bbfs(){while(!q.empty())q.pop();s a,b;a.x=0;a.y=0;a.step=0;vis[0][0]=1;q.push(a);while(!q.empty()){if(bz)break;a=q.front();q.pop();if(a.x==n-1&&a.y==m-1&&a.step==cnt[1]){bz=1;break;}for(int i=0;i<4;i++){b.x=a.x+dir[i][0];b.y=a.y+dir[i][1];if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&!vis[b.x][b.y]&&map[0][b.x][b.y]=='.'&&map[1][b.x][b.y]=='.'){b.step=a.step+1;vis[b.x][b.y]=1;q.push(b);}}}}int main(){int i;while(scanf("%d%d",&n,&m)!=EOF){mem(map);for(i=0;i<n;i++)scanf("%s",map[0][i]);for(i=0;i<n;i++)scanf("%s",map[1][i]);mem(v);cnt[0]=INF;cnt[1]=INF;bfs(0);bfs(1);//printf("%d %d\n",cnt[0],cnt[1]);if(cnt[0]!=cnt[1]){printf("NO\n");continue;}bz=0;mem(vis);bbfs();if(bz)printf("YES\n");elseprintf("NO\n");}return 0;}
- Codeforces Gym 100187 E. Two Labyrinths (双图BFS找共同最短路)
- Codeforces E. Two Labyrinths ( BFS
- Gym 100187E - Two Labyrinths
- Codeforces 100187E:Two Labyrinths(搜索&&BFS)
- codeforces-gym-100187-E【bfs】
- E - Two Labyrinths
- Codeforces gym 101149 L 最短路
- 最短路Codeforces E. Breaking Good
- CodeForces 507E Breaking Good 最短路
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
- 【最短路】NEERC15 F Froggy Ford (Codeforces GYM 100851)
- CodeForces Gym 100735E
- Codeforces gym 100685 E
- codeforces [Gym-100814E]
- Codeforces gym 101243 E
- 用BFS找最短路,并打印路径
- CodeForces 543B Destroying Roads(最短路BFS)
- Codeforces Round #361 (Div. 2) B BFS最短路
- HttpUrlConnection缓存,仿微信朋友圈图片缓存效果
- UI界面与实现功能的交互
- [LintCode]Insert Interval
- 树链剖分--细节发现 BSOJ3690:Grass Planting种草
- 学习笔记5 Supervised Convolutional Neural Network 之 Stochastic Gradient Descent
- Codeforces Gym 100187 E. Two Labyrinths (双图BFS找共同最短路)
- 1644 免费馅饼 题解(c++)
- BGL_polyhedron_3/normals.cpp analysis
- nyoj 1036 非洲小孩
- 利用jquery插件jquery.form.js异步上传图片
- hdu2819Swap(二分匹配+巧妙地建图)
- Exception:Request processing failed; nested exception is org.apache.ibatis.binding.BindingException
- HDU 2675 Equation Again
- Key-Value Coding (KVC)