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.
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.
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
Fire Net
1#include<iostream>
2using namespace std;
3
4char map[4][4];//地图
5int n,maxsum;
6
7bool canput(int row,int col)//判断这点是否可以放置堡垒
8{
9 int i,j;
10 //测试是否可以放碉堡,就是是否有危险
11
12 //先测试col列方向上的
13
14 for(i=row-1;i>=0;--i)
15 {
16 if(map[i][col]=='O')//如果这列上有碉堡
17 return false;
18 if(map[i][col]=='X') break;//如果先遇到的是墙即可以放
19
20 }
21 //从row行方向上的
22 for(j=col-1;j>=0;--j)
23 {
24 if(map[row][j]=='O')
25 return false;
26 if(map[row][j]=='X')
27 break;
28
29 }
30 return true;
31
32
33}
34
35void dfs(int k,int num)//深搜的关键,行向走的,先遍历完一行再遍历一行
36{
37 int x,y;//代表行号和列号
38 if(k==n*n)//终止条件
39 {
40 if(maxsum<num)
41 {
42 maxsum=num;
43 return ;
44 }
45 }
46 else
47 {
48 x=k/n;//行号
49 y=k%n;//列号
50
51 //满足条件可以放堡垒
52 if((map[x][y]=='.')&&(canput(x,y)==true))
53 {
54 //当前点是空白处,并且可以放置碉堡
55 map[x][y]='O';//代替了visit[x][y]==1,来放置堡垒
56 dfs(k+1,num+1);//进入下一个递归,由于满足条件可以放置堡垒,所以num+1
57 map[x][y]='.';//回溯
58
59 }
60 dfs(k+1,num);//有两种用途一种是没有满足条件的递归,还有一种是回溯以后的递归
61 }
62
63}
64int main()
65{
66 int i,j;
67 while( cin>>n && n )
68 {
69 //输入地图的数据
70 for(i=0;i<n;i++)
71 {
72 getchar();
73 for(j=0;j<n;j++)
74 cin>>map[i][j];
75 }
76 //初始化
77 maxsum=0;
78 //开始深搜
79 dfs(0,0);//从第一个结点开始搜索
80 cout<<maxsum<<endl;
81 }
82
83 return 0;
84}