三、搜索和二分 [Cloned V - 搜索

本文介绍了一个关于在带有墙壁的城镇中放置碉堡的算法问题,目标是在确保任意两个碉堡不会互相射击的前提下,计算并实现放置最多的碉堡数量。通过深度优先搜索策略,文章提供了一种解决方案,并详细解释了如何遍历地图、检查放置条件以及更新最大数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. 

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening. 

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets. 

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through. 

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways. 



Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration. 

题意:

在一个由方格组成的城镇中有一些墙,需要在空着的地方放一些碉堡,这些碉堡不能直接面对,也就是在同一列或同一行而且中间没有墙隔着,这样两个碉堡就会误伤,但是墙无比坚固所以可以阻碍攻击。给出这个城镇的墙的分布,问最多能够放多少个碉堡。

题解:

这个深搜难度就要比前边几个稍大一点,说白了是因为第一个碉堡不一定放在哪里,也就是起点不确定,所以要全部遍历一遍将从每个点开始深搜找出能放的碉堡的数量的最大值,但是在一次搜索中放过碉堡的地方就不必再次搜索。同时还要注意墙的问题,判断这个格子能否放碉堡的判据是不越界,不是墙,横竖两个方向不会碰到碉堡,也就是搜索横竖两个方向要么会搜出界,要么会搜到墙然后停止,如果搜到碉堡那这个位置则不能放。

代码:AC

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char map[5][5];
char visit[5][5];
int d[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n;
int sum;
void DFS(int ans)
{
	  int flag;
	  int i,j,k;
	  for(i=0;i<n;i++)
	  {
		  for(j=0;j<n;j++)
		  {
			  if(map[i][j]=='.')
			  {
				  flag=0;
				  for(k=0;k<4;k++)
				  {
					  int dx=i+d[k][0];
					  int dy=j+d[k][1];
					  while(1)
					  {
						  if(dx<0||dy<0||dx>=n||dy>=n||map[dx][dy]=='X')
							  break;
						  if(map[dx][dy]=='1')
							  flag=1;
						  dx+=d[k][0];
						  dy+=d[k][1];
					  }
				  }
				  if(!flag)
				  {
					  map[i][j]='1';
					  DFS(ans+1);
					  map[i][j]='.';
				  }
			  }
		  }
	  }
	  if(sum<ans)
		  sum=ans;
}
int main()
{
	while(cin>>n&&n)
	{
		int i,j;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				cin>>map[i][j];
		sum=0;
		DFS(0);
		cout<<sum<<endl;
	}
	return 0;
}
	

 

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、COSO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析模拟,揭示了生物质炉具在实际应用中的优点挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值