当前位置:网站首页>Codeforces Round #648 (Div. 2) D. Solve The Maze
Codeforces Round #648 (Div. 2) D. Solve The Maze
2022-07-05 08:51:00 【Qizi K】
Codeforces Round #648 (Div. 2) D. Solve The Maze
label :BFS.
The question : Give me a maze ,‘#’ Represents a wall ,‘G’ Show good people ,’B‘ It means bad people , End at (n,m). Ask if you can add a wall to the open space , So that all good people can go to the end , All bad guys can't get to the end .
tips:
A. greedy , Consider adding walls where you can add walls around all the bad guys ( Surround the bad guys ). If a bad man is next to a good man , Obviously NO( Because where good people can go, bad people can go qwq).
B. If there are no good people , Direct output YES( You can directly block the end ).【 But note that you can't output directly without bad people YES, Because good people may be blocked by walls 】.
C. Don't run to every good person BFS, This complexity is too high . Because if every good person can finally reach the end , Then run backwards from the end BFS, The number of good people that can pass should be the total number of good people . once BFS that will do .
#include<bits/stdc++.h>
#define m_p make_pair
using namespace std;
int n,m,t,cntgood;
int dir[4][2] = {
{
0,1},{
0,-1},{
1,0},{
-1,0}};
vector<pair<int, int> > bad;
vector<pair<int, int> > :: iterator it;
char mp[55][55];
void init(){
while(!bad.empty()) bad.pop_back();
cntgood = 0;
}
int bfs(){
if(mp[n][m] == '#') return 0;
int cnt = 0;
bool vis[55][55] = {
false};
queue<pair<int, int> >q;
q.push(m_p(n, m));
while(!q.empty()){
int xx = q.front().first, yy = q.front().second; q.pop();
if(vis[xx][yy]) continue;
vis[xx][yy] = true;
if(mp[xx][yy] == 'G') cnt++;
for(int k = 0; k < 4; ++k){
int tx = xx + dir[k][0] , ty = yy + dir[k][1];
if(tx < 1 || tx > n || ty < 1 || ty > m || mp[tx][ty] == '#') continue;
q.push(m_p(tx, ty));
}
}
return cnt;
}
void solve(){
init();
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; ++i){
getchar();
for(int j = 1; j <= m; ++j){
scanf("%c",&mp[i][j]);
if(mp[i][j] == 'B') bad.push_back(m_p(i,j));
if(mp[i][j] == 'G') cntgood++;
}
}
if(!cntgood) printf("Yes\n");
else{
for(it = bad.begin(); it != bad.end(); ++it){
int x = it->first, y = it->second;
for(int k = 0; k < 4; ++k){
int tx = x + dir[k][0] , ty = y + dir[k][1];
if(tx < 1 || tx > n || ty < 1 || ty > m) continue;
if(mp[tx][ty] == 'G'){
printf("No\n");
return ;
}else if(mp[tx][ty] == '.') mp[tx][ty] = '#';
}
}
(bfs() == cntgood) ? printf("Yes\n") : printf("No\n");
}
}
int main(){
scanf("%d",&t);
while(t--)
solve();
return 0;
}
/*bool bfs(int x, int y){ // Don't do that bfs, Run for everyone bfs Too slow bool vis[55][55] = {false}; queue<pair<int, int> >q; q.push(m_p(x, y)); while(!q.empty()){ int xx = q.front().first, yy = q.front().second; q.pop(); if(xx == n && yy == m) return true; if(vis[xx][yy]) continue; vis[xx][yy] = true; for(int k = 0; k < 4; ++k){ int tx = xx + dir[k][0] , ty = yy + dir[k][1]; if(tx < 1 || tx > n || ty < 1 || ty > m || mp[tx][ty] == '#') continue; q.push(m_p(tx, ty)); } } return false; }*/
边栏推荐
- Solutions of ordinary differential equations (2) examples
- Meta tag details
- Yolov4 target detection backbone
- 696. 计数二进制子串
- Adaboost使用
- 【日常训练】1200. 最小绝对差
- Programming implementation of ROS learning 6 -service node
- 资源变现小程序添加折扣充值和折扣影票插件
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- Basic number theory - fast power
猜你喜欢

TF coordinate transformation of common components of ros-9 ROS

TypeScript手把手教程,简单易懂

Halcon Chinese character recognition

猜谜语啦(6)

Halcon snap, get the area and position of coins

Guess riddles (4)

Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)

Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc

猜谜语啦(142)
![[牛客网刷题 Day4] JZ55 二叉树的深度](/img/f7/ca8ad43b8d9bf13df949b2f00f6d6c.png)
[牛客网刷题 Day4] JZ55 二叉树的深度
随机推荐
Redis实现高性能的全文搜索引擎---RediSearch
[daiy4] copy of JZ35 complex linked list
Halcon: check of blob analysis_ Blister capsule detection
Guess riddles (5)
c#比较两张图像的差异
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
TypeScript手把手教程,简单易懂
Count of C # LINQ source code analysis
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Basic number theory - fast power
Array,Date,String 对象方法
猜谜语啦(9)
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
轮子1:QCustomPlot初始化模板
C语言标准函数scanf不安全的原因
MPSoC QSPI Flash 升级办法
Halcon blob analysis (ball.hdev)
皮尔森相关系数
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
【日常训练--腾讯精选50】557. 反转字符串中的单词 III