杭电1045解题报告

7 篇文章 0 订阅
5 篇文章 0 订阅

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.
 


 

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);
	}
 }


 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
杭电oj是杭州电子科技大学开发的一个在线评测系统,主要用于计算机考研的教育辅助工具。对于计算机考研的学生来说,杭电oj提供了一个良好的平台,可以进行编程练习和算法训练,帮助学生提升编程能力和算法水平。 首先,杭电oj上有大量的题目库,涵盖了计算机考研的各个方面,比如数据结构、算法设计、数据库等,学生可以根据自己的需要选择相应的题目进行练习。题目难度分级明确,从简单到困难,适合不同水平的学生进行练习。通过解题训练,学生可以熟悉各类算法思想和程序设计方法,提高解题能力。 其次,杭电oj提供了在线评测系统,可以帮助学生及时了解自己的编程能力和代码水平。在解答题目后,杭电oj会自动评判答案的正确性和效率,以及输出格式是否正确。学生可以根据评测结果对自己的代码进行优化和改进,提高编程的正确性和效率。 此外,杭电oj还提供了讨论区功能,学生可以与其他考研学生进行交流和讨论。在讨论区里,学生可以提问、回答问题,分享解题思路和经验,相互学习和进步。通过互助学习,学生可以更好地理解和掌握各类算法和编程知识,提高解题的能力和效率。 总的来说,杭电oj对于计算机考研的学生是一个非常有用的教育辅助工具,它提供了题库、评测系统和讨论区,能够帮助学生提升编程能力和算法水平,提高解题能力和效率。对于计算机考研的学生来说,积极利用杭电oj进行练习和学习,能够有效地提高自己的竞争力和通过考研的概率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值