本文主要是介绍HDU 4462 Scaring the Birds(状态压缩枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路:考虑K只有10,压缩一下然后暴力枚举即可,注意的是放稻草人的点是不需要被覆盖的
#include<bits/stdc++.h>
using namespace std;
const int maxn = 150;
#define INF 1e9
int r[maxn],c[maxn],vis[maxn][maxn],R[maxn];
int a[maxn];
int n,k;
int main()
{while(scanf("%d",&n)!=EOF && n){scanf("%d",&k);for(int i = 0;i<k;i++)scanf("%d%d",&r[i],&c[i]);for(int i = 0;i<k;i++)scanf("%d",&R[i]);int ans = INF;for(int s = 0;s<(1<<k);s++){int num = 0;for(int i = 0;i<k;i++)if(s&(1<<i))a[num++]=i;memset(vis,0,sizeof(vis));int flag = 1;for(int i =1;i<=n;i++){for(int j = 1;j<=n;j++){for(int x = 0;x<k;x++){if(i==r[x] && j == c[x])vis[i][j]=1;}if(vis[i][j])continue;for(int x = 0;x<num;x++){if(abs(i-r[a[x]]) + abs(j-c[a[x]]) <=R[a[x]]){vis[i][j]=1;break;}}if(vis[i][j]==0)flag = 0;if (!flag)break;}}if(!flag)continue;ans = min(ans,num);}if(ans > 1000)ans = -1;printf("%d\n",ans);}
}
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.
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.
这篇关于HDU 4462 Scaring the Birds(状态压缩枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!