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. Collections.sort()

    Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...

  2. JSF和Struts的区别概述

    JSF和Struts的区别概述,都采用taglib来处理表示层:在jsp页面中,二者都是采用一套标记库来处理页面的表示和model层的交互. 据说JSF的主要负责人就是struts的主要作者,所以二者 ...

  3. 利用dedecms给近三天(或当天)发布的文章显示红色日期或加上new字或new小图片

    1)红色日期 <br>[field:pubdate runphp='yes'] <br>$a="<font color=red>".strfti ...

  4. Android自定义控件(二)——有弹性的ScrollView

    本文在http://gundumw100.iteye.com/blog/1075286的基础上稍作修改, 实现了当手指滑动到ScrollView的顶部.底部时, 可以继续的向上.向下拉伸.当释放手指的 ...

  5. [置顶] ./build_native 时出现please define NDK_ROOT

    在一次帮朋友弄cygwin交叉编译时出现了这个问题 cygwin是按照成功了,make-v,以及gcc-v都没出现问题,就是在./build_native 时出现please define NDK_R ...

  6. android sqlite数据库封装 实现crud

    android常用的数据保存方式有文件.sharepreferences.数据库.网络.contentprovider集中方式. 文件存储方式,经常使用在缓存整个页面数据,比如电子书内容.html数据 ...

  7. correlated subquery and non-correlated subquery

    子查询:嵌套在其他查询中的查询称之. 子查询又称内部,而包含子查询的语句称之外部查询(又称主查询). 所有的子查询可以分为两类,即相关子查询和非相关子查询 1>非相关子查询是独立于外部查询的子查 ...

  8. SSH 服务启动时出现如下错误:fatal: Cannot bind any address

    注意:本文相关配置及说明已在 CentOS 6.5 64 位操作系统中进行过测试.其它类型及版本操作系统配置可能有所差异,具体情况请参阅相应操作系统官方文档. 问题描述 云服务器 ECS (Elast ...

  9. js数组 函数

    js数组 filter(),map(),some(),every(),forEach(),lastIndexOf(),indexOf() 文章1:http://www.jb51.net/article ...

  10. ASP.Net MVC与WebForm的区别