您的位置:首页 > 其它

hdoj 1045 Fire Net

2015-08-05 11:23 288 查看

Fire Net

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7922 Accepted Submission(s):
4514


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output one line containing the
maximum number of blockhouses that can be placed in the city in a legal
configuration.

[align=left]Sample Input[/align]

4

.X..
....

XX..
....
2
XX

.X
3

.X.
X.X
.X.

3

...

.XX

.XX
4

....

....

....

....

0

[align=left]Sample Output[/align]

5

1
5

2

4
题意:x为墙,机枪不可以打破,“.”为空地可以放置碉堡,碉堡可以朝四个方向开枪并毁灭其他碉堡,所以如果没有墙
的阻挡,碉堡不可以放置在同一行或者同一列,问最多可以放置多少个碉堡

#include<stdio.h>
#include<string.h>
#define maxn(x,y)(x>y?x:y)
char map[10][10];
int n,m,max,tot;
int judge(int r,int c)
{
int i;
if(map[r][c]!='.') return 0;//如果当前位置不是“.”直接不能放置
for(i=r-1;i>=0;i--)//判断上方是否已经放置
{
if(map[i][c]=='o')//如果已经放置则要返回0
return 0;
if(map[i][c]=='X')
break;
}
for(i=c-1;i>=0;i--)//判断左端是否已经放置
{
if(map[r][i]=='o')
return 0;
if(map[r][i]=='X')
break;
}
return 1;
}
void dfs(int x,int y,int tot)
{
int i;
if(x==n&&y==0) max=maxn(max,tot);//如果整个图遍历完毕则看当前所走路径是否可放置最多的碉堡
else
{
for(i=y;i<n;i++)
{
if(!judge(x,i))//如果当前点不可以放置
continue;      //则判断此行的下一个点
map[x][i]='o';//可以放置就标记下来
dfs(x,i+1,tot+1);//并进行此行后边点的判断
map[x][i]='.';//回溯回来,清楚标记
}
dfs(x+1,0,tot);//如果上一行已经遍历完毕,开始遍历下一行
}
}
int main()
{
int i;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
max=0;
dfs(0,0,0);
printf("%d\n",max);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: