HDU4462Scaring the Birds 枚举子集

来源:互联网 发布:开源yii框架商城源码 编辑:程序博客网 时间:2024/06/05 06:15

时隔很久,重拾敲代码。。。。

十一月末十二月初事情很多啊。。。

不说了,上题。

HDU4462Scaring the Birds

Scaring the Birds

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


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 r1,c1,r2,c2 …. rK,ck . (ri,ci) is the position of the i-th intersection and 1 <= r1,c1,r2,c2 …. rK,ck <= N. 
The forth line gives the scaring range of all vacant intersections, in the format of R1,R2…RK and 0 <= R1,R2…RK <= 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
422 2 3 31 3422 2 3 31 40
 

Sample Output
-11
 

Source
2012 Asia Hangzhou Regional Contest

题目大意是说有个农场,在边的交叉处种东西,给定几个点放稻草人,每个稻草人影响范围给出曼哈顿距离,问至少多少个稻草人能覆盖全部的农场

大牛们都称之为水题。。

然而还是没有独立完成。。

不过学习了很多东西

子集枚举就是这个题最机智之处了

先上代码

//============================================================================// Name        : HDU4462.c// Author      : zzjzxh// Version     :// Copyright   : Your copyright notice// Description : learn from others//============================================================================#include<stdio.h>#include<math.h>#include<string.h>int main(){int n,k,i,M,x,j,num,ans,flag,s;int rx[12],ry[12],r[12],a[12],f[55][55];    while(scanf("%d",&n)==1 && n){memset(rx,0,sizeof(rx));memset(ry,0,sizeof(ry));memset(a,0,sizeof(a));memset(r,0,sizeof(r));ans=1000000;scanf("%d",&k);for (i=0;i<k;i++){scanf("%d%d",&rx[i],&ry[i]);}for (i=0;i<k;i++){scanf("%d",&r[i]);}M=1<<k;for (s=0;s<M;s++){num=0;for (i=0;i<k;i++)//枚举子集 第一步,s可取的有0,1,2,3 {// 所以二进制为0000,0001,0010,0011 if (s&(1<<i)) //枚举子集 第二步,i可取0,1{//1<<i分别是1,2,二进制分别是0001,0010a[num++]=i;//和s进行&运算,当s=0的时候,子集为空;s=1的时候,子集为第0个稻草人点;s=2的时候,子集为第1个;s=3的时候,子集为0,1两个。}}flag=1;memset(f,0,sizeof(f));for (i=1;i<=n;i++)for (j=1;j<=n;j++){for (x=0;x<k;x++)//判断是不是放稻草人的点 {if (i==rx[x] && j==ry[x])//如果是 {f[i][j]=1;//该点没有粮食,无需覆盖,所以设为true break;}}if (f[i][j]) continue;for (x=0;x<num;x++)//注意!此x非上面的x!这个是在<=num的范围内,枚举a数组下标,而上面的是枚举re,ry下标 {//这个循环用来判断现在枚举的子集中能否覆盖(i,j)这个点 if (fabs(i-rx[a[x]])+fabs(j-ry[a[x]])<=r[a[x]]){f[i][j]=1;break;}}if (!f[i][j]) flag=0;//如果这个点不能被包括进去,flag就是false if (!flag) break;//如果flag是false说明不满足条件,退出枚举点的循环 }if (!flag) continue;//如果flag是false就不拿num和ans比较 if (num<ans) ans=num;}if (ans>1000) {printf("-1\n");continue;}printf("%d\n",ans);}return 0;}

首先,养成好习惯,以后坚持写题头和描述~

M=1<<k;for (s=0;s<M;s++){num=0;for (i=0;i<k;i++)//枚举子集 第一步,s可取的有0,1,2,3 {// 所以二进制为0000,0001,0010,0011 if (s&(1<<i)) //枚举子集 第二步,i可取0,1{//1<<i分别是1,2,二进制分别是0001,0010a[num++]=i;//和s进行&运算,当s=0的时候,子集为空;s=1的时候,子集为第0个稻草人点;s=2的时候,子集为第1个;s=3的时候,子集为0,1两个。}}

这一段是枚举子集的代码

首先,k=2,所以M=4

s可取0.1.2.3

转为二进制:0000.0001.0010.0011

i可取0.1

所以(1<<i)可取1.2

转为二进制:0001.0010

重点:if (s&(1<<k))

&运算,同为1则为1

显然,0000&0001和0000&0010都是0

所以s=0的时候,子集为空集

s=1时

0001&0001=1

0001&0010=0

子集为第0个元素,a记录下标,所以a[0]=0

s=2时

0010&0001=0

0010&0010=1

子集为第1个元素,a记录下标,所以a[0]=1

s=3时

0011&0001 != 0

0011&0010 != 0

所以子集为第0和第1个元素,a记录下标,a[0]=0,a[1]=1

这就是子集枚举的过程

一个含有n个元素的集合,有2^n个子集

所以,M=1<<n

后面的枚举过程也是很机智的,尤其是展现了continue和break的魅力

受益颇多

继续努力! 

0 0