Codeforces E. Two Labyrinths ( BFS
2017-03-26 23:40
281 查看
E. Two Labyrinths
time 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 size n × 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.
InputIn 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 these n lines consists of m 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
NOInput
3 5 ..... .#.## ..... ..... ##.#. .....Output
YES
3个BFS板子题 菜的难受
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define N 1000 char s1 , s2 ; bool vis ; int m, n; int dir[4][2] = {0,-1,-1,0,1,0,0,1}; struct node{ int x, y, step; }now,next; bool judge(int x,int y) { if(x>=0 && x<n && y>=0 && y<m) return true; else return false; } int bfs(char map ) { queue<node> q; memset(vis,false,sizeof(vis)); now.x = 0, now.y = 0, now.step = 0; q.push(now); vis[0][0] = true; while(!q.empty()) { now = q.front(); q.pop(); if(now.x==n-1 && now.y==m-1) return now.step; int x = now.x; int y = now.y; for(int i = 0;i < 4; i++) { int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(judge(xx,yy) && !vis[xx][yy] && map[xx][yy]!='#') { next.x = xx; next.y = yy; next.step = now.step+1; vis[xx][yy] = true; q.push(next); } } } return -1; } int main() { bool flag = true; scanf("%d%d",&n,&m); for(int i = 0;i < n; i++) scanf("%s",s1[i]); for(int i = 0;i < n; i++) scanf("%s",s2[i]); int ans_1 = bfs(s1); int ans_2 = bfs(s2); if((ans_1 != ans_2)||(ans_1 == ans_2&&ans_1==-1)) { flag = false; } for(int i = 0;i < n; i++) { for(int j = 0;j < m; j++) { if(s1[i][j]=='.' && s2[i][j]=='#') s1[i][j] = '#'; } } int ans = bfs(s1); if(ans != ans_1) flag = false; if(flag) puts("YES"); else puts("NO"); return 0; }
相关文章推荐
- 【Educational Codeforces Round 10E】【双连通分量缩环 BFS】Pursuit For Artifacts ★
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- 【Educational Codeforces Round 6D】【暴力 SET二分】Professor GukiZ and Two Arrays
- Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
- Educational Codeforces Round 2D. Area of Two Circles' Intersection
- Educational Codeforces Round 15 B. Powers of Two (数学)
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- Codeforces Gym 101064E A Word to Trump All [AC自动机+bfs]
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- Educational Codeforces Round 9-B. Alice, Bob, Two Teams(前缀和)
- Biridian Forest CodeForces - 329B (逆向bfs)
- Codeforces gym 100685 F. Flood bfs
- DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave
- Codeforces Round #268 (Div. 1) B. Two Sets 暴力
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- Educational Codeforces Round 37 E. Connected Components?(bfs)
- 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
- Educational Codeforces Round 37 E. Connected Components? 链表+bfs
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks (bfs)
- Alice, Bob, Two Teams codeforces 632B