【HDU 1045】Fire Net(DFS,check函数)

来源:互联网 发布:防盗网络应用 编辑:程序博客网 时间:2024/06/09 16:30

Fire Net


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.

12

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
4
.X..
….
XX..
….
2
XX
.X
3
.X.
X.X
.X.
3

.XX
.XX
4
….
….
….
….
0


Sample Output
5
1
5
2
4


题意:

有一个n*n城市(n<=4),地图已输入,”.”表示空地,可以安置武器,“#”表示墙,不可安置武器,武器也不可打穿它。武器只能直线射击。
现在需要在城市布防,此城有如下规定:

  • 两个武器不能在同一条直线上。

  • 若两个武器直线之间有墙,则可以安置。

现在要求出此城最多可以安置的武器数量。

解题思路:

  • 既然要求城中可放置的武器最大数量,易想到套用两层for循环遍历每一个点进行DFS求解。

  • 关键在于条件的处理。由于每一个安置武器的点都需要检查其所在行、列,故需要for循环遍历。若是墙 则break跳出循环,继续往其他方向遍历,若遇到了炮弹,不能填,返回false。

  • 每次求出一个炮弹数量值之后要记得回溯。

代码示例

#include<iostream>#include<cstring>using namespace std;char Map[5][5];bool vis[5][5];int n,ans;bool check(int x,int y){    if(x<1||y<1||x>n||y>n) return false;    else return true;}bool Recheck(int x,int y){    if(Map[x][y]=='X') return false;    //向右、左搜索     for(int i=x;check(i,y);++i)     {        if(Map[i][y]=='X') break;              //若是墙         else if(vis[i][y]==true) return false;  //若遇到了炮弹,不能填,返回false     }    for(int i=x;check(i,y);--i)     {        if(Map[i][y]=='X') break;        else if(vis[i][y]==true) return false;    }    //向下、上搜索     for(int i=y;check(x,i);++i)     {        if(Map[x][i]=='X') break;        else if(vis[x][i]==true) return false;    }    for(int i=y;check(x,i);--i)     {        if(Map[x][i]=='X') break;        else if(vis[x][i]==true) return false;    }    //若四周方向中,存在墙挡住炮弹或者四周都空旷,则可以填入炮弹,返回true     return true;}void DFS(int num){    if(num>ans) ans=num;    for(int i=1;i<=n;i++)       for(int j=1;j<=n;j++)           if(Recheck(i,j))    //检查函数的重要!!!!           {            vis[i][j]=true;//填入炮弹             DFS(num+1);            vis[i][j]=false;//回溯           }}int main(){    while(cin>>n&&n)    {        memset(vis,false,sizeof(vis));        ans=0;         for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)                cin>>Map[i][j];         DFS(0);        cout<<ans<<endl;    }    return 0;} 
原创粉丝点击