B - Fire Net

Subject:

Supposethat we have a square city with straight streets. A map of a city is a squareboard 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 throughwhich 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 runacross any distance and destroy a blockhouse on its way. On the other hand, awall is so strongly built that can stop the bullets. 

The goal is to place as many blockhouses in a city aspossible so that no two can destroy each other. A configuration of blockhousesis legal provided that no two blockhouses are on the same horizontal row orvertical column in a map unless there is at least one wall separating them. Inthis problem we will consider small square cities (at most 4x4) that containwalls 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 legalconfigurations, and the fourth and fifth pictures show illegal configurations.For this board, the maximum number of blockhouses in a legal configuration is5; 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 amap, calculates the maximum number of blockhouses that can be placed in thecity 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 isthe size of the city; n will be at most 4. The next n lines each describe onerow 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 themaximum number of blockhouses that can be placed in the city in a legalconfiguration. 

*****************************************************************************************************

Sample Input
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
5
1
5
2
4

 *****************************************************************************************************

思路,解题重要部分:

 主要采用DFS算法,递归的思想。

     你在寻找的时候,只需要看之前的,后面的DFS未改变,不需要去判断。

也就是你判断他是不是可以安炮台,不需要看下面和左边,只需要看右边和上面。因为那两边就是没有改变过,除了是墙不能安以外,就不要判断是不是那两边有炮台会造成影响。

*****************************************************************************************************

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

char map[10][10];

int n;
int ans=0;

//判断是不是可以放
bool cmp(int x,int y)
{
	for(int i=x-1;i>=1;i--)
	{
		if(map[i][y]=='#')
		   return false;
		if(map[i][y]=='X')
		   break;
	}
	for(int i=y-1;i>=1;i--)
	{
		if(map[x][i]=='#')
		   return false;
		if(map[x][i]=='X')
		   break;
	}
	return true;
}

//递归寻找
int shu(int x,int y,int z)
{
    if(y==n+1){
    	x++;
		y=1;
    }
	if(x==n+1 && y==1)
	{
		//保存最大值。 
		if(ans < z)
		  ans = z;
		return z;
	}
    if(map[x][y]=='.' && cmp(x,y))
    {
    	map[x][y]='#';
    	shu(x,y+1,z+1);
    	map[x][y]= '.';
    }
    shu(x,y+1,z);
}

int main()
{
	while(scanf("%d",&n),n)
	{
	   ans=0;
	   for(int i=1;i<=n;i++)
	   {
	   	for(int j=1;j<=n;j++)
	   	{
	   		cin>>map[i][j];
	   	}
	   }
	   shu(1,1,0);
	   printf("%d\n",ans);
	}
   return 0;
}
*****************************************************************************************************

posted @ 2017-07-11 20:08  让你一生残梦  阅读(89)  评论(0编辑  收藏  举报