CF540C Ice Cave 题解

CF传送门

洛谷传送门

洛谷题解传送门

CF *2000 水蓝一道。

一开始我以为 dfs 能过,结果 TLE on #14 了。

TLE CODE:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e3+100;
int n,m,sx,sy,fx,fy;
char a[maxn][maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void dfs(int x,int y){
//	for(int i=1;i<=n;i++){
//		for(int j=1;j<=n;j++) cout<<a[i][j];
//		puts("");
//	}
//	puts("");
	if(x==fx&&y==fy&&a[x][y]=='K'){
		puts("YES");
		exit(0);
	}
	for(int i=0;i<4;i++){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(nx==fx&&ny==fy&&a[nx][ny]=='X'){
			a[nx][ny]='K';
			dfs(nx,ny);
		} 
		if(nx>0&&nx<=n&&ny>0&&ny<=m&&a[nx][ny]=='.'){
			a[nx][ny]='X';
			dfs(nx,ny);
			a[nx][ny]='.'; 
		}
	}
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) cin>>a[i][j];
	} 
	cin>>sx>>sy>>fx>>fy; 
	dfs(sx,sy);
	cout<<"NO";
	return 0;
}

于是我只能 bfs ……

显然,没到终点却踩到了碎冰,就 GAME OVER 了。

所以,我们只能走正常的冰块,经过之后变成一块碎冰。

如果下一步走到终点并且终点是一块碎冰,就输出 YES。

若无法走到终点,输出 NO。

AC CODE:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e3+100;
int n,m,sx,sy,fx,fy;
char a[maxn][maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
struct node{
	int x,y; 
};
void bfs(){
	queue<node> q;
	node tmp=node{sx,sy};
	q.push(tmp);
	while(!q.empty()){
		tmp=q.front();
		for(int i=0;i<4;i++){
			int nx=tmp.x+dx[i];
			int ny=tmp.y+dy[i];
			if(nx==fx&&ny==fy&&a[nx][ny]=='X'){
				cout<<"YES";
				exit(0);
			} 
			if(nx>0&&nx<=n&&ny>0&&ny<=m&&a[nx][ny]=='.'){
				node res;
				res.x=nx;
				res.y=ny;
				a[nx][ny]='X';
				q.push(res);
			}
		}
		q.pop();
	} 
	cout<<"NO";
	return ;
} 
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) cin>>a[i][j];
	} 
	cin>>sx>>sy>>fx>>fy; 
	bfs();
	return 0;
}

本蒟蒻的主页

  • 18
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值