hdu 4400 Mines(离散化+bfs+枚举)
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.
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=.
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.
Case #:
题意:引爆一个炸弹会同时引爆与它相距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+枚举)的更多相关文章
- HDU 4400 Mines(好题!分两次计算距离)
http://acm.hdu.edu.cn/showproblem.php?pid=4400 题意: 在笛卡尔坐标中有多个炸弹,每个炸弹有一个坐标值和一个爆炸范围.现在有多次操作,每次引爆一个炸弹,问 ...
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- 离散化+BFS HDOJ 4444 Walk
题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...
- hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)
Mines Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- HDU 5025Saving Tang Monk BFS + 二进制枚举状态
3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...
- HDU 5925 Coconuts 离散化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5925 Coconuts Time Limit: 9000/4500 MS (Java/Others) ...
- hdu 5303 DP(离散化,环形)+贪心
题目无法正常粘贴,地址:http://acm.hdu.edu.cn/showproblem.php?pid=5303 大意是给出一个环形公路,和它的长度,给出若干颗果树的位置以及树上的果子个数. 起点 ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
随机推荐
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- C# Excel导入、导出
本篇主要介绍C#的Excel导入.导出. 目录 1. 介绍:描述第三方类库NPOI以及Excel结构 2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图.NOPI以及C#代 ...
- Collections.sort()
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...
- JavaScript学习总结(二)
JavaScript学习总结(二) ---- 对象 在JavaScript中,几乎用到的每个js都离不开它的对象.下面我们深入了解一下js对象. js中对象的分类跟之前我们学过的语言中函数的分类一样, ...
- java -jdk配置1(环境变量配置)
此文转载自:http://www.cnblogs.com/nicholas_f/articles/1494073.html 进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1 ...
- Android与JS混编(js调用java)
项目中需要使用android与js的混编来开发app. 下面就介绍一下吧. 有时候我们需要用js调用native控件,要想实现这个功能,我们需要做的就只有三步: 1.允许webview执行js脚本 2 ...
- Objective-C set/get方法
主要内容set get方法的使用 关键字 @property 全自动生成set get方法 // 类的声名 @interface People : NSObject{ int _age; // 成员变 ...
- web 调用OPC HRESULT:0x80070005 (E_ACCESSDENIED))
除了配置DCOM外,还需要在web.config里面添加设置系统管理员权限的帐号和密码<identity impersonate="true" userName=" ...
- php+mysql将大数据sql文件导入数据库
<?php $file_name = "d:test.sql"; $dbhost = "localhost"; $dbuser = "root& ...
- jquery交替实现隐藏、显示
<html> <head> <title></title> </head> <script src="j ...