Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10072 Accepted Submission(s): 5866
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
题目大意就是(看图理解我说的)只有白格子可放碉堡,每行每列的碉堡不能面对面,面对面是指同一行/列的两个碉堡如果没有黑格子夹在中间那就是面对面,不管隔多远。黑格子就是防止碉堡面对面的,求能建的最多碉堡数。
解题思路:
从第一行开始,一个一个格子的检测是否可以放碉堡,先检测对应行的左边是否有碉堡,再检测对应列的上方是否有碉堡,没有的话就先把坐标记下来,等把同一行的都检查完了,优先在下方有挡板的白格子建碉堡,如果很多都有挡板,那就在距离该行最近的挡板所在列建碉堡(这样可以充分挡板,建更多的碉堡)。
C代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
char a[6][6],b,c,d;
int n,i,j,k,p,q,x,u,v;//pq记录可建碉堡的坐标
while(scanf("%d",&n)!=EOF&&n!=0){
getchar();
for(i=0;i<n;i++)
gets(a[i]);
for(i=0;i<n;i++){
d=0;
u=n;
for(j=0;j<n;j++){//检测横排
if(a[i][j]=='X') {//把x之前的碉堡输出
if(u<n){
a[i][v]='#';
u=n;
}
if(d>0)
a[p][q]='#';
d=0;
continue;
}
b=j;//检测左边是否有'#'
while(b!=0){
if(a[i][b-1]=='#')
break;
if(a[i][b-1]=='X'){
b=0;break;
}
b--;
}
if(b>0) continue;
c=i;//检测上面是否有'#'
while(c!=0){
if(a[c-1][j]=='#')
break;
if(a[c-1][j]=='X'){
c=0;break;
}
c--;
}
if(c>0) {
if(j==n-1&&u<n){
a[i][v]='#';
d=0;}
if(j==n-1&&d>0)
a[p][q]='#';
continue;
}
d++;
if(d==1){//记录可做碉堡的最左一个的坐标
p=i;q=j;
}
c=i;//检测下面是否有'x'
while(c<n-1){
if(a[c+1][j]=='X')
break;
c++;
}
if(c<n-1){
if(c+1<u){//比较x的位置,选择最近的x的上方为'#'
u=c+1;
v=j;
}
d=0; //有x的上方坐标记录就不需要pq
}
if(j==n-1&&u<n){
a[i][v]='#';
d=0;}
if(j==n-1&&d>0)
a[p][q]='#';
}
}
x=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if((a[i][j])=='#')
x++;
printf("%d\n",x);
}
}