LYQ_666 @ 2024-05-12 10:31:46
#include <bits/stdc++.h>
using namespace std;
char a[101][101];
int n,m,x1,x2,y,y2;
void dfs(int x,int y)
{
a[x][y]='#';
if(x-1>=0&&a[x-1][y]=='.') dfs(x-1,y);
if(x+1<n&&a[x+1][y]=='.') dfs(x+1,y);
if(y-1>=0&&a[x][y-1]=='.') dfs(x,y-1);
if(y+1<n&&a[x][y+1]=='.') dfs(x,y+1);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
x1=1;y=1;x2=n;y2=m;
if(a[x2][y2]=='#') cout<<"NO"<<endl;
else
{
dfs(x1,y);
if(a[x2][y2]=='.')
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
by Chu_awa_xing @ 2024-05-12 10:36:59
在dfs函数里加一个如果x=n,y=m就输出YES然后直接退出程序,然后在主函数里调用dfs函数下一行写输出NO
by Chu_awa_xing @ 2024-05-12 10:38:30
建议写搜索程序再开一个vis数组表示当前点是否走过
by LYQ_666 @ 2024-05-12 11:10:58
@Chu_awa_xing 谢谢