您的位置:首页 > 其它

Codeforces 100187E:Two Labyrinths(搜索&&BFS)

2017-03-20 10:40 435 查看
E. Two Labyrinthstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA 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 forboth labyrinths. Resolve their conflict and say if the plagiarism took place.InputIn the first line two integers n andm (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.OutputOutput «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».ExamplesInput
3 5
.....
.#.#.
.....

.....
#.#.#
.....
Output
NO
Input
3 5
.....
.#.##
.....

.....
##.#.
.....
Output
YES
题目大意:两个人设计迷宫,俩人起争执说彼此抄袭彼此的迷宫地图,让你判断他俩是否抄袭彼此。抄袭的条件是存在一条相同的一条路都是彼此的最短路。
解题思路:先求各自最短路的步数,如果步数都不一样的话,那么肯定不是抄袭的图。如果步数一样的话,两张图一起进行搜索,看是否存在一条共同的通路,
且通路的步数为之前各自的最短路长度即可。(用广搜快,深搜写的在13个例子超时了。。。。)
代码:
#include <cstdio>#include <cstring>#include <queue>using namespace std;char map[2][510][510];//存两个地图int visit[2][510][510];//为防止破坏map地图而设置的记录方位的路径变int cnt[2];//统计两张地图的最短步数int dirx[4]={-1,1,0,0};int diry[4]={0,0,-1,1};int n,m;int flag;struct node{int x,y,step;};queue<node > q;int cheak(struct node x,int kk){if(x.x>=0&&x.x<n&&x.y>=0&&x.y<m&&(map[kk][x.x][x.y]=='.'&&visit[kk][x.x][x.y]==0)){return 1;}else{return 0;}}void bfs(int k){while(!q.empty()){q.pop();}visit[k][0][0]=1;node a,b;a.x=0,a.y=0,a.step=1;q.push(a);while(!q.empty()){a=q.front();q.pop();if(a.x==n-1&&a.y==m-1){if(cnt[k]>a.step){cnt[k]=a.step;}}for(int i=0;i<4;i++){b.x=a.x+dirx[i];b.y=a.y+diry[i];if(cheak(b,k))//判断下一个点是否可走{b.step=a.step+1;visit[k][b.x][b.y]=1;q.push(b);}}}}void bbfs(int k){while(!q.empty()){q.pop();}visit[k][0][0]=1;node a,b;a.x=0,a.y=0,a.step=1;q.push(a);while(!q.empty()){a=q.front();q.pop();if(a.x==n-1&&a.y==m-1){if(cnt[k]==a.step){flag=1;break;//广搜的特点是一旦到达目的地,那么一定是最短路}}for(int i=0;i<4;i++){b.x=a.x+dirx[i];b.y=a.y+diry[i];if(cheak(b,k)&&map[0][b.x][b.y]=='.'&&map[1][b.x][b.y]=='.')//两个图都能走{b.step=a.step+1;visit[k][b.x][b.y]=1;q.push(b);}}}}int main(){scanf("%d%d",&n,&m);for(int i=0;i<n;i++){scanf("%s",map[0][i]);}for(int i=0;i<n;i++){scanf("%s",map[1][i]);}cnt[0]=0x3f3f3f3f;cnt[1]=0x3f3f3f3f;memset(visit,0,sizeof(visit));bfs(0);bfs(1);if(cnt[0]!=cnt[1])//如果各自的最短路的步数都不一样,那么肯定不是一张抄袭的图{printf("NO\n");return 0;}memset(visit,0,sizeof(visit));flag=0;bbfs(0);//两张图一起搜if(flag==1){printf("YES\n");}else{printf("NO\n");}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  搜索 BFS