Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4516 Accepted Submission(s): 2568
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.

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
#include <iostream>
using namespace std;
char a[4][4];//保存数据
int use[4][4],n;//use的值表示i,j点能否安置碉堡,若为0表示能安置,若为1表示不能
int get(int,int);
void set(int,int);
int main()
{
int i,j;
while(cin>>n&&n)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cin>>a[i][j];
use[i][j]=0;
}
int count=0;//表示能安置碉堡的最大数
int min=0;
while(min<5)
{
min=5;
int ii,jj;
for(i=0;i<n;i++)//遍历图,找到碉堡所能扫射的方向数最小的点安置碉堡(贪心的根本,每次都安置在扫射方向数最小的点,这样就能保证安置的碉堡数最多)
for(j=0;j<n;j++)
{
if(use[i][j]==0&&a[i][j]=='.')
if(get(i,j)<min)
min=get(i,j),ii=i,jj=j;
}
if(min<5)
{
count++;
set(ii,jj);
}
}
cout<<count<<endl;
}
return 1;
}
int get(int i,int j) //某一节点辐射的方向数,如果该方向有'X',不辐射该方向,否则方向数+1
{
int num=0,u,d,l,r;
for(u=i-1;u>=0;u--)
if(a[u][j]=='X')
break;
if(u==-1) num++;
for(d=i+1;d<n;d++)
if(a[d][j]=='X')
break;
if(d==n) num++;
for(l=j-1;l>=0;l--)
if(a[i][l]=='X')
break;
if(l==-1) num++;
for(r=j+1;r<n;r++)
if(a[i][r]=='X')
break;
if(r==n) num++;
return num;
}
void set(int i,int j) //以这个点为中心,四个方向的所有格子的u[i][j]都设置成1,除非遇到X,X后面的不设置成1
{
use[i][j]=1;
for(int u=i-1;u>=0;u--)
{
if(a[u][j]=='X') break;
use[u][j]=1;
}
for(int d=i+1;d<n;d++)
{
if(a[d][j]=='X') break;
use[d][j]=1;
}
for(int l=j-1;l>=0;l--)
{
if(a[i][l]=='X') break;
use[i][l]=1;
}
for(int r=j+1;r<n;r++)
{
if(a[i][r]=='X') break;
use[i][r]=1;
}
}