求助一道题
  • 板块灌水区
  • 楼主shipeiqian
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/7/25 08:23
  • 上次更新2023/10/27 18:33:44
查看原帖
求助一道题
632063
shipeiqian楼主2022/7/25 08:23

rt,题目,蒟蒻只有七十分的dfs:

#include <iostream>
using namespace std;
int n,m,a[45][45];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
bool found=false,vi[45][45];
bool check(int x,int y){
	if(x<0||y<0||x>n||y>m)return false;
	if(vi[x][y])return false;
	if(a[x][y])return false;
	return true;
}
void dfs(int x,int y){
	if(x==n&&y==m){
		found=true;
		return ;
	}
	vi[x][y]=true;
	for(int i=0;i<4;i++){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(check(nx,ny))dfs(nx,ny);
	}
	return ;
}
int main(){
	cin >>n >>m;
	char ch;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin >>ch;
			if(ch=='.')a[i][j]=0;
			else if(ch=='#')a[i][j]=1;
		}
	}
	dfs(1,1);
	cout <<(found?"YES":"NO");
	return 0;
}
2022/7/25 08:23
加载中...