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.
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
题目大意:在 ‘.’位置可以放置 大炮,这些大炮不能在同一行 同一列除非 中间有‘X' 隔开
叫你求最多可以放置多少炮台
思路: 就是在主函数main中对 每一个位置进循环,对这个位置进行深搜,如果满足条件的话,那么在这一个位置放上“P”并且step++
#include<iostream>// http://blog.csdn.net/riba2534/article/details/54231824
using namespace std;
char map[15][15];
int n,maxx;
int judge(int x,int y)//判断这一点能否防止炮台 能防止的话返回 1 不能的话返回 0
{
for(int j=y;j>=0;j--)//判断列
{
if(map[x][j]=='X') break;
if(map[x][j]=='P') return 0;
}
for(int i=x;i>=0;i--)//判断行
{
if(map[i][y]=='X') break;
if(map[i][y]=='P') return 0;
}
return 1;// 如果两次都是 break 所以说这一行 这一列都是有墙的 所有这一点 是 一定可以放置的
// 还有一种情况 i,j 都没有大炮 所以当然是可以放置大炮的
}
void dfs(int num,int step)
{
if(num==n*n)
{
if(step>maxx)//更新最大值
maxx=step;
return ;
}
int x=num%n;
int y=num/n;
if(map[x][y]=='.'&&judge(x,y))//判断改点非墙 并且可以放置大炮
{
map[x][y]='P';//如果说这点可以的话 放置大炮
dfs(num+1,step+1);
map[x][y]='.';
}
dfs(num+1,step);//这点不放置大炮 往下回溯
}
int main()
{
while(cin>>n&&n)
{
maxx=0;//最大值每次案例都要初始化
for(int i=0;i<n;i++)
cin>>map[i]; //读取地图
dfs(0,0);//从第一个点开始搜索
cout<<maxx<<endl;
}
return 0;
}