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 ...
随机推荐
- 数字游戏(string的sort的应用)
题目描述 牛牛举办了一场数字游戏,有n个玩家参加这个游戏,游戏开始每个玩家选定一个数,然后将这个数写在纸上(十进制数,无前缀零),然后接下来对于每一个数字将其数位按照非递减顺序排列,得到新的数,新数的 ...
- Reactor Cooling ZOJ - 2314 上下界网络流
Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...
- Node_进阶_7
Node进阶第七天 一.复习 一.索引 数据库中,根据一个字段的值,来寻找一个文档,是很常见的操作.比如根据学号来找一个学生.这个学号是唯一的.只要有学号,就能唯一确认一个学生的文档.学号这个属性 ...
- How Javascript works (Javascript工作原理) (七) WebAssembly 对比 JavaScript 及其使用场景
个人总结: 1.webworkers实现了用多线程浏览器来进行多线程操作js的能力. 2.web workers不能操作dom,window,document等对象,一般用于cpu计算型的任务. ...
- SP687 REPEATS - Repeats(后缀数组)
一个初步的想法是我们枚举重复子串的长度\(L\).然后跑一遍SA.然后我们枚举一个点\(i\),令他的对应点为\(i+L\),然后求出这两个点的LCP和LCS的长度答案就是这个点的答案就是\((len ...
- jQuery第一课 加载页面弹出一个对话框
<script type="text/javascript"> $(document).ready(function(){ alert("欢迎收看:" ...
- 记录python之递归函数
函数move(n,a,b,c)的定义是将n个圆盘从a借助b移动到c. def move(n,a,b,c): if n==1: print a,'-->',c move (n-1,a,c,b) p ...
- 紫书 习题 8-24 UVa 10366 (构造法)
又是一道非常复杂的构造法-- #include<cstdio> #include<algorithm> #define REP(i, a, b) for(int i = (a) ...
- 【图灵杯 A】谷神的赌博游戏
[题目链接]:http://oj.acmclub.cn/problem.php?cid=1164&pid=0 [题意] [题解] 把每个数字都%3处理; 会发现最后1的个数为n+1 2和0的个 ...
- ETL-informatica进阶资料整理
名称 资源 说明 Informatica全球客户支持网站 https://network.informatica.com/ Informatica全球客户支持网站Network,其前身为MySuppo ...