首页 > 专题系列 > Java解POJ > POJ 3736 Snipe the Sniper [解题报告] Java
2013
11-13

POJ 3736 Snipe the Sniper [解题报告] Java

Snipe the Sniper

问题描述 :

Sniper is a hero from a very famous game called DotA (Defence of the Ancient). Although his name is “Sniper”, in most cases he is the one who get sniped. Now the sniper is surrounded by his enemies once more, your task is to write a program to determine whether the sniper can successfully escape.

Let’s put the game into a coordinate system. We assume that at the beginning of the game, the Sniper is positioned at the origin (0,0). When the game starts, the Sniper will start moving straight to the point (100,0) along the x-axis at the speed of 1 unit per second. Once he PASSED the point, he is considered safe.

There are totally n enemies who want to kill the Sniper before he passes the safe point. Each of them has its own coordinate (Xi, Yi) at the beginning of the game. Their move speed is the same with the Sniper’s (1 unit per second). And each one of them can cast one and only one spell to reduce some of the Sniper’s Health Point and stun the Sniper for a period of time( That means, in this period of time, the Sniper will stop moving). Each enemy’s spell has its own range, the enemy can only cast the spell if the distance between him and the Sniper is not longer than the range. We assume all the Sniper’s enemies are clever enough and they will use the best strategy.

输入:

In the beginning of each case is a integer N(1≤N≤100), indicates the number of the Sniper’s enemies. In each line of next N lines, there are 5 numbers, Xi, Yi, Si, Ri. Di, (Xi, Yi) is the initial coordinate of the i-th enemy. Si, Ri, Di are the stun time, range and damage of the i-th enemy’s spell. The last line of each case is consist of one integer, HP, indicate the initial Health Point of the Sniper.

输出:

For each case, if the Sniper can successfully pass the safe point, output “Safe!”. If the Sniper will die before pass the safe point, output “Danger!”.

样例输入:

2
50.0 50.0 10 0.0 0
100 210 0 100 100
100
3
50 50 10 50 50
50 50 10 50 50
200 200 100 50 100
150

样例输出:

Danger!
Safe!

温馨提示:

The picture above provided a possible strategy for the enemies to kill the Sniper in 1st case.

解题代码:

//* @author: 
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Comparator;
import java.util.ArrayList;
public class Main {
 public static class node
 {
  double x,y,s,r,d,t;
  node(double xx,double yy,double ss,double rr,double dd,double tt)
  {
   x=xx;y=yy;s=ss;r=rr;d=dd;t=tt;
  }
  double getx(){return x;}
  double gety(){return y;}
  double gets(){return s;}
  double getr(){return r;}
  double getd(){return d;}
  double gett(){return t;}
 }
/* class MyComparator implements Comparator
 {
     public int compare(Object  obj1, Object obj2) 
     {
      node w1 = (node)obj1;
      node w2 = (node)obj2;
         return w1.get() < w2.get() ? 1 : -1;
     }
 }*/

 public static void main(String[] args) {
    int n,i;
    double pt,hp,x,y,s,r,d,t;
    Scanner in =new Scanner(System.in);
    
    node p[] = new node [101];
    while(in.hasNext())
    {
     n=in.nextInt();
     pt=100;
        for(i=0;i< n;i++)
        {
         x=in.nextDouble();
         y=in.nextDouble();
         s=in.nextDouble();
         r=in.nextDouble();
         d=in.nextDouble();
         t=Math.sqrt((x-100.0)*(x-100.0)+y*y)-r;
         if(t< 0) t=0;
         p[i]=new node(x,y,s,r,d,t);
        }
        hp=in.nextDouble();
           Arrays.sort(p, 0, n, new Comparator< node>(){
               public int compare(node w1, node w2) {
                  return w1.gett() > w2.gett() ? 1 : -1;
               }
           });

//    for(i=0;i< n;i++)System.out.println(p[i].t);
           i=0;
           while(p[i].gett()<=pt)
           {                 
              hp-=p[i].getd();
              pt+=p[i].gets();
              i++;
              if(i==n)break;
           }
           if(hp<=0)System.out.println("Danger!");
           else System.out.println("Safe!");
    }
    }
}