CF1365D Solve The Maze (BFS)

原题链接
题意:
在这里插入图片描述
思路:
考虑最为保险的放法,就是将每个坏人的四周都放墙,这样坏人一定无法到达终点,但是会对好人的移动产生影响,有可能会造成好人也无法到达。只需要检验一下这种构造之后有多少个好人能够到达终点即可。
好人有多个,终点却只有一个。可以从终点倒着跑bfs,看能够到达多少个好人。
其中要注意两点:
1.如果坏人周围有好人的话,无法构造。
2.如果构造后终点变成了墙,不一定是无法构造;如果这时候好人的数量为0应该是Yes。

代码:

#pragma GCC optimize(3)
///#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
char F[200];
inline void out(I_int x)
{
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0)
    {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
const int inf=0x3f3f3f3f,mod=998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+100,maxm=3e5+7,N=1e6+7;
const double PI = atan(1.0)*4;
int n,m;
char mp[55][55];
int nx[]= {0,0,1,-1};
int ny[]= {1,-1,0,0};
void solve()
{
    n=read(),m=read();
    for(int i=1; i<=n; i++) cin>>mp[i]+1;
    int cnt1=0,cnt2=0,flag=1;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(mp[i][j]=='G') cnt1++;
            else if(mp[i][j]=='B')
            {
                ///将四周都堵上墙
                for(int k=0; k<4; k++)
                {
                    int xx=i+nx[k],yy=j+ny[k];
                    if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                    {
                        if(mp[xx][yy]=='.') mp[xx][yy]='#';
                        else if(mp[xx][yy]=='G')
                        {
                            puts("No");
                            return ;
                        }
                    }
                }
            }
    if(mp[n][m]=='#')
    {
        if(cnt1==0) puts("Yes");
        else puts("No");
        return ;
    }
    queue<PII>q;
    q.push({n,m});
    mp[n][m]='#';
    while(!q.empty())
    {
        PII t=q.front();
        q.pop();
        int x=t.first,y=t.second;
        for(int i=0; i<4; i++)
        {
            int xx=x+nx[i],yy=y+ny[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]!='#')
            {
                if(mp[xx][yy]=='G') cnt2++;
                q.push({xx,yy});
                mp[xx][yy]='#';
            }
        }
    }
    if(cnt1==cnt2) puts("Yes");
    else puts("No");
}
int main()
{
    int t=read();
    while(t--)
    {
        solve();
    }
    return 0;
}






原文地址:https://www.cnblogs.com/OvOq/p/14853067.html