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

题目链接:

Problem - 1365D - CodeforcesCodeforces. Programming competitions and contests, programming communityicon-default.png?t=N7T8https://codeforces.com/problemset/problem/1365/D

题目大意:

有一张地图n行m列(地图外面全是墙),里面有好人(G),坏人(B),道路(.),墙(#),出口在n,m,人可以往上下左右走(不论好坏)。

你可以把道路变成墙,问你是否可以把所有的坏人封住,让所有的好人逃出?

思路:

地图有4种情况。但无所谓咱们又不是对4种情况去判断。

把坏人周围4格的道路封住,最后从终点去看所有的好人能不能到这里,有没有坏人能到这。

方法:

在读入图之后,遍历每一个点,遍历到坏人时,在周围4格尝试建墙。

最后用bfs查询:

bool bfs(){//寻找能出去的好人个数,有没有坏人能出去 
	ll sum=0;//能出去的好人个数 
	bool f=1;//因为只要有坏人出去就不行,直接用bool 
	memset(vis,0,sizeof vis);//每次清空 
	while(!q.empty()){
		ll x=q.front().first,y=q.front().second;//取点 
		q.pop();
		if(vis[x][y])continue;
		vis[x][y]=1;//标记已经来过 
		if(mp[x][y] == 'G')sum++;
		if(mp[x][y] == 'B')f=0;
		for(ll i = 0 ; i < 4 ; i ++){//查询下一个点 
			ll tx = x + dir[i][0];
			ll ty = y + dir[i][1];
			if(tx < 1 || ty < 1 || tx > n || ty > m || vis[tx][ty] || mp[tx][ty] == '#')continue;
			q.push({tx,ty});
		}
	}
	return sum == g && f;//好人全部能出去,没有坏人出去 
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"

const ll N = 1e2+7;
ll n,m,g;
char mp[N][N];//存图 
bool vis[N][N];//标记走过的路 
ll dir[4][2]={0,1,1,0,0,-1,-1,0};//方向数组 
queue<pair<ll,ll> >q;//接下来要走的地方 

bool bfs(){//寻找能出去的好人个数,有没有坏人能出去 
	ll sum=0;//能出去的好人个数 
	bool f=1;//因为只要有坏人出去就不行,直接用bool 
	memset(vis,0,sizeof vis);//每次清空 
	while(!q.empty()){
		ll x=q.front().first,y=q.front().second;//取点 
		q.pop();
		if(vis[x][y])continue;
		vis[x][y]=1;//标记已经来过 
		if(mp[x][y] == 'G')sum++;
		if(mp[x][y] == 'B')f=0;
		for(ll i = 0 ; i < 4 ; i ++){//查询下一个点 
			ll tx = x + dir[i][0];
			ll ty = y + dir[i][1];
			if(tx < 1 || ty < 1 || tx > n || ty > m || vis[tx][ty] || mp[tx][ty] == '#')continue;
			q.push({tx,ty});
		}
	}
	return sum == g && f;//好人全部能出去,没有坏人出去 
}

void solve(){
	cin >> n >> m;
	memset(mp,'#',sizeof mp);
	g=0;//好人的个数重置 
	for(ll i = 1 ; i <= n ; i ++)
		for(ll j = 1 ; j <= m ; j ++){
			cin >> mp[i][j];
			if(mp[i][j] == 'G')g++;
		}
	for(ll i = 1 ; i <= n ; i ++)
		for(ll j = 1 ; j <= m ; j ++)
			if(mp[i][j] == 'B')
				for(ll k = 0 ; k < 4 ; k ++){
					ll x = i + dir[k][0];
					ll y = j + dir[k][1];
					if(x < 1 || y < 1 || x > n || y > m || mp[x][y] != '.')continue;
					mp[x][y]='#';
				}
	if(mp[n][m] == '#'){//如果终点被封上了 
		g == 0 ? cout << "Yes" << endl : cout << "No" << endl;
		return;
	}
	q.push({n,m});
	bfs() ? cout << "Yes" << endl : cout << "No" << endl;
	return;
}

int main(){
	ll t=1;cin >> t;
	while(t --)solve();
	return 0;
}

相关推荐

  1. Codeforces Round 912 (Div. 2)

    2024-04-12 16:24:01       60 阅读
  2. Codeforces Round 924 (Div. 2)

    2024-04-12 16:24:01       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 16:24:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 16:24:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 16:24:01       87 阅读
  4. Python语言-面向对象

    2024-04-12 16:24:01       96 阅读

热门阅读

  1. SQL SERVER 备份

    2024-04-12 16:24:01       32 阅读
  2. stmmac_dvr_probe解析

    2024-04-12 16:24:01       39 阅读
  3. Go 之缓冲通道限制协程并发数目

    2024-04-12 16:24:01       34 阅读
  4. C语言什么是寄存器变量?如何实现?

    2024-04-12 16:24:01       39 阅读
  5. 36岁程序员,10年前错过了阿里,我一点都不后悔

    2024-04-12 16:24:01       38 阅读
  6. 【蓝桥杯日常】

    2024-04-12 16:24:01       36 阅读
  7. Composer安装与配置详解

    2024-04-12 16:24:01       35 阅读
  8. 蓝桥杯省B组复习(小白篇)

    2024-04-12 16:24:01       40 阅读
  9. C++ 的内存安全与效率

    2024-04-12 16:24:01       42 阅读
  10. 力扣经典150题第十八题:整数转罗马数字

    2024-04-12 16:24:01       45 阅读