C语言贪心(1)___Fire Net(Hdu 1045)

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
 


题目大概意思:输入地图,‘.’表示陆地 'X'表示墙,要在地图的陆地上建碉堡,要求每两个碉堡不能面对面,即是不能处于同一列同一行,除非中间有墙隔开.

输出最多能够建多少个碉堡.


这道题可以用暴力搜索贪心二分图匹配


这里献上贪心和暴力搜索这两种方法.


样例1(暴力搜索)

<span style="font-size:10px;">#include <stdio.h>
char map[4][4];
int n,max;
int judge(int i,int j)          //判断函数,判断该点能不能建碉堡.
{
	int k;
	for (k=i-1; k>=0 && map[k][j]!='X'; k--)//上边  
		if(map[k][j]!='.')
			return 0;
	for (k=i+1; k<n && map[k][j]!='X'; k++)//下边  
		if(map[k][j]!='.')
			return 0;
	for (k=j-1; k>=0 && map[i][k]!='X'; k--)//左边  
		if(map[i][k]!='.')
			return 0; 
	for (k=j+1; k<n && map[i][k]!='X'; k++)//右边  
		if(map[i][k]!='.')
			return 0;  
	return 1;
}
void dfs(int step,int num)  //将地图的点从(0,0)到(n-1,n-1)一次标序0~n*n-1,step表示当前点的序号,num表示已经修了几个碉堡.
{
	if(step==n*n)                    
	{
		max=(num>max)? num:max;      //处理完最后一个点就更新最大值.
		return;
	}
	if(judge(step/n,step%n)&&map[step/n][step%n]=='.')   //判断该点能不能建碉堡.</span><span style="font-size: 10px; font-family: Arial, Helvetica, sans-serif;">对于每一个陆地都有两个处理方法.一个是建碉堡,一个是不建.</span><span style="font-size:10px;">
	{
		map[step/n][step%n]='1';               //建
		dfs(step+1,num+1);
		map[step/n][step%n]='.';
	}
	dfs(step+1,num);   //不建
}
int main()
{
	int i,j;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;i++)
			scanf("%s",map[i]);
		max=0;
		dfs(0,0);
		printf("%d\n",max);
	}
	return 0;
}</span>



样例2(贪心算法)

题解:

先对图预处理,再贪心求解

对图预处理如下

                      图一:                                                                                                                                    图二

图二中的数字代表该方格所在的排列上有多少个白方格,计算白方格个数的时候不能越过黑方格。

贪心求解:

第 1 步: 先从第1排开始,找出数字最小的白方格(该方格满足条件),计数,再将该方格排列所触及的摆放个的数字全置无穷大(表示这些方格不再满足条件,无穷的可以是8,因为最大图的规格是4*4)

第 2 步: 然后再找第1排的最小值,当最小值不是8的时候,返回第一步。最小值是8的时候,下一步

第 3 步: 求下一排,方法如前两步,知道求完最后一排,得出解。

代码如下:

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

char A[5][5];
int B[5][5];

int main()
{
	int n, i, j, k, cnt, a, b, min;
	while (scanf("%d", &n) && n)
	{
		memset(A, 0, sizeof(A));
		memset(B, 0, sizeof(B));
		for (i=0; i<n; i++)//数据输入
			scanf("%s", A[i]);
		for (i=0; i<n; i++)
		{//预处理
			for (j=0; j<n; j++)
			{
				if (A[i][j] == '.')
				{//对B[i][j]的处理,令B[i][j]代表该单位与其他相互影响的单位的个数
					for (k=i-1; k>=0 && A[k][j]=='.'; k--)//上边
						B[i][j]++;
					for (k=i+1; k<n && A[k][j]=='.'; k++)//下边
						B[i][j]++;
					for (k=j-1; k>=0 && A[i][k]=='.'; k--)//左边
						B[i][j]++;
					for (k=j+1; k<n && A[i][k]=='.'; k++)//右边
						B[i][j]++;
					B[i][j]++;//自己影响自己
				}
			}
		}
		cnt = 0;
		for (i=0; i<n; i++)
		{
			min = 8;
			for (j=0; j<n; j++)
			{
				if (B[i][j]>0 && min > B[i][j])
				{//找到最小值的坐标
					min = B[i][j];
					a = i;
					b = j;
				}
			}
			if (min != 8)
			{
				cnt++;//找到一个满足条件的位置
				for (k=a-1; k>=0 && A[k][b]=='.'; k--)//上边
					B[k][b] = 8;//因为大的图是4*4的图,一个方格最多能影响另外的7个方格
				for (k=a+1; k<n && A[k][b]=='.'; k++)//下边
					B[k][b] = 8;
				for (k=b-1; k>=0 && A[a][k]=='.'; k--)//左边
					B[a][k] = 8;
				for (k=b+1; k<n && A[a][k]=='.'; k++)//右边
					B[a][k] = 8;
				B[a][b] = 8;
				i--;
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值