HDU Scaring the Birds

来源:互联网 发布:centos 6.5网卡绑定 编辑:程序博客网 时间:2024/05/19 08:25

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.

题目大意

题意:在一个n*n的区域,以水平和竖直的道路为边界。有m个空地且空地位于十字路口上,空地上可以放稻草人,每个空地的稻草人有一定的覆盖半径r(注意半径r是曼哈顿距离),求最少放多少个稻草人才能覆盖所有的区域。

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

题解

状态压缩dp。
因为k很小,所以考虑“状压k”。
但是要注意的是,不一定要将所有的十字路口”都放满(原本是空地的十字路口可不放)。代码如下:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#define inf 0x7fffffffusing namespace std;int n,m,S/*表示所有稻草人的集合*/,count,ans;struct ren{int x,y,r;} d[12];bool pd[55][55],kw[55][55];bool hed(int x,int y){if(x<=0||x>n||y<=0||y>n) return false;else return true;}void doit(){int xuan[12],zz;/*保存所选用的稻草人*/for(int zt=0;zt<=S;zt++)   {zz=count=0;    memset(pd,false,sizeof(pd));for(int i=0;i<m;i++)   {if((1<<i)&zt) xuan[++zz]=i;}    for(int k=1;k<=zz;k++)       {int r=d[xuan[k]].r,x=d[xuan[k]].x,y=d[xuan[k]].y;    for(int i=-r;i<=r;i++)    for(int j=-r;j<=r;j++)//为了避免过多的枚举        {if(hed(x+i,y+j)&&abs(i)+abs(j)<=r)       {if(!pd[x+i][y+j])   {pd[x+i][y+j]=true;    if(!kw[x+i][y+j]) count++;   }   }   }   }if(count+m==n*n) ans=min(zz,ans);   }} int main(){while(scanf("%d",&n)&&n)   {scanf("%d",&m); ans=inf;    memset(kw,false,sizeof(kw));    for(int i=0;i<m;i++)   {scanf("%d%d",&d[i].x,&d[i].y); kw[d[i].x][d[i].y]=true;}    for(int i=0;i<m;i++) scanf("%d",&d[i].r);    S=(1<<m)-1;    doit();    if(ans<inf) printf("%d\n",ans);    else printf("-1\n");   }return 0;}



0 0
原创粉丝点击