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

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.

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;
}
时间: 2024-08-29 19:31:46

ZJU-OJ(1002)的相关文章

杭电oj 1002

1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int nCases; 5 int m[1001], n[1001]; 6 char a[1001], b[1001]; 7 int main() 8 { 9 scanf("%d", &nCases); 10 for(int i = 1; i <= nCases; ++i) 11 { 12 memset(m,

PKU OJ 1002 487-3279

? 487-3279 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT

Light oj 1002 Country Roads (Dijkstra)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中心城市为t,问每个城市到中心城市的最小路径的花费,路径花费大小的定义为:一条路上花费最大的边的值. 解题思路: Dijkstra的变形,用Dijkstra求出来的单源路径可以保证每条边都是最优的,所以最短路上的最长边就是所求. 1 #include <algorithm> 2 #include &

九度OJ 1002 Grading

题目1002:Grading 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15686 解决:4053 题目描述: Grading hundreds of thousands of Graduate Entrance Exams is a hard work. It is even harder to design a process to make the results as fair as possible. One way is to assign each exam pro

杭电oj 1002 wrong answer(待改正)

/*#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ int n; int i,j,q;//计数 int al=0; int bl=0; int cl,dl; char turn; char a[1001]; char b[1001]; char c[1002]; int sum[1002];for(i=0;i<1001;i++){ sum[i]=a[i]=b[i]=c[i]='0';

杭电OJ 1002 大数相加

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol

九度oj 1002 Grading 2011年浙江大学计算机及软件工程研究生机试真题

1 #include<iostream> 2 #include<queue> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 int map[15][15]; 9 int main(){ 10 int P,T,G1,G2,G3,GJ; 11 while(cin>>P

oj 1002题 (大数题)

#include <stdio.h> #include <string.h> int main(void) { int q,j,h,k,l; int d; char s1[1001],s2[1001];//题目要求位数不大于1000 scanf("%d",&h); for(l=1;l<=h;l++){ int c[1001]={0}, a[1001]={0},b[1001]={0};//这样可以令数组内全部元素为0 scanf("%s %

图论训练

Light OJ 1002 - Country Roads 1.Dijkstra 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N=555; 5 int d[N]; 6 bool vis[N]; 7 const int INF=0x3f3f3f3f; 8 vector < pair<int,int> > E[N]; 9 10 void init(){ 11 for(int i=0;i<

九度OJ刷题——1002:Grading

题目描述: Grading hundreds of thousands of Graduate Entrance Exams is a hard work. It is even harder to design a process to make the results as fair as possible. One way is to assign each exam problem to 3 independent experts. If they do not agree to eac