hdu 4462 Scaring the Birds【Dfs+暴力判断】

Scaring the Birds

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

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

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


题目大意:

给你一个N*N的一个矩阵,其中有K个位子随着时间的变迁,种植的东西都被毁坏了,农场主可以在这K个位子放置稻草人,也可以不放置稻草人,如果在第i个位子放置稻草人,其作用范围为Ri,问至少用几个稻草人就能将整个农场里边的种植的东西(N*N)都保护起来。


1、首先这K个位子是不需要保护的,因为这K个位子上没有种植的东西。


2、然后观察到K最大为10,那么我们可以Dfs深搜枚举1024种情况,对应每一种情况都暴力处理一下,判断一下当前枚举的情况是否能够保护起来所有的种植东西的格子。如果可以,维护一下最优解即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int x[100];
int y[100];
int map[100][100];
int r[100];
int vis[100];
int n,k;
int ans;
int abs(int w)
{
    if(w<0)return -w;
    else return w;
}
void Dfs(int now)
{
    if(now==k)
    {
        memset(map,0,sizeof(map));
        int sum=0;
        for(int i=0;i<k;i++)
        {
            map[x[i]][y[i]]=1;
            if(vis[i]==0)continue;
            sum++;
            for(int j=1;j<=n;j++)
            {
                for(int kk=1;kk<=n;kk++)
                {
                    if(abs(j-x[i])+abs(kk-y[i])<=r[i])
                    {
                        map[j][kk]=1;
                    }
                }
            }
        }
        int flag=0;
        for(int j=1;j<=n;j++)
        {
            for(int kk=1;kk<=n;kk++)
            {
                if(map[j][kk]==0)
                {
                    flag=1;break;
                }
            }
            if(flag==1)break;
        }
        if(flag==0)
        {
            ans=min(ans,sum);
        }
        return ;
    }
    vis[now]=1;
    Dfs(now+1);
    vis[now]=0;
    Dfs(now+1);
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        scanf("%d",&k);
        for(int i=0;i<k;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
        }
        for(int i=0;i<k;i++)
        {
            scanf("%d",&r[i]);
        }
        memset(vis,0,sizeof(vis));
        ans=0x3f3f3f3f;
        Dfs(0);
        if(ans==0x3f3f3f3f)printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值