Scaring the Birds

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

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
用一个状压就可了,直接全部枚举,保存最优解,这题要注意,有一个大坑,就是,可能为0,因为,如果全部都是空地,就直接输出0就可以了!
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
using namespace std;
#define K 12
#define inf 0x4f4f4f4f
int r[K],c[K],l[K],n,m,edge;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}},visit[55][55],prime[55][55];
int bfs(int sx,int sy,int goal)
{
int i,j,ans=0;
ans=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(!visit[i][j]&&fabs(i-sx)+fabs(j-sy)<=l[goal])
{
visit[i][j]=1;
ans++;
}
}
return ans;
}
bool judge(int num)
{
int i,ans,all=edge;
if(all==0)
return true;
for(i=0;i<m;i++)
{
if(num&(1<<i))
{
all-=bfs(r[i],c[i],i);
//printf("%d all",all);
if(all==0)
return true;
}
}
return false;
}
int main()
{
int i,ans,j;
while(scanf("%d",&n)!=EOF&&n)
{
scanf("%d",&m);
memset(prime,0,sizeof(prime));
edge=n*n;
for(i=0;i<m;i++)
{
scanf("%d%d",&r[i],&c[i]);
if(!prime[r[i]][c[i]])
{
prime[r[i]][c[i]]=1;
edge--;
}
}
for(i=0;i<m;i++)
scanf("%d",&l[i]);
int all=(1<<m)-1;
int maxx=inf;
for(i=0;i<=all;i++)
{
for(j=0;j<=n;j++)
for(int k=0;k<=n;k++)
{
visit[j][k]=prime[j][k];
}
if(judge(i))
{
ans=i;
int temp=0;
for(j=0;j<m;j++)
{
if(ans&(1<<j))
{
temp++;
}
}
maxx=min(temp,maxx);
}
}
if(maxx!=inf)
printf("%d\n",maxx);
else
printf("-1\n"); }
return 0;
}


hdu4462 Scaring the Birds的更多相关文章

  1. HDU - 4462 Scaring the Birds

    It's harvest season now! Farmer John plants a lot of corn. There are many birds living around his co ...

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

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

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

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

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

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

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

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

  6. HDU 4462Scaring the Birds(枚举所有状态)

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

  7. Regionals 2012 :: HangZhou

    题目传送门排行榜 一个人做了12年北大出的题,自己还是太弱了,图论的知识忘光光,最小生成树裸题写不来,Dijkstra TLE不知道用SPFA. 简单几何(点到线段的距离) + 三分 B Steali ...

  8. 2012 Asia Hangzhou Regional Contest

    Friend Chains http://acm.hdu.edu.cn/showproblem.php?pid=4460 图的最远两点距离,任意选个点bfs,如果有不能到的点直接-1.然后对于所有距离 ...

  9. PAT1118:Birds in Forest

    1118. Birds in Forest (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Some ...

随机推荐

  1. http://blog.csdn.net/shirdrn/article/details/6270506

    http://blog.csdn.net/shirdrn/article/details/6270506

  2. Web开发-表单验证

    表单验证是Web开发中必不可少的一个环节,用来限制用户输入数据的规范和一致性.那么如何能够简化这一任务,让开发人员通过简单的属性设置就能达到目的呢? FineUI在这一点上也是下足了功夫,比Asp.N ...

  3. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-006-处理表单数据(注册、显示用户资料)

    一.显示注册表单 1.访问资源 @Test public void shouldShowRegistration() throws Exception { SpitterRepository mock ...

  4. Dagger 2: Step To Step

    文/iamwent(简书作者)原文链接:http://www.jianshu.com/p/7505d92d7748著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 假设你已经了解 依赖注 ...

  5. 安装gem invalid date format in specification错误的解决方法

    别的不说,报错信息直接贴图: 解决方法: 1.找到你环境目录下的spec,例如:D:\Ruby187\lib\ruby\gems\1.8\specifications. 2.找到引起错误文件的gems ...

  6. 使用GDI+ DrawDriverString实现行距及字符间距控制

    主要是要将一个标题和几段文字绘制到固定大小的图片上,如果一张放不下就生成多张.在使用DrawString是发现无法控制行距 namespace TibetTest {     public class ...

  7. 【HDOJ】1098 Ignatius's puzzle

    数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...

  8. 对象、对象数组、JSON、JSON数组的相关操作

    本文主要是对JS操作JSON的要领做下总结在JSON中,有两种结构:对象和数组 1. 一个对象以“{”(左括号)开始,“}”(右括号)结束.每个“名称”后跟一个“:”(冒号):“"名称/值& ...

  9. Spring Timer 两种实现

    有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz.1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 impo ...

  10. java反编译工具

    由于JAVA语言安全性高.代码优化.跨平台等特性,从1995年5月由SUN公司发布后,迅速取代了很多传统高级语言,占据了企业级网络应用开发等诸多领域的霸主地位. 不过,JAVA最突出的跨平台优势使得它 ...