您的位置:首页 > 其它

hdu 4400 Mines(离散化+bfs+枚举)

2015-09-12 14:37 309 查看
[align=left]Problem Description[/align]

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.


[align=left]Input[/align]

There are several test cases.
In each test case:
Line 1: an integer N, indicating that there are N mines. All mines are numbered from 1 to N.
Line 2…N+1: There are 3 integers in Line i+1 (i starts from 1). 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+2: an integer M, representing the number of operations.
Line N+3...N+M+2 : 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.

1<=M<=N<=100000,0<=xi,yi<=10^9,0<=di<=10^9

Input ends with N=0.


[align=left]Output[/align]

For each test case, you should print ‘Case #X:’ at first, which X is the case number starting from 1. Then you print M lines, each line has an integer representing the number of mines explode in the correspondent operation.


[align=left]Sample Input[/align]

3
0 0 0
1 1 2
2 2 2
3
1
2
3
0


[align=left]Sample Output[/align]

Case #1:
1
2
0


[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Jinhua Online

题意:引爆一个炸弹会同时引爆与它相距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
;
int X
;
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
;
int vis
;
int main()
{
int ac=0;
while(scanf("%d",&n)==1 && n){
for(int i=0;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=0;i<N;i++) mt[i].clear();
for(int i=0;i<n;i++){
int wx=lower_bound(X,X+ng,point[i].x)-X;
mt[wx].insert(Node2(point[i].y,i));
}

memset(vis,0,sizeof(vis));
printf("Case #%d:\n",++ac);
scanf("%d",&m);
for(int u=0;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=0;
vis[k]=1;
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,0));
ry=mt[i].upper_bound(Node2(point[t1].y+dy,0));
for(it=ly;it!=ry;it++){
if(vis[it->id]){
continue;
}
vis[it->id]=1;
q.push(it->id);
}
mt[i].erase(ly,ry);
}
}
printf("%d\n",ans);

}

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: