2 条题解
-
3
直接从
B
位置开始 Floodfill 即可。如果 Floodfill 到了L
位置,那么答案为Yes
,否则为No
。#include <bits/stdc++.h> using namespace std; const int K = 1000 + 12; char x[K][K]; int main() { freopen("honey1.in", "r", stdin); freopen("honey1.out", "w", stdout); int T; cin >> T; while (T--) { int k, sx = 0, sy = 0; cin >> k; for (int i = 1; i <= k; i++) for (int j = 1; j <= k; j++) { cin >> x[i][j]; if (x[i][j] == 'B') sx = i, sy = j; } for (int i = 0; i <= k + 1; i++) x[0][i] = x[i][0] = x[i][k + 1] = x[k + 1][i] = 'X'; queue<pair<int, int> > q; q.push(make_pair(sx, sy)); x[sx][sy] = 'X'; bool ok = false; while (!q.empty()) { pair<int, int> u = q.front(); q.pop(); if (x[u.first][u.second + 1] == 'L') { ok = true; puts("Yes"); break; } if (x[u.first][u.second + 1] != 'X') { x[u.first][u.second + 1] = 'X'; q.push(make_pair(u.first, u.second + 1)); } if (x[u.first][u.second - 1] == 'L') { ok = true; puts("Yes"); break; } if (x[u.first][u.second - 1] != 'X') { x[u.first][u.second - 1] = 'X'; q.push(make_pair(u.first, u.second - 1)); } if (x[u.first + 1][u.second] == 'L') { ok = true; puts("Yes"); break; } if (x[u.first + 1][u.second] != 'X') { x[u.first + 1][u.second] = 'X'; q.push(make_pair(u.first + 1, u.second)); } if (x[u.first - 1][u.second] == 'L') { ok = true; puts("Yes"); break; } if (x[u.first - 1][u.second] != 'X') { x[u.first - 1][u.second] = 'X'; q.push(make_pair(u.first - 1, u.second)); } } if (!ok) puts("No"); } return 0; }
-
2
食用性更强的 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!
- 1
信息
- ID
- 30
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 3
- 标签
- 递交数
- 25
- 已通过
- 4
- 上传者