- P21's solution
P21's Solution
- 2025-9-5 18:37:23 @
食用性更强的 DFS 代码!
#include<bits/stdc++.h>
using namespace std;
const long long maxn=1e3+5;
long long T,k,i,j,bx,by;
char ch[maxn][maxn];
long long vis[maxn][maxn],dx[]={1,-1,0,0},dy[]={0,0,1,-1};
bool flag;
void dfs(long long x,long long y)
{
if(vis[x][y]) return;
vis[x][y]=1;
if(flag==true) return;
if(ch[x][y]=='L')
{
flag=true;
return;
}
for(long long it=0;it<4;it++)
{
long long hx=x+dx[it],hy=y+dy[it];
if(hx>k || hx<1 || hy>k || hy<1 || vis[hx][hy] || ch[hx][hy]=='X') continue;
//cout<<hx<<" "<<hy<<"\n";
dfs(hx,hy);
}
return;
}
int main()
{
freopen("honey1.in","r",stdin);
freopen("honey1.out","w",stdout);
ios::sync_with_stdio(false);
cin>>T;
while(T--)
{
flag=false;
cin>>k;
for(i=1;i<=k;i++)
{
for(j=1;j<=k;j++)
{
cin>>ch[i][j];
if(ch[i][j]=='B') bx=i,by=j;
vis[i][j]=0;
}
}
//cout<<bx<<" "<<by<<"\n";
dfs(bx,by);
if(flag) puts("Yes");
else puts("No");
}
return 0;
}
//Sleeping Cup can be better!
//CSP2024RP+=INF!