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


    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 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<=1000000<=xi,yi<=10^90<=di<=10^9
    
    Input ends with N=0.
     
    Output
    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.
     
    Sample Input
    3
    0 0 0
    1 1 2
    2 2 2
    3
    1
    2
    3
    0
     
    Sample Output
    Case #1: 
    1 
    2 
    0
     
    Source
     

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

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

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

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

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

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 using namespace std;
    15 #define ll long long
    16 #define eps 1e-10
    17 #define MOD 1000000007
    18 #define N 100006
    19 #define inf 1e12
    20 int n,m;
    21 struct Node{
    22     int x,y,d;
    23 }point[N];
    24 int X[N];
    25 struct Node2{
    26     int y,id;
    27     Node2(int a,int b):y(a),id(b){}
    28     friend bool operator < (Node2 a,Node2 b){
    29         return a.y<b.y;
    30     }
    31     
    32 };
    33 multiset<Node2>mt[N];
    34 int vis[N];
    35 int main()
    36 {
    37     int ac=0;
    38     while(scanf("%d",&n)==1 && n){
    39         for(int i=0;i<n;i++){
    40             scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].d);
    41             X[i]=point[i].x;
    42         }
    43         sort(X,X+n);
    44         int ng=unique(X,X+n)-X;
    45         for(int i=0;i<N;i++) mt[i].clear();
    46         for(int i=0;i<n;i++){
    47             int wx=lower_bound(X,X+ng,point[i].x)-X;
    48             mt[wx].insert(Node2(point[i].y,i));
    49         }
    50         
    51         memset(vis,0,sizeof(vis));
    52         printf("Case #%d:
    ",++ac);
    53         scanf("%d",&m);
    54         for(int u=0;u<m;u++){
    55             int k;
    56             scanf("%d",&k);
    57             k--;
    58             if(vis[k]){
    59                 printf("0
    ");
    60                 continue;
    61             }
    62             multiset<Node2>::iterator ly,ry,it;
    63             queue<int>q;
    64             q.push(k);
    65             int ans=0;
    66             vis[k]=1;
    67             while(!q.empty()){
    68                 ans++;
    69                 int t1=q.front();
    70                 q.pop();
    71                 
    72                 int l=lower_bound(X,X+ng,point[t1].x-point[t1].d)-X;
    73                 int r=upper_bound(X,X+ng,point[t1].x+point[t1].d)-X;
    74                 for(int i=l;i<r;i++){
    75                     int dy=point[t1].d-abs(point[t1].x-X[i]);
    76                     ly=mt[i].lower_bound(Node2(point[t1].y-dy,0));
    77                     ry=mt[i].upper_bound(Node2(point[t1].y+dy,0));
    78                     for(it=ly;it!=ry;it++){
    79                         if(vis[it->id]){
    80                             continue;
    81                         }
    82                         vis[it->id]=1;
    83                         q.push(it->id);
    84                     }
    85                     mt[i].erase(ly,ry);
    86                 }
    87             }
    88             printf("%d
    ",ans);
    89             
    90         }
    91         
    92     }
    93     return 0;
    94 }
    View Code
  • 相关阅读:
    screen命令
    完全背包问题
    01背包问题
    数组排序使得数组负数在正数左边且按照原来的顺序
    Git 后悔药系列
    Nacos作为注册中心和配置中心
    JDK15都出了,你确定不来了解下JDK8吗?
    WPF创建一个凹凸效果的边框
    vue---splitpane分割
    列表实现拖拽
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4802984.html
Copyright © 2020-2023  润新知