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.

InputThere 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.OutputFor 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

原来状态压缩就是二进制啊。
这题注意读题,距离不是半径,而是只能上下左右走过去,走一次算一步。
用二进制状态压缩最多也就2^10.美滋滋

附ac代码:
  1 #include <cstdio>
2 #include <iostream>
3 #include <cmath>
4 #include <string>
5 #include <cstring>
6 #include <algorithm>
7 using namespace std;
8 const int maxn = 111;
9 typedef long long ll;
10 const int inf = 0x3f3f3f3f;
11 int mp[maxn][maxn];
12 struct nod
13 {
14 int x,y;
15 int r;
16 }nu[maxn];
17 int vis[maxn];
18 int n,k;
19 int a,b;
20 int c;
21 double getd(int x1,int x2,int y1,int y2)
22 {
23 double ans=abs(x1-x2)+abs(y1-y2);
24 return ans;
25 }
26 int main() {
27 while(scanf("%d",&n),n)
28 {
29 memset(vis,0,sizeof(vis));
30 memset(mp,0,sizeof(mp));
31 scanf("%d",&k);
32
33 for(int i=0;i<k;++i)
34 {
35 scanf("%d%d",&a,&b);
36 nu[i].x=a;
37 nu[i].y=b;
38 mp[a][b]=1;
39 }
40 for(int i=0;i<k;++i)
41 {
42 scanf("%d",&nu[i].r);
43 }
44 int len=(1<<k);
45 int minn=inf;
46 for(int i=0;i<len;++i)
47 {
48 memset(mp,0,sizeof(mp));
49 memset(vis,0,sizeof(vis));
50 for(int u=0;u<k;++u)
51 mp[nu[u].x][nu[u].y]=1;
52 for(int j=0;j<k;++j)
53 {
54
55 vis[j]=i&(1<<j);
56 // printf("%di %d(1<<j) %dj %dvis\n",i,(i<<j)&1,j,vis[j]);
57
58 if(vis[j])
59 {
60
61 for(int u=1;u<=n;++u)
62 {
63 for(int v=1;v<=n;++v)
64 {
65 if(!mp[u][v] && nu[j].r>=getd(nu[j].x,u,nu[j].y,v))
66 {
67 mp[u][v]=1;
68 }
69 }
70 }
71 }
72 }
73 int flag=0;
74 for(int u=1;u<=n;++u)
75 {
76 for(int v=1;v<=n;++v)
77 {
78 if(!mp[u][v])
79 {
80 flag=1;
81 break;
82 }
83 }
84 if(flag==1)
85 break;
86 }
87 int cnt=0;
88 if(!flag)
89 {
90 for(int u=0;u<k;++u)
91 {
92 if(vis[u])
93 {
94 cnt++;
95 // printf("%dcnt\n",cnt);
96 }
97 }
98 // printf("%d %d\n",minn,cnt);
99 minn=min(cnt,minn);
100 }
101 }
102 if(minn!=inf)
103 printf("%d\n",minn);
104 else printf("-1\n");
105 }
106 return 0;
107 }

HDU - 4462 Scaring the Birds的更多相关文章

  1. HDU 4462 Scaring the Birds (暴力枚举DFS)

    题目链接:pid=4462">传送门 题意:一个n*n的区域,有m个位置是能够放稻草人的.其余都是玉米.对于每一个位置(x,y)所放稻草人都有个作用范围ri, 即abs(x-i)+ab ...

  2. HDU 4462 Scaring the Birds (暴力求解,二进制法)

    题意:给定一个 n*n的矩阵,在一些位置放上稻草人,每个稻草人的范围是一定,问你最少几个能覆盖整个矩阵. 析:稻草人最多才10个,所以考虑暴力,然后利用二进制法,很容易求解,并且时间很少0ms,注意有 ...

  3. [dfs+水] hdu 4462 Scaring the Birds

    题意: N*N的矩阵中有M个点能够放稻草人.且给覆盖距离R 每一个稻草人能覆曼哈顿距离R以内的点 问最少须要多少个稻草人 思路: 由于范围非常小,直接能够暴力 注意稻草人所在的位置是不须要被覆盖的 代 ...

  4. hdu4462 Scaring the Birds

    Scaring the Birds Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. HDU 4462:Scaring the Birds(暴力枚举+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=4462 题意:有一个n*n的地图,有k个空地可以放稻草人,给出每个空地可以放的稻草人属性,属性中有个R代表这个位置 ...

  6. HDU 4462

    http://acm.hdu.edu.cn/showproblem.php?pid=4462 一道题意不清的水题 题意:给一个n*n的格子,在上面放草人,每个草人有恐惧范围,问最少选择几个草人可以覆盖 ...

  7. hdu 4462(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4462 思路:由于数据不大,可以直接将所有的状态表示出来,然后枚举,判断能否将方格全部覆盖. http: ...

  8. HDU 4462(暴力枚举)

    因为题目当中的k比较小k <= 10,所以可以直接枚举,题目里面由两个trick, 一个是如果每个点都可以放稻草人的话,那么答案是0, 另外一个就是如果可以放稻草人的点不用被照到.知道了这两个基 ...

  9. hdu 4462 第37届ACM/ICPC 杭州赛区 J题

    题意:有一块n*n的田,田上有一些点可以放置稻草人,再给出一些稻草人,每个稻草人有其覆盖的距离ri,距离为曼哈顿距离,求要覆盖到所有的格子最少需要放置几个稻草人 由于稻草人数量很少,所以状态压缩枚举, ...

随机推荐

  1. Linux 安装分区设置分区大小

    一.Linux分区挂载点介绍 Linux分区挂载点介绍,推荐容量仅供参考不是绝对,跟各系统用途以及硬盘空间配额等因素实际调整: 分区类型 介绍 备注 /boot 启动分区 一般设置100M-200M, ...

  2. Qt Undo Framework

    Qt undo/redo 框架 基于Command设计模式 支持命令压缩和命令合成 提供了与工具包其他部分融合很好的widgets和actions 术语(Terminology) Command - ...

  3. ichartjs插件的使用

    项目中可能会用到饼状图.柱状图.环形图等,ichartjs是一个很不错的插件,体量小,只需引入ichart.1.2.1.min.js即可满足基础需求,github下载地址是:https://githu ...

  4. uni-app开发经验分享三: Vuex实现登录和用户信息留存

    在做用户登录的过程中,其实最重要的是登录成功后的数据要怎么储存,储存到哪里,这里我分享一个利用vuex来实现用户登录和用户数据留存的方法 vuex代码如下: //引入vue和vuex import V ...

  5. COLMAP简易教程(命令行模式)

    完整的 multi view stereo pipeline 会有以下步骤 structure from motion(SfM)==> camera parameters, sparse poi ...

  6. day132:2RenMJ:MJ需求文档&MJ游戏流程&Egret白鹭引擎安装&TypeScript简要介绍

    目录 1.麻将产品需求文档 2.麻将游戏流程 3.Egret白鹭引擎 4.TypeScript简要了解 5.TypeScript快速入门 1.麻将产品需求文档 1.麻将术语 1.名词术语 牌⼦: 序数 ...

  7. Memcached与Redis对比及其优劣分析

    国外讨论 本文主要总结缓存Redis和Memcached的区别,总结之前先参考外国知乎上的一篇问答:<Is memcached a dinosaur in comparison to Redis ...

  8. vuex有哪几种属性

    有五种,分别是 State. Getter.Mutation .Action. Modulestate => 基本数据(数据源存放地)getters => 从基本数据派生出来的数据muta ...

  9. loj10153二叉苹果树

    有一棵二叉苹果树,如果数字有分叉,一定是分两叉,即没有只有一个儿子的节点.这棵树共 N 个节点,标号 1 至 N,树根编号一定为 1. 我们用一根树枝两端连接的节点编号描述一根树枝的位置.一棵有四根树 ...

  10. ts中提示“绑定元素“routes”隐式具有“any”类型”

    思考单独的.ts和typescript框架在vscode层的差异,想想只有launch.json和tsconfig.json,一个个属性过去后,发现是tsconfig.json配置项"noI ...