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 squareboard 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 of the city aspossible so no two can destroy all other. A configuration ofblockhouses islegal provided this no two blockhouses are on the samehorizontal row or vertical column in a map unless there be at least one wallseparating them. In this problem we'll consider small square cities (at most 4x4) that contain walls through Whichbullets cannot run thro Ugh.
The following image shows five pictures of the same board. Thefirst The empty board, the second and third pictures show Legalconfigurations, and the fourth and fifth Ures Show illegal configurations. For this board, the maximum number of blockhouses in a legal configurationis 5; The second picture shows one way to do it, but there are and severalother.
Your task is to write a and given a description of a map, calculates the maximum number of blockhouses that can be placed in thecity in a legal configuration.
The input file contains one or more map descriptions, followed Bya line containing the number 0 that signals the end of th E file. Eachmap description begins with a line containing a positive integernthat is the the size of the city;n would be at most 4. The next nlines each describe one row of the maps, with a '. ' indicating anopen spaces and an uppercase ' X ' indicating a Wal L. There are nospaces in the input file.
For each test case, output one line containing themaximum number of blockhouses that can is placed in the Cityin a legal C Onfiguration.
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
#include <iostream>
using namespace std;
Char map[5][5];
int n;
int Mmax;
void Init () {
int i,j;
for (i=0;i<n;i++) {for
(j=0;j<n;j++)
cin>>map[i][j];
}
}
int putcan (int col,int row) {
int i,j;
for (i=row-1;i>=0;i--) {
if (map[col][i]== ' 0 ') return
0;
else if (map[col][i]== ' X ') break
;
for (j=col-1;j>=0;j--) {
if (map[j][row]== ' 0 ') return
0;
else if (map[j][row]== ' X ') break;
return 1;
}
void Slove (int pos,int ccount) {
int col,row;
if (pos==n*n) {
if (mmax<ccount)
mmax=ccount;
return;
}
col=pos/n;
row=pos%n;
if (map[col][row]== '. ') &&putcan (Col,row)) {
map[col][row]= ' 0 ';
Slove (pos+1,ccount+1);
Map[col][row]= '. ';
}
Slove (Pos+1,ccount);
}
int main () {while
(cin>>n&&n) {
mmax=0;
Init ();
Slove (0,0);
cout<<mmax<<endl;
}
return 0;
}