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
#include
#include
#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;
}

你可能感兴趣的:(codeforce水题)