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,将各 ...
随机推荐
- Java学习之Iterator(迭代器)的一般用法
迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...
- HTML5新增的一些属性和功能之六——拖拽事件
拖放事件的前提是分为源对象和目标对象,你鼠标拖着的是源对象,你要放置的位置是目标对象,区分这两个对象是因为HTML5的拖放事件对两者是不同的. 被拖动的源对象可以触发的事件: 1).ondragsta ...
- Android SDK及ADT更新访问问题的解决办法
一.访问问题Eclipse使用SDK Manager更新时总是出现问题 Failed to fetch URL https://dl-ssl.google.com/android/repository ...
- Vagrant入门[转]
Vagrant是一个简单易用的部署工具,用英文说应该是orchestration tool.它能帮助开发人员迅速的构建一个开发环境,帮助测试人员构建测试环境. Vagrant的基本工作原理大致如下: ...
- 在struts2的action中操作域对象(request、session)
在struts2的Action中,操作域对象一共有三种方式: 1.ActionContext(与servelt API无关联): //相当于request ActionContext.getConte ...
- SQLServer 2008 R2 发布订阅配置指南
原以为配置SQLServer 2008 R2的发布订阅很简单,实际配置后才发现过程中有问题地方一直都没搞明白,最后经过几天的查找问题和实践,终于搞定了.现将过程记录如下. SQLServer 2008 ...
- Js 时间间隔计算(间隔天数)
function GetDateDiff(startDate,endDate) { var startTime = new Date(Date.parse(startDate.replac ...
- Find The Multiple
算法:深搜(水题): Given a positive integer n, write a program to find out a nonzero multiple m of n whose d ...
- python下如何处理windows的路径名
f = open(r'e:\迅雷下载\TEK-071\test.txt','r') 在windows下\会被认为是转义字符,所以需要在字符串前加上r,来告诉计算机后面的字符串是没有转义的.
- ecstore后台规格超过一定数量保存丢失
问题描述: 后台规格超过一定数量保存丢失,规格新增不了,删除出问题等不正常情况. 解决方法: 经过波波的不懈努力和日夜冥想终于破了.分析其原因就是因为多个规格组合生成的表单域太多,与php.ini配置 ...