#301 (div.2) C. Ice Cave
发布时间:2020-12-13 20:42:35 所属栏目:PHP教程 来源:网络整理
导读:1.题目描写:点击打开链接 2.解题思路:本题利用BFS解决。由于行走的时候有两种情况,当遇到‘X'时会掉到下1层,当遇到’.'时还在本层,只不过’.'要变成'X'。那末直接用BFS进行搜索便可。如果遇到了’X',只需要判断是否是终点便可,否则跳过,如果遇到了‘
1.题目描写:点击打开链接 2.解题思路:本题利用BFS解决。由于行走的时候有两种情况,当遇到‘X'时会掉到下1层,当遇到’.'时还在本层,只不过’.'要变成'X'。那末直接用BFS进行搜索便可。如果遇到了’X',只需要判断是否是终点便可,否则跳过,如果遇到了‘.',那末将它改成‘X',并入队列便可。比赛时我1直在DFS和BFS之间徘徊不定,但其实不难发现,如果用DFS的话,可能有走不动的情况,需要往回撤。时间复杂度会比较高,因此自然会想到BFS。 3.代码: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;
#define N 500+5
int n,m;
int s,t,e,d;
int dx[] = { 1,⑴,0 };
int dy[] = { 0,1,⑴ };
char g[N][N];
typedef pair<int,int>P;
bool bfs()
{
queue<P>q;
q.push(P(s,t));
g[s][t] = 'X';
while (!q.empty())
{
s = q.front().first,t = q.front().second; q.pop();
for (int i = 0; i < 4; i++)
{
int xx = s + dx[i];
int yy = t + dy[i];
if (xx < 0 || xx >= n || yy < 0 || yy >= m)continue;
if (g[xx][yy] == 'X')
{
if (xx == e&&yy == d)return true;
continue;
}
g[xx][yy] = 'X';
q.push(P(xx,yy));
}
}
return false;
}
int main()
{
//freopen("t.txt","r",stdin);
while (~scanf("%d%d",&n,&m))
{
for (int i = 0; i < n; i++)
scanf("%s",g[i]);
scanf("%d%d%d%d",&s,&t,&e,&d);
s--,t--,e--,d--;
printf("%s
",bfs() ? "YES" : "NO");
}
return 0;
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |