Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12711 Accepted Submission(s): 7699
题目链接:点击打开链接
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.
![](https://i-blog.csdnimg.cn/blog_migrate/2833d81666db37f9dec4df3db9d38734.jpeg)
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/2833d81666db37f9dec4df3db9d38734.jpeg)
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
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
5
1
5
2
4
题意:
现在给你一张地图,‘X’ 代表墙,‘.’ 代表空地,现在要在这个地图上建立炮台,因为炮台是四个方向都有开口,子弹又很强大,可以摧毁一座炮台,但是墙是如此的坚固,可以阻挡子弹,所以如果该点要建立炮台,它所在的行和列都不能再有其他炮台,除非有墙阻隔,如题目中的前三个图都是合法建立炮台的,后两幅图是非法的。
分析:
两个函数,book 是为了判断该点是否可以建炮台,dfs 是搜索每一个点,然后代码有注释,可以看得明白。
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;
int n,sum;
char s[5][5];///存储地图
int book(int m,int n)///该函数用于判断该点是否可以建立炮台
{
int bj=1;
///只需要遍历一点该点的上方和左方,因为我们是按图中的坐标从上到下,从左到右的顺序搜索的
for(int i=m;i>=0;i--)
{
if(s[i][n]=='X')
break;
if(s[i][n]=='*')
{
bj=0;
break;
}
}
for(int i=n;i>=0;i--)
{
if(s[m][i]=='X')
break;
if(s[m][i]=='*')
{
bj=0;
break;
}
}
return bj;
}
void dfs(int a,int b)
{
int x=a/n;///一种坐标的表示方法,注意存图的时候下标是从 0 开始的
int y=a%n;
if(a==n*n)///如果搜索到最后一个位置,则比较一下本次建立的炮台数是否比上次多
{
if(b>sum)
sum=b;
return ;
}
else
{
if(s[x][y]=='.'&&book(x,y))///如果该点是空地并且该点所在的行和列没有建炮台或有墙
{
s[x][y]='*';///该标记为在该点建立炮台
dfs(a+1,b+1);
s[x][y]='.';
}
dfs(a+1,b);
}
}
int main()
{
while(~scanf("%d",&n)&&n)
{
getchar();///吞掉换行符
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%c",&s[i][j]);
}
getchar();///吞掉输入地图过程中的换行符
}
sum=0;///统计在一张地图中最多可以建立的炮台数
dfs(0,0);
printf("%d\n",sum);
}
return 0;
}