码迷,mamicode.com
首页 > 其他好文 > 详细

Hdu 4462 Scaring the Birds(枚举子集)

时间:2016-08-08 21:12:05      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4462

思路:有坑。可放置稻草人的地方无需覆盖;当k==n*n时,答案为0。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=55;
const int INF=0x3f3f3f3f;
struct Node
{
    int x,y;
};
int R[15];
Node p[15];
int ans,n,k;
vector<int> pick;
int v[maxn][maxn];
void Find()
{
    for(int a=1; a<=n; a++)
    {
        for(int b=1; b<=n; b++)
        {
            if(v[a][b]) continue;
            int flag=0;
            for(int i=0; i<pick.size(); i++)
            {
                int P=pick[i];
                if(abs(a-p[P].x)+abs(b-p[P].y)<=R[P])
                {
                    flag=1;
                    break;
                }
            }
            if(!flag) return ;
        }
    }
    int tmp=pick.size();
    ans=min(ans,tmp);
}
int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        ans=INF;
        scanf("%d",&k);
        memset(v,0,sizeof(v));
        for(int i=0; i<k; i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            v[p[i].x][p[i].y]=1;
        }
        for(int i=0; i<k; i++)
            scanf("%d",&R[i]);
          if(k==n*n)
        {
            printf("0\n");
            continue;
        }
        for(int S=0; S<(1<<k); S++)
        {
            pick.clear();
            for(int i=0; i<k; i++)
                if(S&(1<<i)) pick.push_back(i);
            if(pick.size()<ans) Find();
        }
        if(ans==INF) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


Hdu 4462 Scaring the Birds(枚举子集)

标签:

原文地址:http://blog.csdn.net/wang2147483647/article/details/52154891

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!