本文主要是介绍HDU 4462 Scaring the Birds(dfs),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:http://acm.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): 3094 Accepted Submission(s): 980
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.
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 r 1,c 1,r 2,c 2 …. r K,c k . (r i,c i) is the position of the i-th intersection and 1 <= r 1,c 1,r 2,c 2 …. r K,c k <= N.
The forth line gives the scaring range of all vacant intersections, in the format of R 1,R 2…R K and 0 <= R 1,R 2…R K <= 2 × N.
The input ends with N = 0.
4 2 2 2 3 3 1 3 4 2 2 2 3 3 1 4 0
-1 1
题目大意:
有一块N*N的农田,有一些空地可以放置稻草人,稻草人可以覆盖周围一圈。问最少用几个稻草人覆盖完全部的玉米地。不能,则输出-1。
坑点:
1.可能没有一个空地,那么直接就是不可能。
2.所有的地,都是空格,那么就不需要了。
3.并不是所有的第都要保护,空地就不需要。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,k,tmp,ans;
bool mapp[55][55],grass[55][55];
int x[12],y[12],r[12],path[12];
bool inside(int xx,int yy)
{if(xx>=1&&xx<=n&&yy>=1&&yy<=n)return 1;elsereturn 0;
}
void check()
{int cnt=0,lim,s,tx,ty;memset(mapp,0,sizeof(mapp));for(int i=0;i<k;i++){if(path[i]){cnt++;for(int j=x[i]-r[i];j<=x[i];j++){if(j>=1){lim=2*(j-x[i]+r[i])+1;s=y[i]-r[i]+(x[i]-j);for(int k=0;k<lim;k++){tx=j;ty=s+k;if(inside(tx,ty))mapp[tx][ty]=1;}}}for(int j=x[i]+1;j<=x[i]+r[i];j++){if(j<=n){lim=2*(r[i]-(j-x[i]))+1;s=y[i]-r[i]+(j-x[i]);for(int k=0;k<lim;k++){tx=j;ty=s+k;if(inside(tx,ty))mapp[tx][ty]=1;}}}}}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(grass[i][j]&&(!mapp[i][j]))return;}}if(cnt<ans)ans=cnt;
}
void dfs(int status,int step)
{path[step]=status;if(step==k-1){check();return;}dfs(0,step+1);dfs(1,step+1);
}
int main()
{while(scanf("%d",&n)&&n){memset(grass,-1,sizeof(grass));ans=15;scanf("%d",&k);for(int i=0;i<k;i++){scanf("%d%d",&x[i],&y[i]);grass[x[i]][y[i]]=0;}for(int i=0;i<k;i++)scanf("%d",&r[i]);if(k==0){printf("-1\n");continue;}dfs(0,0);dfs(1,0);if(ans==15)printf("-1\n");else printf("%d\n",ans);}return 0;
}
这篇关于HDU 4462 Scaring the Birds(dfs)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!