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 ...
随机推荐
- NodeJS学习笔记 (8)网络服务-http-server(ok)
http服务端概览 创建server 几行代码搞定 var http = require('http'); var requestListener = function(req, res){ res. ...
- axios 使用post方式传递参数,后端接受不到问题
一.URLSearchParams var params = new URLSearchParams(); params.append('key1', 'value1'); //你要传给后台的参数值 ...
- 有趣的console
博文第一篇,就以前端调试的“座上客”---console开始
- 大O时间复杂度
大O表示法指出了在最糟情况下的运行时间.比较操作数,指出了算法运行时间的增速 常见的大O运行时间 O(logn):也叫对数时间,包括二分查找 O(n):也叫线性时间,包括简单查找 O(nlogn):包 ...
- The Zen of Python, by Tim Peters
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Comp ...
- JAVA关于byte数组与String转换的问题
1 public class ToString{ public static void main(String[] args){ String aa = "hellow"; byt ...
- EEPlat PaaS中的多租户数据隔离模式
EEPlat PaaS支持三种租户的数据隔离技术:Sparce Column.tenantId字段隔离.每一个租户独立数据库. 1)Sparce Column,和Salesforce Appforce ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- wsimport 使用方法具体解释
wsimport 使用方法 本文主要介绍wsimport的简单使用方法.帮助大家在webserviceclient开发过程中生成接口代码: 打开java JDK文件夹我们会看到wsimport工具,这 ...
- 经常使用传感器协议1:CJ/T-188 水表协议解析1
本文以实例说明CJ/T-188水表协议的解析过程,下面数据未经特殊说明,均指十六进制. 数据发送: FE FE FE FE 68 10 44 33 22 11 00 33 ...