HDU 4462 Scaring the Birds

来源:互联网 发布:mac usb有线网卡驱动 编辑:程序博客网 时间:2024/05/16 18:21

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4462


Scaring the Birds

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3516    Accepted Submission(s): 1091



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

422 2 3 31 3422 2 3 31 40
 

Sample Output

-11
 

Source

2012 Asia Hangzhou Regional Contest
 

Recommend


思路:数据量不是很大,直接枚举子集即可。对于每一个位置,要么它是在圈套处,要么它受某一个圈套保护。对于能满足的圈套集合,更新答案。详见代码。


附上AC代码:

#include <bits/stdc++.h>using namespace std;const int maxn = 55;bool vis[maxn][maxn];int x[maxn>>2], y[maxn>>2], r[maxn>>2];int n, m;int dist(int j, int k, int s){return abs(j-x[s])+abs(k-y[s]);}int main(){while (~scanf("%d", &n) && n){memset(vis, false, sizeof(vis));scanf("%d", &m);for (int i=0; i<m; ++i){scanf("%d%d", x+i, y+i);vis[x[i]][y[i]] = true;}for (int i=0; i<m; ++i)scanf("%d", r+i);if (m == 0){puts("-1");continue;}int t=1<<m, ans=m+1;for (int i=0; i<t; ++i){bool ok = true;for (int j=1; j<=n&&ok; ++j)for (int k=1; k<=n&&ok; ++k){if (vis[j][k])continue;ok = false;for (int s=0; s<m; ++s)if ((i&(1<<s)) && dist(j, k, s)<=r[s]){ok = true;break;}}if (ok){int cnt = 0;for (int j=0; j<m; ++j)if (i & (1<<j))++cnt;ans = min(ans, cnt);}}printf("%d\n", ans==m+1 ? -1 : ans);}return 0;}


1 0
原创粉丝点击