题意:给定一个 n*n的矩阵,在一些位置放上稻草人,每个稻草人的范围是一定,问你最少几个能覆盖整个矩阵。

析:稻草人最多才10个,所以考虑暴力,然后利用二进制法,很容易求解,并且时间很少0ms,注意有一个坑,就是那些指定的位置是可以不用覆盖的,

当时WA一次。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int m, n;
//int x[55], y[55];
int f[55];
int Find(int x){ return x == f[x] ? x : f[x] = Find(f[x]); }
struct node{
int r, c, R;
double d;
bool operator < (const node &p) const{
return d < p.d;
}
};
node a[15];
int ans; int solve(int s){
vector<int> v;
for(int i = 0; i < m; ++i){
if(s & (1<<i)) v.push_back(i);
} if(v.size() >= ans) return INF;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
bool ok = false;
for(int k = 0; k < m; ++k)
if(a[k].r == i && a[k].c == j){ ok = true; break; }
if(ok) continue;
for(int k = 0; k < v.size(); ++k){
if(abs(a[v[k]].r-i)+abs(a[v[k]].c-j) <= a[v[k]].R){ ok = true; break; }
}
if(!ok) return INF;
}
}
return v.size();
} int main(){
while(scanf("%d", &n) == 1 && n){
scanf("%d", &m);
for(int i = 0; i < m; ++i)
scanf("%d %d", &a[i].r, &a[i].c);
for(int i = 0; i < m; ++i) scanf("%d", &a[i].R); ans = INF;
for(int i = 0; i < (1<<m); ++i){
ans = min(ans, solve(i));
}
printf("%d\n", ans == INF ? -1 : ans);
}
return 0;
}

HDU 4462 Scaring the Birds (暴力求解,二进制法)的更多相关文章

  1. HDU 4462 Scaring the Birds (暴力枚举DFS)

    题目链接:pid=4462">传送门 题意:一个n*n的区域,有m个位置是能够放稻草人的.其余都是玉米.对于每一个位置(x,y)所放稻草人都有个作用范围ri, 即abs(x-i)+ab ...

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

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

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

  4. UVa 1151 Buy or Build (最小生成树+二进制法暴力求解)

    题意:给定n个点,你的任务是让它们都连通.你可以新建一些边,费用等于两点距离的平方(当然越小越好),另外还有几种“套餐”,可以购买,你购买的话,那么有些边就可以连接起来, 每个“套餐”,也是要花费的, ...

  5. hdu4462 Scaring the Birds

    Scaring the Birds Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. POJ 1562(L - 暴力求解、DFS)

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

  7. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

  8. 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型

    先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...

  9. UVa 818Cutting Chains (暴力dfs+位运算+二进制法)

    题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...

随机推荐

  1. 418. Sentence Screen Fitting

    首先想到的是直接做,然后TLE. public class Solution { public int wordsTyping(String[] sentence, int rows, int col ...

  2. How to install JDK (Java Development Kit) on Linux

    This tutorial will guide you on how to install JDK (Java Development Kit) on Linux. Since I use Cent ...

  3. 函数 page_dir_get_n_heap

    查看某page中含有的记录个数 #define PAGE_N_HEAP 4 /* number of records in the heap, bit =flag: new-style compact ...

  4. 函数buf_page_init_for_read

    /********************************************************************//** Function which inits a pag ...

  5. crontab无法调用java的问题解决

    本来想将写的代码挂在crontab下运行,谁知道无法运行,没有任何输出,试着用ls -al >> 1.log试了一下,确定crontab是正常运行的. 从网站上找了下问题,原因出在cron ...

  6. 如何在Asp.Net WebApi接口中,验证请求参数中是否携带token标识!

    [BasicAuthentication] public abstract class ApiControllerBase : ApiController { #region Gloal Proper ...

  7. poj 2184 Cow Exhibition

    // 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...

  8. tomcat调优的几个方面

    转载自:http://my.oschina.net/u/593721/blog/146710 作者:小报童 和早期版本相比最新的Tomcat提供更好的性能和稳定性.所以一直使用最新的Tomcat版本. ...

  9. 如何在Android开发中让你的代码更有效率

    最近看了Google IO 2012年的一个视频,名字叫做Doing More With Less: Being a Good Android Citizen,主要是讲如何用少少的几句代码来改善And ...

  10. 【剑指offer 面试题16】反转链表

    思路: 用三个指针preNode.curNode.nextNode完成. #include <iostream> using namespace std; struct ListNode ...