D. Solve The Maze(BFS+思維)

                        D. Solve The Maze

Vivek has encountered a problem. He has a maze that can be represented as an n×m grid. Each of the grid cells may represent the following:
Empty — ‘.’
Wall — ‘#’
Good person — ‘G’
Bad person — ‘B’
The only escape from the maze is at cell (n,m).

A person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains ‘G’ or ‘B’ cannot be blocked and can be travelled through.

Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions.

It is guaranteed that the cell (n,m) is empty. Vivek can also block this cell.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n, m (1≤n,m≤50) — the number of rows and columns in the maze.

Each of the next n lines contain m characters. They describe the layout of the maze. If a character on a line equals ‘.’, the corresponding cell is empty. If it equals ‘#’, the cell has a wall. ‘G’ corresponds to a good person and ‘B’ corresponds to a bad person.

Output
For each test case, print “Yes” if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print “No”

You may print every letter in any case (upper or lower).

題意:就是有一個n*m的迷宮,裏面有若干個好人(用G表示)以及若干個壞人(用B)表示,還有若干個牆(用#表示),牆的方塊不能行走,人只能往上下左右四個方向行走,可以在某些空白位置設置一些牆,使得壞人到達不了點(n,m),好人能夠全部到達(n,m)。

思路:這是div2的D題,已經稍微有點難度了,實際上仔細想一下也沒那麼難,我們就考慮在壞人的四周圍上牆,然後要是有好人在壞人的四周,那麼直接就輸出NO,因爲無論好人怎麼走,壞人都可以跟着好人走下去。然後在把所有的壞人都圍上之後,就可以bfs遍歷,但是我們從每個好人開始遍歷時間複雜度又過高,所以我們就從終點開始bfs搜索,看是否能遍歷到所有的好人。

下面是AC代碼:

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define pb push_back
using namespace std;
const int maxn=1e2+100;
int vis[maxn][maxn];
int dix[4]={1,-1,0,0};
int diy[4]={0,0,1,-1};
char a[maxn][maxn];
struct node{
    int x,y;
};
int t,n,m,flag=1;
void build(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int dx=x+dix[i];
        int dy=y+diy[i];
        if(dx>=1&&dx<=n&&dy>=1&&dy<=m)
        {
         if(a[dx][dy]=='.')a[dx][dy]='#';
         if(a[dx][dy]=='G')flag=0;
        }
    }
}
void bfs(int s,int d)
{
     queue<node>q;
     q.push((node){s,d});
     memset(vis,0,sizeof vis);
     vis[s][d]=1;
     while(!q.empty())
     {
         node u=q.front();
         q.pop();
         for(int i=0;i<4;i++)
         {
             int dx=u.x+dix[i];
             int dy=u.y+diy[i];
             if(dx<1||dx>n||dy<1||dy>m||a[dx][dy]=='#'||vis[dx][dy])continue;
             vis[dx][dy]=1;

             q.push(node{dx,dy});
         }

     }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        flag=1;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        cin>>a[i][j];

       for(int i=1;i<=n;i++)
       for(int j=1;j<=m;j++)
       {
           if(a[i][j]=='B')build(i,j);
       }
       memset(vis,0,sizeof vis);

       if(a[n][m]!='#')bfs(n,m);

       for(int i=1;i<=n;i++)
       for(int j=1;j<=m;j++)
       {
           if(a[i][j]=='G')
           {
               if(vis[i][j])continue;
               else flag=0;
           }
       }
       if(flag)cout<<"YES"<<endl;
       else cout<<"NO"<<endl;
    }
    //system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章