hdu4462(DFS)
来源:互联网 发布:node sass windows 编辑:程序博客网 时间:2024/06/08 14:49
Scaring the Birds
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1036 Accepted Submission(s): 351
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
本题给定N*N的方格网,然后给定k个坐标及其所对应的看守范围,求最小选取几个点就能看住所有的方格。
1、本题的N、k较小,可以直接用状态压缩做。枚举选取坐标的所有集合,然后看是否满足条件。
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>using namespace std;const int MAXN=60;const int INF=20;//1000000;bool visited[MAXN][MAXN];struct node{int x,y,r;}Point[15];int main(){int n,i,j,cnt,k,tx,ty,num,ans,tmp;while(scanf("%d",&n)){if(!n)break;scanf("%d",&k);for(i=0;i<k;i++)scanf("%d%d",&Point[i].x,&Point[i].y);for(i=0;i<k;i++)scanf("%d",&Point[i].r);cnt=1<<k;ans=INF;for(i=0;i<cnt;i++){tmp=0;memset(visited,0,sizeof(visited));for(j=0;j<k;j++){visited[Point[j].x][Point[j].y]=true;}for(j=0;j<k;j++){if(i&1<<j){tmp++;for(tx=1;tx<=n;tx++){for(ty=1;ty<=n;ty++){if(abs(Point[j].x-tx)+abs(Point[j].y-ty)<=Point[j].r)visited[tx][ty]=true;}}}}num=0;for(tx=1;tx<=n;tx++){for(ty=1;ty<=n;ty++){if(visited[tx][ty])num++;}}if(num==n*n&&tmp<ans)ans=tmp;}if(ans==INF)printf("-1\n");else {printf("%d\n",ans);}}return 0;}
2、DFS方法,此法在可以状态压缩时一般比较拙劣。
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>using namespace std;const int MAXN=60;const int INF=1000000;bool visited[MAXN][MAXN];struct node{int x,y,r;}Point[15];int n,ans,k;void DFS(int s,int cnt){int i,j,num=0;for(i=1;i<=n;i++){for(j=1;j<=n;j++)if(visited[i][j])num++;}if(num==n*n){if(cnt<ans)ans=cnt;return ;}bool used[MAXN][MAXN];for(i=s+1;i<=k;i++){int tx,ty;for(tx=1;tx<=n;tx++){for(ty=1;ty<=n;ty++){used[tx][ty]=false;if(abs(Point[i].x-tx)+abs(Point[i].y-ty)<=Point[i].r){if(visited[tx][ty])continue;used[tx][ty]=true; visited[tx][ty]=true;}}}DFS(i,cnt+1);for(tx=1;tx<=n;tx++){for(ty=1;ty<=n;ty++)if(used[tx][ty])visited[tx][ty]=false;}}}int main(){int i,j;while(scanf("%d",&n)){if(!n)break;scanf("%d",&k);for(i=1;i<=k;i++)scanf("%d%d",&Point[i].x,&Point[i].y);for(i=1;i<=k;i++)scanf("%d",&Point[i].r);memset(visited,0,sizeof(visited));for(j=1;j<=k;j++){visited[Point[j].x][Point[j].y]=true;}ans=INF;DFS(0,0);if(ans==INF)printf("-1\n");else {printf("%d\n",ans);}}return 0;}
- hdu4462(DFS)
- HDU4462(子集生成)
- hdu4462 Scaring the Birds
- HDU4462 Scaring the Birds
- hdu4462 Scaring the Birds状态压缩
- DFS
- DFS
- dfs
- dfs
- dfs
- dfs
- DFS
- DFS
- dfs
- DFS
- DFS
- DFS
- dfs
- MACHINE_START分析
- 九度1012畅通工程
- 不用变量交换两个int变量的值的分析
- 最新发布EhLib 6.2中的新功能DropDownForms的应用
- java练习题
- hdu4462(DFS)
- LeetCode | Binary Tree Level Order Traversal II
- 找工作笔试面试那些事儿(15)---互联网公司面试的零零种种和多家经验
- 跨域服务调用基本概念及解决方法
- RDF简介RDF
- Python数据结构之图
- qwt例程效果图合集
- 最简单的plist文件读写代码
- LeetCode | Binary Tree Level Order Traversal