Codeforces648div2D

Codeforces648div2D

原题:

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).

思路:先把坏人四周堵起来,如果四周有好人,直接输出no;然后再从终点bfs好人,看能否搜到所有好人。注意坏人相邻的情况,否则会WA4

ACcodes

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
int n,m;
char ch[55][55];
int g;
typedef pair<int,int> P;
bool cmp(int a, int b)
{
	return a>b;
}
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void bfs(int i, int j)
{
	if(i<0||i>=n||j<0||j>=m||ch[i][j]=='#')
		return;
	if(ch[i][j]=='G')
	{
		g--;
		ch[i][j]='.';
	}
	for(int x=0;x<4;x++)
	{
		int x0 = i+dx[x];
		int y0 = j+dy[x];
		bfs(x0,y0);
	}
}
int main()
{
	int test=1;
	scanf("%d",&test);
	while(test--) {
		g=0;
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>ch[i][j];
				if(ch[i][j]=='G')
					g++;
			}
		}
		bool flag=true;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(ch[i][j]=='B')
				{
					for(int k=0;k<4;k++)
					{
						int x0 = i+dx[k];
						int y0 = j+dy[k];
						if(x0<0||x0>=n||y0<0||y0>=m||ch[x0][y0]=='B')
							continue;
						if(ch[x0][y0]=='G')
						{
							flag = false;
							break;
						}
						else
							ch[x0][y0] = '#';
					}
				}
			}
		}
		if(g==0)
		{
			cout << "YES" << endl;
			continue;
		}
		if(!flag || ch[n-1][m-1]=='#')
		{
			cout << "NO" << endl;
			continue;
		}
		queue<P> q;q.push(P(n-1,m-1));
		ch[n-1][m-1]='#';
		while(q.size())
		{
			P cnt = q.front();
			q.pop();
			for(int i=0;i<4;i++)
			{
				int x0 = dx[i]+cnt.first;
				int y0 = dy[i]+cnt.second;
				if(x0>=0&&x0<n&&y0>=0&&y0<m&&ch[x0][y0]!='#')
				{
					if(ch[x0][y0]=='G')
						g--;
					q.push(P(x0,y0));
					ch[x0][y0]='#';
				}
			}
		}
		if(g!=0)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值