Fire Net time Limit:2 Seconds Memory limit:65536 KB
Suppose that we have a square city with straight streets. A map of a city are a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle this has four openings through which to shoot. The four openings are facing north, east, South, and west, respectively. There would be one machine gun shooting through each opening.
Here we are assume that a bullet are so powerful, it can run across any distance and destroy a blockhouse in its way. On the other hand, a wall are so strongly built that can stop the bullets.
The goal is to place as many blockhouses as possible so no two can destroy all other. A configuration of blockhouses is legal provided this no two blockhouses are on the same horizontal row or vertic The Al column in a map unless there was at least one wall separating them. In this problem we'll consider small square cities (at most 4x4) that contain walls through which bullets cannot run thr Ough.
The following image shows five pictures of the same board. The empty board, the second and third pictures show legal configurations, and the fourth and fifth PI Ctures Show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; The second picture shows one way to does it, but there are other several.
Your task is to write a and 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 maps descriptions followed by a line containing the number 0 that signals the end of T He file. Each map description begins with a line containing a positive integer n, is the size of the city;n'll be at most 4. The next n lines each describe one row of the maps, with a '. ' indicating an open space and a uppercase ' X ' indicating a W All. There are no spaces in the input file.
For each test case, output one line containing the maximum number of blockhouses so can is 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
The main effect of the topic:
There are walls and open spaces on the map, bunkers to be placed inside, bunkers can attack in four directions, but the attack will be blocked by the wall. The rule is that the bunker can't attack each other. Ask for the maximum number of bunkers that can be placed.
In the beginning, the way to mark the attack range was used, but the cost of the restoration was too high, or the method of judgment was used.
Moreover, it is difficult to write recursion directly with the coordinate x,y in the beginning. Later found that the online use of a number of coordinates to represent the method, so that the completion of the conditions also become simple.
#include <iostream> #define WALL 0 #define EMPTY 1 #define PUT 2 using namespace std;
int n;
int isok[5][5];
int MAXN;
BOOL Canput (int x,int y) {if (Isok[x][y]==wall) return false;
for (int i=x-1;i>=0;i--) {if (isok[i][y]==wall) break;
if (isok[i][y]==put) return false;
for (int i=x+1;i<n;i++) {if (isok[i][y]==wall) break;
if (isok[i][y]==put) return false;
for (int i=y-1;i>=0;i--) {if (isok[x][i]==wall) break;
if (isok[x][i]==put) return false;
for (int i=y+1;i<n;i++) {if (isok[x][i]==wall) break;
if (isok[x][i]==put) return false;
return true;
} void Dfs (int k,int cnt) {if (k==n*n) {if (maxn<cnt) {maxn=cnt;
Return
} else {int x=k/n;
int y=k%n; if (cAnput (X,y)) {isok[x][y]=put;
DFS (K+1,CNT+1);
Isok[x][y]=empty;
DFS (K+1,CNT);
int main () {while (cin>>n,n) {A for (int i=0;i<n;i++) {char C;
for (int j=0;j<n;j++) {cin>>c;
if (c== '. ')
Isok[i][j]=empty;
else if (c== ' X ') Isok[i][j]=wall;
}} maxn=0;
DFS (0,0);
cout<<maxn<<endl;
}
}