D. Solve The Maze Codeforces Round #648 (Div. 2)

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

Example
inputCopy
6
1 1
.
1 2
G.
2 2
#B
G.
2 3
G.#
B#.
3 3
#B.
#…
GG.
2 2
#B
B.
outputCopy
Yes
Yes
No
No
Yes
Yes
Note
For the first and second test cases, all conditions are already satisfied.

For the third test case, there is only one empty cell (2,2), and if it is replaced with a wall then the good person at (1,2) will not be able to escape.

For the fourth test case, the good person at (1,1) cannot escape.

For the fifth test case, Vivek can block the cells (2,3) and (2,2).

For the last test case, Vivek can block the destination cell (2,2).

先把坏人堵住,然后再BFS搜就行了
注意:不要把坏人旁边的坏人变成墙,不然会漏写。我一开始就wa了4发这个问题。。。。。

#include <iostream>
#include <algorithm>
#include <set>
#include <cstring>
#include <queue>
using namespace std;
#define int long long
char ch[55][55];
char g[55][55];
int f1[] = {0, 0, 1, -1};
int f2[] = {1, -1, 0, 0};
int cou = 0;
int n, m;
bool pan[55][55];
struct node
{int x, y;
};
void BFS(int a, int b)
{queue<node> cun;if (ch[a][b]=='#') return;cun.push({a, b});pan[a][b] = true;if (ch[a][b] == 'G')cou++;while (!cun.empty()){int x = cun.front().x;int y = cun.front().y;cun.pop();for (int i = 0; i < 4; i++){int xx = x + f1[i];int yy = y + f2[i];if (xx <= n && yy <= m && xx >= 1 && yy >= 1 && pan[xx][yy] == false){if (ch[xx][yy] != '#'){if (ch[xx][yy] == 'G')cou++;cun.push({xx, yy});pan[xx][yy] = true;}}}}
}
signed main()
{int t;cin >> t;while (t--){cou = 0;int hh = 0;memset(ch, '#', sizeof(ch));memset(pan, false, sizeof(pan));cin >> n >> m;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cin >> ch[i][j];if (ch[i][j] == 'G')hh++;}}bool flag = true;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (ch[i][j] == 'B'){for (int h = 0; h < 4; h++){int x = i + f1[h];int y = j + f2[h];if (ch[x][y]!='B') ch[x][y] = '#';//这里注意}}}}if (hh == 0){cout << "YES" << endl;}else{BFS(n, m);//   cout<<cou<<" "<<hh<<endl;if (cou == hh)cout << "YES" << endl;elsecout << "NO" << endl;}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428742.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android之View绘制流程开胃菜---setContentView(...)详细分析

版权声明&#xff1a;本文出自汪磊的博客&#xff0c;转载请务必注明出处。 1 为什么要分析setContentView方法 作为安卓开发者相信大部分都有意或者无意看过如下图示&#xff1a;PhoneWindow,DecorView这些究竟都是些神马玩意&#xff1f;图示的层级关系是怎么来的&#xff1f…

Hibernate查询方式

Hibernate查询方式 1 OID查询 &#xff08;1&#xff09;根据id查询某一条记录&#xff0c;返回对象 2 对象导航查询 &#xff08;1&#xff09;根据id查询某个公司&#xff0c;再查询这个公司里面所有的员工 Company csession.get(Company.class, 1);Set<Worker> set …

Harbour.Space Scholarship Contest 2021-2022 (open for everyone, rated, Div. 1 + Div. 2)(A - D)

A 太简单了&#xff0c;写完就删了B 有很多人暴力搜&#xff0c;或者其他方法&#xff0c;样例不严谨过了&#xff0c;然后就被hack了 #include<bits/stdc.h> #define int long long using namespace std; signed main() {int t;cin>>t;while (t--){string str,s…

Codeforces Round #734 (Div. 3) (A-D1)

A 太简单了&#xff0c;写完就删了B1 #include <bits/stdc.h> using namespace std; #define int long long signed main() {int t;cin >> t;while (t--){int ch[33];memset(ch, 0, sizeof(ch));string str;cin >> str;for (int i 0; i < str.length()…

排序算法之堆排序

一、什么是堆 如果一个完全二叉树的每个节点&#xff0c;都不大于它的子节点&#xff0c;就可以称之为堆。所谓完全二叉树&#xff0c;就是除了叶子节点以外&#xff0c;所有的其他节点&#xff0c;都有完整的左字树和右子树&#xff0c;除了最后一层的非叶子节点以外。 二、堆…

Codeforces Global Round 15 (A-D)没有C

A 太简单&#xff0c;写完删了B 我写的繁了&#xff0c;但是意思是这么个意思&#xff0c;就遍历打擂台一样&#xff0c;找到最后的胜者&#xff0c;然后在遍历一遍找是不是最厉害的 #include <bits/stdc.h> using namespace std; #define int long long int op[200095…

Codeforces Round #434 (Div. 2)【A、B、C、D】

Codeforces Round #434 (Div. 2) codeforces 858A. k-rounding【水】 题意&#xff1a;已知n和k&#xff0c;求n的最小倍数x&#xff0c;要求x后缀至少有k个0。 题解&#xff1a;答案就是10^k和n的最小公倍数。 1 #include<cstdio>2 #include<cstring>3 #include&l…

Codeforces Round #735 (Div. 2)(A-D)没有B

Codeforces Round #735 (Div. 2) A 一道小思维题 #include <bits/stdc.h> using namespace std; #define int long long const int N 1000100; int a[N]; signed main() {int t;cin>>t;while(t--){int n;cin>>n;int cou 0;for (int i1;i<n;i){cin>&…

Educational Codeforces Round 112 (Rated for Div. 2)(A-D)

Educational Codeforces Round 112 (Rated for Div. 2) A 我写的挺烦的&#xff0c;其实判断一下奇偶数和有没有a>0就行 #include <bits/stdc.h>using namespace std;#define int long longsigned main(){int t;cin>>t;while (t--){int n;cin>>n;int a n…

PLC与触摸屏练习

电机正反转 触摸屏 要用触摸屏进行仿真是不要忘了先启动梯形图测试 效果 1-2例 行程开关控制的自动循环 梯形图 触摸屏 下面只是用了四个开关&#xff0c;可以根据自己想要实现的按照梯形图添加 题目 梯形图 电动机星三角减压启动控制 题目及要求 触摸屏截图 运算 把显示的数值…

Django里面是文件静态化的方法

看Django官网的时候&#xff0c;由于自己的英语基础较差&#xff0c;而实现的谷歌翻译比较烂&#xff0c;只能看懂个大概。在文件静态化的时候&#xff0c;讲的比较繁琐一点&#xff0c;没怎么看懂&#xff0c;遂询问了一下其他人&#xff0c;明白了许多&#xff0c;但是细节需…

Codeforces Round #736 (Div. 2)(B-C)

Codeforces Round #736 (Div. 2) 花了十分钟帮朋友写了B&#xff0c;C就睡觉了&#xff0c;自己没打 B 先看上&#xff0c;再看左后看右 #include <iostream> #include <cstring> #include <algorithm> using namespace std; #define int long long signed…

RabbitMQ 声明Queue时的参数们的Power

RabbitMQ 声明Queue时的参数们的Power 参数们的Power 在声明队列的时候会有很多的参数 public static QueueDeclareOk QueueDeclare(this IModel model, string queue "", bool durable false, bool exclusive true, bool autoDelete true, IDictionary<strin…

解决Firefox已阻止运行早期版本Adobe Flash

解决Firefox已阻止运行早期版本Adobe Flash 类别 [随笔分类]web 解决Firefox已阻止运行早期版本Adobe Flash 最近火狐浏览器不知抽什么风&#xff0c;每次打开总提示"Firefox已阻止(null)运行早期版本的Adobe Flash"。要命的是它提示的解决办法根本不管用&#xf…

误删path怎么办(已重启)

我是在设置环境变量的时候不懂&#xff0c;新建了一个path&#xff0c;导致我原来的path没了。 但是居然jdk能用。。。。。不太懂。 不要慌&#xff0c;大不了重装系统。其实只要再重新新建就行了。 C:\Program Files\Common Files\Siemens\Automation\Simatic OAM\bin;%Syste…

一个容易被忽视的css选择器

之前学的的迷糊了&#xff0c;也不知道什么会什么不会了&#xff0c;跑去面试了。别人列出一堆css选择器&#xff0c;本以为选择器没啥的&#xff0c;结果到那个多类选择器翻车了&#xff0c;.a.b选择同时含a,b类名的&#xff0c;很尴尬所以回来仔细整理了一下。目前根据W3C手册…

Codeforces Round #756 (Div. 3)

Codeforces Round #756 (Div. 3) A. Make Even 思路&#xff1a;如果末尾是偶数&#xff0c;不需要操作&#xff1b;如果开头是偶数&#xff0c;一次操作&#xff0c;即全翻转&#xff1b;如果开头和末尾都是 奇数&#xff0c;判断里面是否有偶数&#xff0c;如果没有&#xff…

使用MyBatista----上传图像

使用MyBatis上传图像&#xff0c;使用的是Oracle的数据库表&#xff0c;有一个TEACHER表&#xff0c;有7列&#xff0c;有1列是存储图片的&#xff0c;类型用BLOB&#xff0c;最大容量是4G&#xff0c;以二进制的形式写入数据库表。 建立这个表的对应实体类Teacher&#xff0c;…

189A. Cut Ribbon

A. Cut Ribbon:题目地址 题意&#xff1a;一条长为n的彩带切割&#xff0c;切割后每段长度是a或者是b或者是c#include <bits/stdc.h> using namespace std; typedef long long ll; vector<int> a((int)4e5); vector<int> b((int)4e5); int main() {int n,a,…

作业1.3

public class Work{   public static void main(String[] args)   {     System.out.println(" J A V V A");     System.out.println(" J A A V V A A"); //直接输出就好了     System.out.println("J J …