Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.math.*;
- /**
- * The code below will read all the game information for you.
- * On each game turn, information will be available on the standard input, you will be sent:
- * -> the total number of visible enemies
- * -> for each enemy, its name and distance from you
- * The system will wait for you to write an enemy name on the standard output.
- * Once you have designated a target:
- * -> the cannon will shoot
- * -> the enemies will move
- * -> new info will be available for you to read on the standard input.
- **/
- class Player {
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- Comparator<Enemy> comparator = new EnemyComparator();
- PriorityQueue<Enemy> queue = new PriorityQueue<Enemy>(100,comparator);;
- // game loop
- while (true) {
- int count = in.nextInt(); // The number of current enemy ships within range
- in.nextLine();
- for (int i = 0; i < count; i++) {
- String enemy = in.next(); // The name of this enemy
- int dist = in.nextInt(); // The distance to your cannon of this enemy
- in.nextLine();
- queue.add(new Enemy(enemy,dist));
- }
- System.out.println(queue.poll().getName());
- queue.clear();
- // Write an action using System.out.println()
- // To debug: System.err.println("Debug messages...");
- // The name of the most threatening enemy (HotDroid is just one example)
- }
- }
- }
- class EnemyComparator implements Comparator<Enemy>
- {
- public int compare(Enemy x , Enemy y)
- {
- return x.getDistance() - y.getDistance() ;
- }
- }
- class Enemy
- {
- private String name ;
- private int distance ;
- public Enemy(String name , int distance)
- {
- this.name = name ;
- this.distance = distance ;
- }
- public String getName()
- {return this.name ;}
- public int getDistance()
- {
- return this.distance ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement