Gym - 100187E E - Two Labyrinths —— bfs
2017-04-19 21:19
260 查看
题目链接:http://codeforces.com/gym/100187/problem/E
题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点。但这种做法的错的,因为走出来的路对于两幅图来说不一定都是最短的。正确做法:
第一步:分别用bfs求出两图的最短路。
第二步:如果最短路长度一样。则将两幅图合并,再bfs,如果能走到终点,且最短路长度仍然等于未合并前的长度,则YES; 否则NO。
学习之处: 求两个或多个事物所共有的东西,其实就是求交集。
代码如下:
题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点。但这种做法的错的,因为走出来的路对于两幅图来说不一定都是最短的。正确做法:
第一步:分别用bfs求出两图的最短路。
第二步:如果最短路长度一样。则将两幅图合并,再bfs,如果能走到终点,且最短路长度仍然等于未合并前的长度,则YES; 否则NO。
学习之处: 求两个或多个事物所共有的东西,其实就是求交集。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<map> #include<string> #include<set> //#define LOCAL using namespace std; #define pb push_back #define ms(a, b) memset(a,b,sizeof(a)); typedef long long LL; const int inf = 0x3f3f3f3f; const int mod = 1000000007; const int maxn = 10; struct node { int x,y,k; }; node now, e; int n,m, vis[550][550]; char a[550][550], b[550][550], c[550][550]; int d[4][2] = {1,0,-1,0,0,1,0,-1}; int bfs(int x, int y, char s[][550]) { queue<node>q; memset(vis,0,sizeof(vis)); now.x = x; now.y = y; now.k = 0; vis[x][y] = 1; q.push(now); while(!q.empty()) { now = q.front(); q.pop(); if(now.x==n && now.y==m) return now.k; for(int i = 0; i<4; i++) { e.x = now.x + d[i][0]; e.y = now.y + d[i][1]; e.k = now.k + 1; if(e.x<1 || e.x>n || e.y<1 || e.y>m || vis[e.x][e.y] || s[e.x][e.y]=='#') continue; vis[e.x][e.y] = 1; q.push(e); } } return -1; } int main() { cin>>n>>m; for(int i = 1; i<=n; i++) scanf("%s",a[i]+1); for(int i = 1; i<=n; i++) scanf("%s",b[i]+1); for(int i = 1; i<=n; i++) for(int j = 1; j<=m; j++) { if(a[i][j]=='#' || b[i][j]=='#') c[i][j] = '#'; else c[i][j] = '.'; } int B = 0; int t1 = bfs(1,1,a);//分别搜两幅图的最短路 int t2 = bfs(1,1,b); if(t1 == t2 && t1 !=-1)//在最短路的前提下,再找两图合并的最短路是否存在并是否为原来的值 { if( bfs(1,1,c)== t1 ) B = 1; } if(B) puts("YES"); else puts("NO"); }
相关文章推荐
- Gym 100187E - Two Labyrinths
- codeforces-gym-100187-E【bfs】
- Gym 101201.B - Buggy Robot (bfs)
- Gym 100112H Horror List—深搜—bfs
- Gym 101572 D - 多源bfs- Distinctive Character
- GYM 100796 D.Journey(bfs)
- codeforces-gym-100187-J【dfs】
- Gym 100187H - Mysterious Photos
- !Gym 100625J 狱警放两犯人的最小开门数-bfs-(优先队列+bfs,求各自到交点的距离)
- Gym 100796D Journey(BFS)
- Gym - 100187D [逆元]
- codeforces-gym-100187-D【组合数】【逆元】
- codeforces Gym 100187A A. Potion of Immortality
- Gym 100187L - Ministry of Truth
- Gym 101196D Lost in Translation(BFS)
- Codeforces 100187E:Two Labyrinths(搜索&&BFS)
- Codeforces Gym 100187D D. Holidays 排列组合
- Gym 101149L Right Build[BFS]
- Gym - 100685F Flood BFS
- Gym - 100187A A - Potion of Immortality —— 贪心