HDU4462Scaring the Birds 枚举子集

2023-10-28 19:40

本文主要是介绍HDU4462Scaring the Birds 枚举子集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

不说了,上题。

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 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
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的魅力

受益颇多

继续努力! 

这篇关于HDU4462Scaring the Birds 枚举子集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/295506

相关文章

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

【Rust练习】12.枚举

练习题来自:https://practice-zh.course.rs/compound-types/enum.html 1 // 修复错误enum Number {Zero,One,Two,}enum Number1 {Zero = 0,One,Two,}// C语言风格的枚举定义enum Number2 {Zero = 0.0,One = 1.0,Two = 2.0,}fn m

枚举相关知识点

1.是用户定义的数据类型,为一组相关的常量赋予有意义的名字。 2.enum常量本身带有类型信息,即Weekday.SUN类型是Weekday,编译器会自动检查出类型错误,在编译期间可检查错误。 3.enum定义的枚举类有什么特点。         a.定义的enum类型总是继承自java.lang.Enum,且不能被继承,因为enum被编译器编译为final修饰的类。         b.只能定义

【C语言】结构体、枚举、联合体

【C语言】结构体、枚举、联合体 文章目录 @[TOC](文章目录) 前言一、结构体声明1.一般格式2.typedef 重命名结构体类型定义变量 二、结构体数组三、结构体与指针及函数传参四、结构体传参五.结构体在内存的存储六、参考文献总结 前言 使用工具: 1.编译器:VScode 2.C Primer Plus 第六版-1 提示:以下是本篇文章正文内容,下面案例可供参考

代码随想录刷题day24丨93.复原IP地址 ,78.子集 , 90.子集II

代码随想录刷题day24丨93.复原IP地址 ,78.子集 , 90.子集II 1.题目 1.1复原IP地址 题目链接:93. 复原 IP 地址 - 力扣(LeetCode) 视频讲解:回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址_哔哩哔哩_bilibili 文档讲解:https://programmercarl.com/0093.%E5%A4%8

10730-Antiarithmetic?【暴力枚举】

水题 求一个序列是否存在3个数按顺序构成等差数列 直接枚举等差数列的差值 时间复杂度降到 n * n / 3 开pos数组记录每个值得为之 楷vis数组记录目前i是否出现过 强行AC 15221397 10730 Antiarithmetic? Accepted C++ 0.035 2015-03-26 12:09:56 #include<cstdio>#include

Java基础入门 【第七章 抽象、接口、内部类、枚举】(二)

匿名内部类书写格式: 父类或接口类型变量名=new父类或接口(构造方法实参列表){ //重写所有的抽象方法 @Override public返回值类型method1(形参列表){ 方法体实现 } @Override public返回值类型method2(形参列表){ 方法体实现 } //省略... }; //匿名内部类对象调用方法 变量名.重写方法(实参列表); 匿名

UVA10010(八方向暴力枚举)

Where's Waldorf? Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18656 Description Where's Waldo

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There