杭电oj1045:Fire Net(深度优先搜索dfs)

Fire Net

题目链接

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18352 Accepted Submission(s): 11191

Problem Description

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.

Input

The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a ‘.’ indicating an open space and an uppercase ‘X’ indicating a wall. There are no spaces in the input file.

Output

For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.

Sample Input
在这里插入图片描述
Sample Output

5
1
5
2
4

一开始的思路特别简单,就是遍历整个map,挨个试,如果没有问题,就放置blockhouse。遍历完整个map,答案也就出来了。

#include<iostream>
using namespace std;

const int maxn = 10000;//题目的数据有点问题,需要把maxn设置的大一点

int n;
char map[maxn][maxn];

//判断放置在(x,y)处是否合法?
//x,y从下表0开始
bool isLegal(int x, int y)
{
	int flag_x = 0, flag_y = 0;
	//map[i][j] == 'o'表示已经在i,j处放置了blockhouse。
	for(int i=0;i<n;i++)
	{
		//判断y一整列是否合法(上下判断)
		if(map[i][y] == 'o' && flag_x == 1)
			return false;
		if(map[i][y] == 'o')
			flag_x = 1;
		else if(map[i][y] == 'X')
			flag_x = 0;
		
		//判断x一整列是否合法(左右判断)
		if(map[x][i] == 'o' && flag_y == 1)
			return false;
		if(map[x][i] == 'o')
			flag_y = 1;
		else if(map[x][i] == 'X')
			flag_y = 0;
	}
	return true;
}

int main()
{
	//freopen("in.txt","r", stdin);
	while(cin>>n, n!=0)
	{
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				cin>>map[i][j];	
		
		int ans = 0;
		//遍历整个map
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
			{
				if(map[i][j] == '.')
				{
					map[i][j] = 'o';//先在(i,j)处放置blockhouse
					if(isLegal(i, j))//判断是否合法
						ans++;
					else
						map[i][j] = '.';//不合法,还原为原始状态
				}
			}
			
		cout<<ans<<endl;//输出结果
	}
	return 0;	
}

结果,在进行样例测试的时候,出现了错误,比如说下面这种情况:
3

.XX
.XX
错误原因:首先会在(0,0)处放置一个blockhouse,并且判断合法。以至于导致后面在任何一个位置放置blockhouse都会被判断为非法。结果为1个。事实上,可以在(0,1),(1,0)或者(0,1),(2,0)或者(0,2),(1,0)或者(0,2),(2,0)处分别放置blockhouse,正确结果为2个。

知道了错误原因之后,立马想到了利用DFS中自动回溯的方法来解决这个问题,下边的代码是AC的。

#include<iostream>
using namespace std;

const int maxn = 10000;//题目的数据有点问题,需要把maxn设置的大一点

int n;
int maxAns = 0;
char map[maxn][maxn];
//place[i][j]==false表示在(i,j)处还没有放置blockhouse。
//place[i][j]==true表示在(i,j)处放置了blockhouse。
bool place[maxn][maxn];

//判断放置在(x,y)处是否合法?
//x,y从下表0开始
bool isLegal(int x, int y)
{
	int flag_x = 0, flag_y = 0;
	//map[i][j] == 'o'表示已经在i,j处放置了blockhouse。
	for(int i=0;i<n;i++)
	{
		//判断y一整列是否合法(上下判断)
		if(map[i][y] == 'o' && flag_x == 1)
			return false;
		if(map[i][y] == 'o')
			flag_x = 1;
		else if(map[i][y] == 'X')
			flag_x = 0;
		
		//判断x一整列是否合法(左右判断)
		if(map[x][i] == 'o' && flag_y == 1)
			return false;
		if(map[x][i] == 'o')
			flag_y = 1;
		else if(map[x][i] == 'X')
			flag_y = 0;
	}
	return true;
}

void dfs(int x, int y, int ans)
{
//	if(x == n-1 && y == n-1)//写不写无所谓 
//		return;
	
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(!place[i][j] && map[i][j] == '.')
			{ 
				map[i][j] = 'o';//先暂且在(i,j)处放置blockhouse。
				if(isLegal(i, j))//判断是否合法
				{
					if(ans + 1 > maxAns)
					//判断是否要更新放置的最大数目
						maxAns = ans+1;
					place[i][j] = true;
					dfs(i, j, ans + 1);
				}
				//回溯,即还原为初始状态
				map[i][j] = '.';
				place[i][j] = false;
			}
}

int main()
{
	freopen("in.txt","r", stdin);
	while(cin>>n, n!=0)
	{
		int flag = 0, x, y;//x,y记录第一个'.'的位置,用于dfs的搜索
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
			{
				cin>>map[i][j];	
				place[i][j] = false;
				if(flag == 0 && map[i][j] == '.')
				{//记录第一个'.'的位置
					x = i; y = j;
					flag = 1;
				}
			}
		
		dfs(x, y, 0);
		cout<<maxAns<<endl;
		maxAns = 0;
	}
	return 0;	
}

注意:如果DFS写成下边的形式,是错误的,请读者自己先思考错误原因。

void dfs(int x, int y, int ans)
{
/******************变动位置***********************/
	if(x == n-1 && y == n-1)
	{	
		if(ans > maxAns)
			//判断是否要更新放置的最大数目
			maxAns = ans;
		return;
	}
/***********************************************/

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(!place[i][j] && map[i][j] == '.')
			{ 
				map[i][j] = 'o';//先暂且在(i,j)处放置blockhouse。
				if(isLegal(i, j))//判断是否合法
				{
					place[i][j] = true;
					dfs(i, j, ans + 1);
				}
				//回溯,即还原为初始状态
				map[i][j] = '.';
				place[i][j] = false;
			}
}

其实错误原因也不难想,我举个栗子大家就一清二楚了:
2
XX
.X
在这个栗子中,当遍历到(1,1)也就是最后一个位置的时候,if(!place[i][j] && map[i][j] == '.')判断条件是不成立的,也就是说dfs函数永远不能执行if(x == n-1 && y == n-1){}中的代码(遍历完整个map就自行结束了),导致结果输出0。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值