HDU 4462 Scaring the Birds (暴力枚举DFS)
题目链接: 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)的更多相关文章
- HDU 4770 Lights Against Dudely 暴力枚举+dfs
又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...
- [dfs+水] hdu 4462 Scaring the Birds
题意: N*N的矩阵中有M个点能够放稻草人.且给覆盖距离R 每一个稻草人能覆曼哈顿距离R以内的点 问最少须要多少个稻草人 思路: 由于范围非常小,直接能够暴力 注意稻草人所在的位置是不须要被覆盖的 代 ...
- HDU 4462 Scaring the Birds (暴力求解,二进制法)
题意:给定一个 n*n的矩阵,在一些位置放上稻草人,每个稻草人的范围是一定,问你最少几个能覆盖整个矩阵. 析:稻草人最多才10个,所以考虑暴力,然后利用二进制法,很容易求解,并且时间很少0ms,注意有 ...
- 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 ...
- hdu 1172 猜数字(暴力枚举)
题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...
- hdu 4445 Crazy Tank (暴力枚举)
Crazy Tank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 4930 Fighting the Landlords(暴力枚举+模拟)
HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型.假设先手能一步走完.或者一步让后手无法管上,就赢 思路:先枚举出两个人全部可能的牌型的最大值.然后再 ...
- HDU 1270 小希的数表 (暴力枚举+数学)
题意:... 析:我们可以知道,a1+a2=b1,那么我们可以枚举a1,那么a2就有了,并且a1+a3=b2,所以a3就有了,我们再从把里面的剩下的数两两相加,并从b数组中去掉, 那么剩下的最小的就是 ...
- hdu 5491 The Next(暴力枚举)
Problem Description Let L denote the number of 1s in integer D’s binary representation. Given two in ...
随机推荐
- ZBrush笔刷属性栏简介
在笔刷的属性栏当中,最先要了解和掌握的就是Zadd和Zsub两个按钮,当激活Zadd按钮时,我们雕刻的形态向屏幕外突出:当激活Zsub时,我们雕刻的形体就会向屏幕内凹陷.如果在激活Zadd按钮时,雕刻 ...
- eclipse的springMVC环境搭建并输出HelloWorld
spring简单介绍:https://www.cnblogs.com/package-java/p/10368672.html 1.创建一个Maven Project项目 点击下一步 点击下一步 2. ...
- 学习jvm,关于MAT an internal error occurred during:"Parsing heap dump" from问题
写了一个死循环不断的创建对象,模拟内存溢出 package com.zuo.test1; import java.util.ArrayList; import java.util.List; publ ...
- Python 线程高级篇 threading 模块的使用
创建一个tread实例,给他传一个函数 #!/usr/bin/python import threading from time import * loops =[4,2] def loop (nlo ...
- Linux ping 不通 域名 添加DNS
修改路由配置文件 vi /etc/resolv.conf # Generated by NetworkManager #NDS nameserver 192.168.32.2 redhat7 系统优化 ...
- 详解:(cron , crontab , anacron)
导读: 人类把时间做了切割,想象一条笔直的线永远向前,本来这条直线上什么都没有,但是人类根据时间的长短(单位)在这条直线上做了密密麻麻的标记(世纪-年-月-日-时-分-秒-纳秒......),通过这样 ...
- nodejs 守护进程运行
有四种方法: 1.forever forver start bin/www 2.pm2 pm2 strat bin/www 3.node自身进程保护 nohup node /bin/www > ...
- vue-lazyload插件
更详细的内容,请移步 使用 npm install vue-lazyload --save //注册插件 import vueLazyload from 'vue-lazyload' Vue.use( ...
- Could not load the FreeMarker template named 'select'
眼下项目使用struts2, 所以页面中就使用到了struts2的标签,可是今天在做新的功能的时候突然出现 "Could not load the FreeMarker template n ...
- 提高FPGA速度的quartus编译选项
Turning on some optimizations in Quartus II may help increase it. Here are some you may want to try: ...