HDU Scaring the Birds
2014-04-02 18:20
302 查看
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
42
2 2 3 3
1 3
4
2
2 2 3 3
1 4
0
Sample Output
-11
题解
状态压缩dp。因为k很小,所以考虑“状压k”。
但是要注意的是,不一定要将所有的“十字路口”都放满(原本是空地的十字路口可不放)。代码如下:
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #define inf 0x7fffffff using 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; }
相关文章推荐
- HDU 3006
- False Mirrors
- hdu 5067 状态dp
- N - Pieces (状态dp)
- J - Long Dominoes (状态dp)
- K - Tourism Planning (状态dp)
- W - 钥匙计数之一 (状态dp)
- O - Necklace (状态dp)
- M - 方格取数(1) (状态dp)
- G - Survival (状态dp)
- C - 炮兵阵地 (状态dp)
- D - Hie with the Pie (状态dp)
- A - Mondriaan's Dream (状态dp)
- L - Doing Homework (状态dp)
- B - Corn Fields (状态dp)
- Corn Fields POJ - 3254 (状压DP)
- Pieces (状态dp)
- 方格取数(1) (状态dp)
- Doing Homework (状态dp)
- Long Dominoes (状态dp)