HDU 4462 Scaring the Birds(dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4462


题面:

Scaring the Birds

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3094    Accepted Submission(s): 980


Problem Description
It’s harvest season now! 
Farmer John plants a lot of corn. There are many birds living around his corn field. These birds keep stealing his corn all the time. John can't stand with that any more. He decides to put some scarecrows in the field to drive the birds away. 
John's field can be considered as an N×N grid which has N×N intersections. John plants his corn on every intersection at first. But as time goes by, some corn were destroyed by rats or birds so some vacant intersections were left. Now John wants to put scarecrows on those vacant intersections and he can put at most one scarecrow on one intersection. Because of the landform and the different height of corn, every vacant intersections has a scaring range R meaning that if John put a scarecrow on it, the scarecrow can only scare the birds inside the range of manhattan distance R from the intersection.



The figure above shows a 7×7 field. Assuming that the scaring range of vacant intersection (4,2) is 2, then the corn on the marked intersections can be protected by a scarecrow put on intersection (4,2). 
Now John wants to figure out at least how many scarecrows he must buy to protect all his corn.
 

Input
There are several test cases. 
For each test case: 
The first line is an integer N ( 2 <= N <= 50 ) meaning that John's field is an N×N grid. 
The second line is an integer K ( 0<= K <= 10) meaning that there are K vacant intersections on which John can put a scarecrow.
The third line describes the position of K vacant intersections, in the format of r 1,c 1,r 2,c 2 …. r K,c k . (r i,c i) is the position of the i-th intersection and 1 <= r 1,c 1,r 2,c 2 …. r K,c k <= N. 
The forth line gives the scaring range of all vacant intersections, in the format of R 1,R 2…R K and 0 <= R 1,R 2…R K <= 2 × N. 
The input ends with N = 0.
 

Output
For each test case, print the minimum number of scarecrows farmer John must buy in a line. If John has no way to protect all the corn, print -1 instead.
 

Sample Input
  
  
4 2 2 2 3 3 1 3 4 2 2 2 3 3 1 4 0
 

Sample Output
  
  
-1 1
 

Source

题目大意:

    有一块N*N的农田,有一些空地可以放置稻草人,稻草人可以覆盖周围一圈。问最少用几个稻草人覆盖完全部的玉米地。不能,则输出-1。


坑点:

    1.可能没有一个空地,那么直接就是不可能。

    2.所有的地,都是空格,那么就不需要了。

    3.并不是所有的第都要保护,空地就不需要。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,k,tmp,ans;
bool mapp[55][55],grass[55][55];
int x[12],y[12],r[12],path[12];
bool inside(int xx,int yy)
{
	if(xx>=1&&xx<=n&&yy>=1&&yy<=n)
		return 1;
	else
		return 0;
}
void check()
{
	int cnt=0,lim,s,tx,ty;
	memset(mapp,0,sizeof(mapp));
	for(int i=0;i<k;i++)
	{
		if(path[i])
		{
           cnt++;
		   for(int j=x[i]-r[i];j<=x[i];j++)
		   {
			   if(j>=1)
			   {
				   lim=2*(j-x[i]+r[i])+1;
				   s=y[i]-r[i]+(x[i]-j);
				   for(int k=0;k<lim;k++)
				   {
                      tx=j;
					  ty=s+k;
					  if(inside(tx,ty))
					    mapp[tx][ty]=1;
				   }
			   }
		   }
		   for(int j=x[i]+1;j<=x[i]+r[i];j++)
		   {
			   if(j<=n)
			   {
				   lim=2*(r[i]-(j-x[i]))+1;
				   s=y[i]-r[i]+(j-x[i]);
				   for(int k=0;k<lim;k++)
				   {
					   tx=j;
					   ty=s+k;
					   if(inside(tx,ty))
						   mapp[tx][ty]=1;
				   }
			   }
		   }
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			
			if(grass[i][j]&&(!mapp[i][j]))
				return;
		}
	}
	if(cnt<ans)
		ans=cnt;
}
void dfs(int status,int step)
{
	path[step]=status;
	if(step==k-1)
	{
		check();
		return;
	}
	dfs(0,step+1);
	dfs(1,step+1);
}
int main()
{
    while(scanf("%d",&n)&&n)
	{
		memset(grass,-1,sizeof(grass));
		ans=15;
		scanf("%d",&k);
		for(int i=0;i<k;i++)
		{
			scanf("%d%d",&x[i],&y[i]);
			grass[x[i]][y[i]]=0;
		}
		for(int i=0;i<k;i++)
			scanf("%d",&r[i]);
		if(k==0)
		 {
 			printf("-1\n");
		  continue;
	     }
		dfs(0,0);
		dfs(1,0);
		if(ans==15)printf("-1\n");
		else printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值