Advertisement
abuzreq

solution train1

Oct 1st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4.  
  5. /**
  6.  * The code below will read all the game information for you.
  7.  * On each game turn, information will be available on the standard input, you will be sent:
  8.  * -> the total number of visible enemies
  9.  * -> for each enemy, its name and distance from you
  10.  * The system will wait for you to write an enemy name on the standard output.
  11.  * Once you have designated a target:
  12.  * -> the cannon will shoot
  13.  * -> the enemies will move
  14.  * -> new info will be available for you to read on the standard input.
  15.  **/
  16. class Player {
  17.  
  18.    
  19.     public static void main(String args[]) {
  20.         Scanner in = new Scanner(System.in);
  21.         Comparator<Enemy> comparator = new EnemyComparator();
  22.          PriorityQueue<Enemy>  queue = new PriorityQueue<Enemy>(100,comparator);;
  23.        
  24.      
  25.  
  26.         // game loop
  27.         while (true) {
  28.             int count = in.nextInt(); // The number of current enemy ships within range
  29.                
  30.             in.nextLine();
  31.             for (int i = 0; i < count; i++) {
  32.                 String enemy = in.next(); // The name of this enemy
  33.                 int dist = in.nextInt(); // The distance to your cannon of this enemy
  34.                 in.nextLine();
  35.                 queue.add(new Enemy(enemy,dist));
  36.             }
  37.          
  38.             System.out.println(queue.poll().getName());
  39.  
  40.         queue.clear();
  41.             // Write an action using System.out.println()
  42.             // To debug: System.err.println("Debug messages...");
  43.  
  44.            // The name of the most threatening enemy (HotDroid is just one example)
  45.         }
  46.     }
  47. }
  48.  
  49. class EnemyComparator implements Comparator<Enemy>
  50. {
  51.     public int compare(Enemy x , Enemy y)
  52.     {
  53.         return x.getDistance() - y.getDistance() ;
  54.     }
  55.    
  56.    
  57. }
  58.  
  59. class Enemy
  60. {
  61.     private String name ;
  62.     private int distance ;
  63.    
  64.     public Enemy(String name , int distance)
  65.     {
  66.         this.name = name ;
  67.         this.distance = distance ;
  68.     }
  69.    
  70.     public String getName()
  71.     {return this.name ;}
  72.    
  73.     public int getDistance()
  74.     {
  75.         return this.distance ;
  76.     }
  77.  
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement