题目链接:

pid=4462">传送门

题意:一个n*n的区域,有m个位置是能够放稻草人的。其余都是玉米。对于每一个位置(x,y)所放稻草人都有个作用范围ri,

即abs(x-i)+abs(y-j)<=r,(i,j)为作用范围内。问至少要在几个位置上放稻草人,才干覆盖全部的玉米,若不可能则输出-1。

有一个trick,就是放稻草人的位置不用被覆盖

eg:

input:

2 4

1 1 1 2 2 1 2 2

0 0 0 0

output:

0 0

代码例如以下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const int maxn = 60; struct point{
int x,y,wide;
point(){}
point(int _x,int _y):x(_x),y(_y){}
}p[maxn]; int mp[maxn][maxn];
int n,k; bool check(){
int tot=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
tot+=(mp[i][j]>0);
}
return tot==n*n;
} bool flag; void modify(point p,int wide,int val){
for(int i=max(1,p.x-wide);i<=min(n,p.x+wide);i++)
for(int j=max(1,p.y-wide);j<=min(n,p.y+wide);j++){
if(abs(i-p.x)+abs(j-p.y)<=wide&&mp[i][j]!=10000)
mp[i][j]+=val;
}
} void dfs(int num,int id,int tot){
if(tot>num) return;
if(tot==num){
if(check())
flag=1;
return;
}
if(flag||id>=k) return;
modify(p[id],p[id].wide,1);
dfs(num,id+1,tot+1);
modify(p[id],p[id].wide,-1);
dfs(num,id+1,tot);
} int main()
{
while(~scanf("%d",&n)&&n){
scanf("%d",&k);
for(int i=0;i<k;i++){
int x,y;
scanf("%d%d",&x,&y);
p[i]=point(x,y);
mp[x][y]=10000;
}
for(int i=0;i<k;i++){
scanf("%d",&p[i].wide);
}
int ans = 100000000;
for(int i=0;i<=k;i++){
memset(mp,0,sizeof(mp));
for(int j=0;j<k;j++)
mp[p[j].x][p[j].y]=10000;'\'\\
flag = 0;
dfs(i,0,0);
if(flag){
ans = i;
break;
}
}
if(ans!=100000000) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}
/***
4
2
2 2 3 3
1 3
4
2
2 2 3 3
1 4
2 4
1 1 1 2 2 1 2 2
0 0 0 0
***/

HDU 4462 Scaring the Birds (暴力枚举DFS)的更多相关文章

  1. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  2. [dfs+水] hdu 4462 Scaring the Birds

    题意: N*N的矩阵中有M个点能够放稻草人.且给覆盖距离R 每一个稻草人能覆曼哈顿距离R以内的点 问最少须要多少个稻草人 思路: 由于范围非常小,直接能够暴力 注意稻草人所在的位置是不须要被覆盖的 代 ...

  3. HDU 4462 Scaring the Birds (暴力求解,二进制法)

    题意:给定一个 n*n的矩阵,在一些位置放上稻草人,每个稻草人的范围是一定,问你最少几个能覆盖整个矩阵. 析:稻草人最多才10个,所以考虑暴力,然后利用二进制法,很容易求解,并且时间很少0ms,注意有 ...

  4. HDU - 4462 Scaring the Birds

    It's harvest season now! Farmer John plants a lot of corn. There are many birds living around his co ...

  5. hdu 1172 猜数字(暴力枚举)

    题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...

  6. hdu 4445 Crazy Tank (暴力枚举)

    Crazy Tank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. HDU 4930 Fighting the Landlords(暴力枚举+模拟)

    HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型.假设先手能一步走完.或者一步让后手无法管上,就赢 思路:先枚举出两个人全部可能的牌型的最大值.然后再 ...

  8. HDU 1270 小希的数表 (暴力枚举+数学)

    题意:... 析:我们可以知道,a1+a2=b1,那么我们可以枚举a1,那么a2就有了,并且a1+a3=b2,所以a3就有了,我们再从把里面的剩下的数两两相加,并从b数组中去掉, 那么剩下的最小的就是 ...

  9. hdu 5491 The Next(暴力枚举)

    Problem Description Let L denote the number of 1s in integer D’s binary representation. Given two in ...

随机推荐

  1. 3dmax快速实现一个逼真地毯效果

    3dsmax怎么制作逼真的毛绒地毯模型?3dsmax中想要中想要建模长方形的毛绒地毯,该怎么制作呢?下面我们就来看看详细的教程,需要的朋友可以参考下: 1.在创建面板-扩展基本体下选择切角长方体 2. ...

  2. iF.svnadmin 安装遇到的坑

    iF.svnadmin 官网:http://svnadmin.insanefactory.com/ 安装配置iF.svnadmin : http://blog.linhere.com/archives ...

  3. js对象追加到数组里

    描述:将一个点击事件得到的对象追加到数组里 做法:全局声明一个数组,,在对象的点击事件里将得到的对象追加到数组 change(a){ arr.push(a) console.log(arr) var ...

  4. TP5防sql注入、防xss攻击

    框架默认没有设置任何过滤规则 可以配置文件中设置全局的过滤规则 config.php 配置选项 default_filter 添加以下代码即可 // 默认全局过滤方法 用逗号分隔多个 'default ...

  5. php include 和require的区别与转码

    php include 和require的区别相同点:include和require 都能把另外一个文件包含到当前文件中.  不同点:使用include时,当包含的文件不存在时,系统会报出警告级别的错 ...

  6. python 面向对象 类方法,静态方法,property

    property 内置装饰器函数 只在面向对象使用 把方法当初属性使用(方法不加参数) 例子: class Rectangle: def __init__(self,long,wide,color): ...

  7. [luogu] P1772 [ZJOI2006]物流运输(动态规划,最短路)

    P1772 [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线 ...

  8. POJ 2375 Cow Ski Area

    Cow Ski Area Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

  9. sqoop从mysql导入到hdfs出现乱码问题

    最近把hive元数据库的快照数据导入到hdfs中,以便对历史的元数据进行查询. 命令如下: sqoop import -D mapred.job.queue.name=do.production -- ...

  10. Google笔试(2015年8月)

    华电北风吹 天津大学认知计算与应用重点实验室 日期:2015/8/21 这三道题目的PDF能够在这里下载 https://github.com/ncepuzhengyi/jobHuntingExam/ ...