HDU4462 Scaring the Birds

来源:互联网 发布:首届全球程序员节 编辑:程序博客网 时间:2024/05/19 13:18

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

题目:
有一片田野,交点处种庄稼。。。。, 一些庄稼被鸟啄坏了,所以农夫想在上面放一个稻草人,来驱赶鸟儿,每个稻草人都有一个驱赶半径r;半径 r 的范围是要用manhattan distance及曼哈顿距离来计算,网上搜了一下,就是每个点x, y 到 稻草人 x,y 绝对值相加不超过r。即
abs(s[k].x - i) + abs(s[k].y - j) <= s[k].r;
然后想要用最少的稻草人把整个田野都保护起来,问 最少几个
这里用到了枚举子集,二进制法枚举,因为只有十个,看网上别人分析了为什么要用枚举子集,而不用直接搜。
这是看的学长的代码。
开始继续写博客了,希望能够继续保持这种态度,上一个星期太懒散,只想着玩,这个星期好一点了。

 #include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;class Scare//稻草人{public:    int x, y, r;    Scare(int xx = 0, int yy = 0, int rr = 0) : x(xx), y(yy), r(rr) {}};Scare s[12];//稻草人int vis[12];//用来看哪个稻草人可以用,因为要枚举int n, nn;int g[60][60];//图int judge()//好像用bool更快一点,{//这里直接看每个点是否被覆盖就好了,直接遍历点,如果某个庄稼没被覆盖,就不行,有稻草人的点是不用被覆盖的。    for(int i = 1; i <= n; i++)        for(int j = 1; j <= n; j++)            if(g[i][j] == 0)            {                int k;                for(k = 0; k < nn; k++)                {                    if(vis[k] == 1)                        if(abs(s[k].x - i) + abs(s[k].y - j) <= s[k].r)                            break;                }                if(k == nn)                    return -1;            }    return 1;}int main(){    while(scanf("%d", &n) != EOF)    {        if(n == 0) break;        scanf("%d", &nn);        memset(g, 0, sizeof g);        for(int i = 0; i < nn; i++)        {            scanf("%d%d", &s[i].x, &s[i].y);            g[s[i].x][s[i].y] = 2;//标记稻草人的位置        }        for(int i = 0; i < nn; i++)//半径        {            scanf("%d", &s[i].r);        }        int ans = 10000;        for(int i = 0; i < (1 << nn); i++)//枚举子集        {            memset(vis, 0, sizeof vis);            int sum = 0;            for(int j = 0; j < nn; j++)                if(i & (1 << j))                    {                        vis[j] = 1;//表示这个稻草人可以用                        sum ++;//个数,最后要用到个数                    }            if(judge() == 1)//如果都覆盖了的话,就跟新一下ans            {                ans = min(ans, sum);            }        }        if(ans == 10000)            ans = -1;        printf("%d\n", ans);    }    return 0;}
0 0
原创粉丝点击