HDU 4462 Scaring the Birds 第37届ACM/ICPC 杭州赛区 J题 (简单题)

Scaring the Birds

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20    Accepted Submission(s): 10

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.  

 

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 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.  

 

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 4 2 2 2 3 3 1 3 4 2 2 2 3 3 1 4 0  

 

Sample Output -1 1  

 

Source
2012 Asia Hangzhou Regional Contest    
比较简单的题目。
但是有陷阱。
它给出的可以放的点是不必要覆盖的。
而且答案可能是0的,就是N*N个点都是放东西的时候。  
按照状态压缩去枚举就可以了。  

//============================================================================
// Name        : HDU4462.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
const int MAXN=60;
int r[12],c[12];
int R[12];
bool f[MAXN][MAXN];
int a[12];
int main() {
    int n;
    int k;
    while(scanf("%d",&n)==1 && 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=1000000;
        int M=(1<<k);
        for(int s=0;s<M;s++)
        {
            int num=0;
            for(int i=0;i<k;i++)
              if(s&(1<<i))
              {
                  a[num++]=i;
              }
            memset(f,false,sizeof(f));
            bool flag=true;
            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])
                            f[i][j]=true;
                    if(f[i][j])continue;
                    for(int x=0;x<num;x++)
                    {
                        if(abs(i-r[a[x]])+abs(j-c[a[x]])<=R[a[x]])
                        {
                            f[i][j]=true;
                            break;
                        }
                    }
                    if(f[i][j]==false)flag=false;
                    if(!flag)break;
                }
            if(!flag)continue;
            ans=min(ans,num);
        }
        if(ans>1000)ans=-1;
        printf("%d\n",ans);
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/11/08/2761128.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞