首页 > 代码库 > ZJU-OJ(1002)

ZJU-OJ(1002)

翻译:

假设我们有一个方形城市。每一个方块代表一个街道或者一堵墙。每个碉堡有四个发射口,分别对应东南西北。
这个我们假设每个子弹有足够的威力可以射击任意距离并且可以摧毁这个方向上碉堡。另外,一堵墙可以抵挡子弹并使其停下。
我们的目标是在一个城市中安放尽可能多的碉堡并且使得任意两个碉堡都不能相互摧毁。一种碉堡的安放方法就是没有任意两个
碉堡在一条水平或者垂直线,除非他们直接由一堵墙隔离。在这个问题中我们将考虑一个城市是带有不能被子弹贯穿的墙。
下面我们在同一种面板展示5个图片,第一个面板是空面板,第二个和第三个面板展示了一种合法设置,第四第五个展示了非法设置。
在下面面板中,一种合法的设置最多包含5个碉堡;第二幅图即展示了一种实现的方法,同样也有其他的方法。你的任务就是写一个程序,
给予一幅地图描述,计算出一种合法的最大碉堡摆放方法。输入数据包括一个或更多的地图描述,伴随一个带有数字0的行意味着输入结束。
每个地图描述以一个正整数n开始,它代表城市大小,n最大为4.下面n行每一行描述地图中的一行,‘.‘代表空地,‘X’代表墙。
对于每个测试用例,输出的每一行包括一个合法配置的最大碉堡安放数。

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 asmany blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses islegal 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.

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 nextn 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.

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<stdio.h>
char map[4][4];
int best,n;
int CanPut(int row,int col)
{
	int i;
	for(i= row - 1;i >= 0; i--)
	{
		if(map[i][col] == 'O') return 0;
		if(map[i][col] == 'X') break;
	}  //列无墙和碉堡
	for(i = col - 1; i>= 0 ; i--)
	{
		if(map[row][i] == 'O') return 0;
		if(map[row][i] == 'X') break;
	}//行无墙和碉堡,列也无
	return 1;
}
void solve(int k, int tot)
{
	int x,y;
	if(k==n*n)
	{
		if(tot>best)
		{
			best = tot;
			return;
		}
	}
	else
	{
		x = k/n;  //行 起始为0
		y = k%n;  //列 起始为0
		if((map[x][y]=='.') && (CanPut(x,y)))
		{
			map[x][y]='O'; //如果合法放碉堡
			solve(k+1,tot+1);
			map[x][y]='.';
		}
		solve(k+1,tot);
	}
	
}
int main()
{
	int i,j;
	scanf("%d",&n);
	while(n>0)
	{
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				scanf("%1s",&map[i][j]);
		best = 0;
		solve(0,0);
		printf("%d\n",best);
		n=0;
		scanf("%d",&n);
	}
	return 0;
}


ZJU-OJ(1002)