HDU 4462 Scaring the Birds(状态压缩枚举)

2023-10-28 19:40

本文主要是介绍HDU 4462 Scaring the Birds(状态压缩枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

思路:考虑K只有10,压缩一下然后暴力枚举即可,注意的是放稻草人的点是不需要被覆盖的


#include<bits/stdc++.h>
using namespace std;
const int maxn = 150;
#define INF 1e9
int r[maxn],c[maxn],vis[maxn][maxn],R[maxn];
int a[maxn];
int n,k;
int main()
{while(scanf("%d",&n)!=EOF && n){scanf("%d",&k);for(int i = 0;i<k;i++)scanf("%d%d",&r[i],&c[i]);for(int i = 0;i<k;i++)scanf("%d",&R[i]);int ans = INF;for(int s = 0;s<(1<<k);s++){int num = 0;for(int i = 0;i<k;i++)if(s&(1<<i))a[num++]=i;memset(vis,0,sizeof(vis));int flag = 1;for(int i =1;i<=n;i++){for(int j = 1;j<=n;j++){for(int x = 0;x<k;x++){if(i==r[x] && j == c[x])vis[i][j]=1;}if(vis[i][j])continue;for(int x = 0;x<num;x++){if(abs(i-r[a[x]]) + abs(j-c[a[x]]) <=R[a[x]]){vis[i][j]=1;break;}}if(vis[i][j]==0)flag = 0;if (!flag)break;}}if(!flag)continue;ans = min(ans,num);}if(ans > 1000)ans = -1;printf("%d\n",ans);}
}


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


这篇关于HDU 4462 Scaring the Birds(状态压缩枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

hdu1565(状态压缩)

本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

hdu 2489 (dfs枚举 + prim)

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