Problem Description
Terrorists put some mines in a crowded square recently. The police evacuate all people in time before any mine explodes. Now the police want all the mines be ignited. The police will take many operations to do the job. In each operation, the police will ignite one mine. Every mine has its "power distance". When a mine explodes, any other mine within the power distance of the exploding mine will also explode. Please NOTE that the distance is Manhattan distance here.

More specifically, we put the mines in the Cartesian coordinate system. Each mine has position (x,y) and power distance d.

The police want you to write a program and calculate the result of each operation.
Input
There are several test cases.
In each test case:
Line : an integer N, indicating that there are N mines. All mines are numbered from to N.
Line …N+: There are integers in Line i+ (i starts from ). They are the i-th mine’s position (xi,yi) and its power distance di. There can be more than one mine in the same point.
Line N+: an integer M, representing the number of operations.
Line N+...N+M+ : Each line represents an operation by an integer k meaning that in this operation, the k-th mine will be ignited. It is possible to ignite a mine which has already exploded, but it will have no effect. <=M<=N<=,<=xi,yi<=^,<=di<=^ Input ends with N=.
 
Output
For each test case, you should print ‘Case #X:’ at first, which X is the case number starting from . Then you print M lines, each line has an integer representing the number of mines explode in the correspondent operation.
 
Sample Input

 
Sample Output
Case #: 
 
Source

题意:引爆一个炸弹会同时引爆与它相距d的炸弹

重点:由于x,y坐标的范围很大,所以必须离散化,显而易见。在这里可以利用sort+unique进行离散化并存储在myhash中。

其次由于一个点可能多次放炸弹,但只有一次有效,所以用一个vis数组记录

所以对于任意一个炸弹(x,y,d)。首先由x-d,x+d在myhash中确定y在set的范围first_pos,last_pos

然后 再在set中按照y的范围寻找。。。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m;
struct Node{
int x,y,d;
}point[N];
int X[N];
struct Node2{
int y,id;
Node2(int a,int b):y(a),id(b){}
friend bool operator < (Node2 a,Node2 b){
return a.y<b.y;
} };
multiset<Node2>mt[N];
int vis[N];
int main()
{
int ac=;
while(scanf("%d",&n)== && n){
for(int i=;i<n;i++){
scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].d);
X[i]=point[i].x;
}
sort(X,X+n);
int ng=unique(X,X+n)-X;
for(int i=;i<N;i++) mt[i].clear();
for(int i=;i<n;i++){
int wx=lower_bound(X,X+ng,point[i].x)-X;
mt[wx].insert(Node2(point[i].y,i));
} memset(vis,,sizeof(vis));
printf("Case #%d:\n",++ac);
scanf("%d",&m);
for(int u=;u<m;u++){
int k;
scanf("%d",&k);
k--;
if(vis[k]){
printf("0\n");
continue;
}
multiset<Node2>::iterator ly,ry,it;
queue<int>q;
q.push(k);
int ans=;
vis[k]=;
while(!q.empty()){
ans++;
int t1=q.front();
q.pop(); int l=lower_bound(X,X+ng,point[t1].x-point[t1].d)-X;
int r=upper_bound(X,X+ng,point[t1].x+point[t1].d)-X;
for(int i=l;i<r;i++){
int dy=point[t1].d-abs(point[t1].x-X[i]);
ly=mt[i].lower_bound(Node2(point[t1].y-dy,));
ry=mt[i].upper_bound(Node2(point[t1].y+dy,));
for(it=ly;it!=ry;it++){
if(vis[it->id]){
continue;
}
vis[it->id]=;
q.push(it->id);
}
mt[i].erase(ly,ry);
}
}
printf("%d\n",ans); } }
return ;
}

hdu 4400 Mines(离散化+bfs+枚举)的更多相关文章

  1. HDU 4400 Mines(好题!分两次计算距离)

    http://acm.hdu.edu.cn/showproblem.php?pid=4400 题意: 在笛卡尔坐标中有多个炸弹,每个炸弹有一个坐标值和一个爆炸范围.现在有多次操作,每次引爆一个炸弹,问 ...

  2. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  4. 离散化+BFS HDOJ 4444 Walk

    题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...

  5. hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

    Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  6. HDU 5025Saving Tang Monk BFS + 二进制枚举状态

    3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...

  7. HDU 5925 Coconuts 离散化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5925 Coconuts Time Limit: 9000/4500 MS (Java/Others) ...

  8. hdu 5303 DP(离散化,环形)+贪心

    题目无法正常粘贴,地址:http://acm.hdu.edu.cn/showproblem.php?pid=5303 大意是给出一个环形公路,和它的长度,给出若干颗果树的位置以及树上的果子个数. 起点 ...

  9. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

随机推荐

  1. C#生成带项目编号的Word段落

    using System; using Microsoft.Office.Interop.Word; using Word = Microsoft.Office.Interop.Word; names ...

  2. NetAnalyzer笔记 之 三. 用C++做一个抓包程序

    [创建时间:2015-08-27 22:15:17] NetAnalyzer下载地址 经过前两篇的瞎扯,你是不是已经厌倦了呢,那么这篇让我们来点有意思的吧,什么,用C#.不,这篇我们先来C++的 Wi ...

  3. jquery Tabs选项卡切换

    效果: HTML部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  4. Android Xfermode 实战 实现圆形、圆角图片

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42094215,本文出自:[张鸿洋的博客] 1.概述 其实这篇本来准备Androi ...

  5. VS2013以管理员身份使用

    Win8系统: 1.将C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe改为以管理员身份运行. 2.将 ...

  6. .Net调用Office Com组件的原理及问题检索com类工厂组件检索 COM 类工厂中 CLSID 为 {XXX} 的组件失败

    我是在本地32位操作系统+vs2010+office2007做创建并下载Excel,ppt文件的操作没有问题,发布到64位系统的服务器上报错,最开始报错:: 1:Retrieving the COM ...

  7. hdu2488 dfs

    G - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  8. 最短路径floy算法———模板

    #include<cstdio>int n,i[1000][1000];int main(){ scanf("%d",&n); for (int a=1;a&l ...

  9. static说明

    1.最基本用法:加static的全局变量或者函数,只能在本文件中使用.可见性只在本文件中. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.为理解这句话,我举例来说 ...

  10. POJ2446 二分图最大匹配

    问题:POJ2446 分析: 采用黑白相间的方法把棋盘分成两个点集,是否可以用1*2的卡片实现全覆盖等价于二分图是否有完全匹配. AC代码 //Memory: 172K Time: 32MS #inc ...