CF540C Ice Cave

一、题目

点此看题

二、解法

ppl yyds \text{ppl yyds} ppl yyds

好巧的一道搜索题,可以分类讨论,如果终点是X,那么直接 b f s bfs bfs。否则一定会走到周围一个好点再走回去,我们就枚举这个好点,暂时把它弄成坏的,看能不能跑到终点。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
const int M = 505;
int read()
{
	int x=0,f=1;char c;
	while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
	while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
	return x*f;
}
int n,m,s1,s2,t1,t2,vis[M][M];char a[M][M];
int dx[4]={1,-1},dy[4]={0,0,1,-1};
struct node
{
	int x,y;
};
void bfs()
{
	memset(vis,0,sizeof vis);
	queue<node> q;
	vis[s1][s2]=1;
	q.push(node{s1,s2});
	while(!q.empty())
	{
		node t=q.front();q.pop();
		for(int i=0;i<4;i++)
		{
			int tx=t.x+dx[i],ty=t.y+dy[i];
			if(tx==t1 && ty==t2)
			{
				puts("YES");
				exit(0);
			}
			if(tx>=1 && tx<=n && ty>=1 && ty<=m && !vis[tx][ty] && a[tx][ty]=='.')
			{
				vis[tx][ty]=1;
				q.push(node{tx,ty});
			}
		}
	}
}
signed main()
{
	n=read();m=read();
	for(int i=1;i<=n;i++)
		scanf("%s",a[i]+1);
	s1=read();s2=read();t1=read();t2=read();
	if(a[t1][t2]=='X')
		bfs();
	else
	{
		for(int i=0;i<4;i++)
		{
			int tx=t1+dx[i],ty=t2+dy[i];
			if(a[tx][ty]=='X' || tx<1 || ty<1 || tx>n || ty>m) continue;
			a[tx][ty]='X';
			bfs();
			a[tx][ty]='.';
		}
	}
	puts("NO");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值