大意:建墙让所有好人可以到达坐标\((n,m)\),任何一个坏人都不能到达坐标\((n,m)\)
分析:
- 把坏人直接关起来,在坏人的四面建墙,
- 统计好人的人数
- 从坐标\((n,m)\)去遍历,整个地图,看能不能遇到所有好人
3可以通过dsu, flood-fill等去做吧
细节:
- 坏人的四面有好人,答案必为\(\text{NO}\)
- 坏人的四面中一个面可以到\((n, m)\),答案必为\(\text{NO}\)
更多细节见代码
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int n, m, cnt;
char op[55][55];
bool vis[55][55];
void dfs(int tx, int ty)
{
for(int i = 0; i < 4; i++)
{
int x = tx + dx[i];
int y = ty + dy[i];
if(x < 1 || x > n || y < 1 || y > m || vis[x][y] || op[x][y] == '#')
continue;
vis[x][y] = true;
if(op[x][y] == 'G')
cnt--;
dfs(x, y);
}
}
void solve()
{
cnt = 0;
memset(vis, false, sizeof vis);
cin>>n>>m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin>>op[i][j];
bool ok = true;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(op[i][j] == 'B')
{
for(int k = 0; k < 4; k++)
{
int x = i + dx[k];
int y = j + dy[k];
if(x < 1 || x > n || y < 1 || y > m)
continue;
if(op[x][y] == 'G')
ok = false;
if(op[x][y] == 'B')
continue;
op[x][y] = '#';
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(op[i][j] == 'G')
cnt++;
if(cnt != 0 && op[n][m] == '#')
{
cout<<"NO"<<endl;
return;
}
dfs(n, m);
if(cnt == 0 && ok)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return;
}