您的位置:首页 > 其它

hdu1045 2010.3.5

2016-02-05 15:08 288 查看
hdu1045 2010.3.5

Fire Net

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1253 Accepted Submission(s): 706

Problem Description

Suppose that we have a square city withstraight 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 hasfour openings through which to shoot. The four openings are facing North, East,South, and West, respectively. There will be one machine gun shooting througheach opening.

Here we assume that a bullet is so powerfulthat it can run across any distance and destroy a blockhouse on its way. On theother hand, a wall is so strongly built that can stop the bullets.

The goal is to place as many blockhouses ina city as possible so that no two can destroy each other. A configuration ofblockhouses is legal provided that no two blockhouses are on the samehorizontal row or vertical column in a map unless there is at least
one wallseparating them. In this problem we will consider small square cities (at most4x4) that contain walls through which bullets cannot run through.

The following image shows five pictures ofthe same board. The first picture is the empty board, the second and thirdpictures show legal configurations, and the fourth and fifth pictures showillegal configurations. For this board, the maximum number of blockhouses
in alegal configuration is 5; the second picture shows one way to do it, but thereare several other ways.

Your task is to write a program that, givena description of a map, calculates the maximum number of blockhouses that canbe placed in the city in a legal configuration.

Input

The input file contains one or more mapdescriptions, followed by a line containing the number 0 that signals the endof the file. Each map description begins with a line containing a positiveinteger n that is the size of the city; n will be at most 4. The
next n lineseach describe one row of the map, with a '.' indicating an open space and anuppercase 'X' indicating a wall. There are no spaces in the input file.

Output

For each test case, output one linecontaining the maximum number of blockhouses that can be placed in the city ina 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

Source

Zhejiang University LocalContest 2001

#include <stdio.h>
#include <string.h>

#define MAXN 10

int map[MAXN][MAXN];
int n,ans,goal;
char ch;

void outout()
{
/*/    int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d",map[i][j]);
printf("\n");
}*/
}

int find (int x,int y)
{
int i;
for(i=x-1;i>=1;i--)
{
if (map[i][y]==0) break;
if(map[i][y]==-1)
return 0;
}
for(i=x+1;i<=n;i++)
{
if (map[i][y]==0) break;
if(map[i][y]==-1)
return 0;
}
for(i=y-1;i>=1;i--)
{
if (map[x][i]==0)break;
if(map[x][i]==-1)
return 0;
}
for(i=y+1;i<=n;i++)
{
if (map[x][i]==0) break;
if(map[x][i]==-1)
return 0;
}
return 1;
}

void deal(int x,int y)
{
int i,j;

if (y==n)
{
for(i=x+1;i<=n;i++)
for(j=1;j<=n;j++)
if ((map[i][j]==1)&&(find(i,j)==1))
{
map[i][j]=-1;
ans++;
if (ans>goal)
{
goal=ans;
outout();
}
deal(i,j);
map[i][j]=1;
ans--;
}
}
else
{
i=x;
for(j=y+1;j<=n;j++)
if ((map[i][j]==1)&&(find(i,j)==1))
{
map[i][j]=-1;
ans++;
if (ans>goal)
{
goal=ans;
outout();
}
deal(i,j);
map[i][j]=1;
ans--;
}
for(i=x+1;i<=n;i++)
for(j=1;j<=n;j++)
if ((map[i][j]==1)&&(find(i,j)==1))
{
map[i][j]=-1;
ans++;
if (ans>goal)
{
outout();
goal=ans;
}
deal(i,j);
map[i][j]=1;
ans--;
}
}
}

void init()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
ch=getchar();
if (ch=='.')
map[i][j]=1;
}
getchar();
}
}
void main()
{
int i,j;
while (scanf("%d",&n),n)
{
getchar();
goal=0;
memset(map,0,sizeof(map));
init();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if ((map[i][j]==1)&&(find(i,j)))
{
ans=1;
if (ans>goal)
{
outout();
goal=ans;
}
map[i][j]=-1;
deal(i,j);
map[i][j]=1;
}
}
printf("%d\n",goal);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: