CF1365D Solve The Maze (BFS)

简介: CF1365D Solve The Maze (BFS)

原题链接

题意:

20200401134307494.png思路:

考虑最为保险的放法,就是将每个坏人的四周都放墙,这样坏人一定无法到达终点,但是会对好人的移动产生影响,有可能会造成好人也无法到达。只需要检验一下这种构造之后有多少个好人能够到达终点即可。

好人有多个,终点却只有一个。可以从终点倒着跑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;
}
目录
相关文章
|
8月前
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
35 0
codeforces1244——D.Paint the Tree(dfs)
codeforces1244——D.Paint the Tree(dfs)
56 0
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
[LeetCode]--73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution us
959 0
[LeetCode]--74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer
1142 0
[LeetCode]--71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, =&gt; “/home” path = “/a/./b/../../c/”, =&gt; “/c” click to show corner cases. Corner Cases:
1035 0