HDU4462(子集生成)
来源:互联网 发布:sqlserver建表语句 编辑:程序博客网 时间:2024/05/19 22:02
Scaring the Birds
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 714 Accepted Submission(s): 252
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.
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.
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
想死的心都有了呀,,用DFS超时间。当时写的也很吐血,DFS写不好狠容易超时间的,这道题目也可以用以前的思路,就是状态压缩生成子集,这样的速度上面远胜过DFS。。诶诶。以前学过的,也应用不上,所以写也写不出来!
时常复习!!!!
#include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <string>using namespace std;const int INF = 0x3fffffff;int N, M;int Min;int cover[55][55];struct Node{int x;int y;int R;}n[22];int hash[55][55];int vis[55];void Do(Node t){int x = t.x;int y = t.y;int R = t.R;int temp = (x - R) < 0 ? 1 : (x - R);int flag = (y - R) < 0 ? 1 : (y - R);for (int i = temp; i <= x + R; i++){for (int j = flag; j <= y + R; j++){if (abs(i - x) + abs(j - y) <= R){hash[i][j] = 1;}}}}/*void Move(Node t){//cout << "2" << endl;int x = t.x;int y = t.y;int R = t.R;//cout << R << endl;int temp = (x - R) <= 0 ? 1 : (x - R);int flag = (y - R) <= 0 ? 1 : (y - R);for (int i = temp; i <= x + R; i++){for (int j = flag; j <= y + R; j++){if (abs(i - x) + abs(j - y) <= R){//cout << "abs(i - x) + abs(j - y) = " <<abs(i - x) + abs(j - y) << endl;//cout << "R = " << R << endl;//system("pause");hash[i][j] = 0;}}}}*/int Judge(){for (int i = 1; i <= N; i++){for (int j = 1; j <= N; j++){if (!hash[i][j] && !cover[i][j]){return 0;}}}return 1;}int Solve(){int MIN = INF;int a[22];for (int i = 1; i < (1 << M); i++){int cnt = 0;memset(hash, 0, sizeof(hash));for (int j = 0; j < M; j++){if (i & (1 << j)){a[cnt++] = j;}}if (cnt >= MIN){continue;}for (int k = 0; k < cnt; k++){Do(n[a[k]]);}if (Judge() && cnt < MIN){MIN = cnt;}}return MIN;}int main(){while (scanf("%d", &N) != EOF){if (N == 0){break;}Min = INF;memset(vis, 0, sizeof(vis));memset(hash, 0, sizeof(hash));memset(cover, 0, sizeof(cover));scanf("%d", &M);for (int i = 0; i < M; i++){scanf("%d%d", &n[i].x, &n[i].y);cover[n[i].x][n[i].y] = 1;}for (int i = 0; i < M; i++){scanf("%d", &n[i].R);}if (Judge()){printf("0\n");continue;}int ans = Solve();if (ans == INF){printf("-1\n");}else{printf("%d\n", ans);}}return 0;}
- HDU4462(子集生成)
- 生成子集
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 生成子集
- 生成子集
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 子集生成
- 版权声明
- Unable to Unable to load configuration. - bean - jar:file
- codeforces 3C. Tic-tac-toe
- css盒状模型定位
- Log4net 多线程写入
- HDU4462(子集生成)
- Android 中的 Service 全面总结
- 最大公约数和最小公倍数
- acm 2041
- 一个unity3d的博客
- 【python】对文件操作flush的一个理解
- iphone FMDB数据库(2)
- 如何检测和隔离内存泄漏(文章)
- struts2中使用freemarker 生成静态页面