Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Player {
  4.  
  5.     @SuppressWarnings("unchecked")
  6.     public static void main(String args[]) {
  7.  
  8.         final Scanner in = new Scanner(System.in);
  9.         final int nodeCount = in.nextInt(); // the total number of nodes in the level, including the gateways
  10.         final int linkCount = in.nextInt(); // the number of links
  11.         final int exitCount = in.nextInt(); // the number of exit gateways
  12.  
  13.         final Set<Integer>[] connections = (Set<Integer>[]) new Set[nodeCount];
  14.  
  15.         for (int i = 0; i < linkCount; i++) {
  16.             final int N1 = in.nextInt(); // N1 and N2 defines a link between these nodes
  17.             if (connections[N1] == null) {
  18.                 connections[N1] = new HashSet<>();
  19.             }
  20.             final int N2 = in.nextInt();
  21.             if (connections[N2] == null) {
  22.                 connections[N2] = new HashSet<>();
  23.             }
  24.  
  25.             connections[N1].add(N2);
  26.             connections[N2].add(N1);
  27.         }
  28.  
  29.         final Set<Integer> exits = new HashSet<>();
  30.         for (int i = 0; i < exitCount; i++) {
  31.             final int exit = in.nextInt(); // the index of a gateway node
  32.             exits.add(exit);
  33.         }
  34.  
  35.  
  36.         Queue<Integer> toVisit = new LinkedList<>();
  37.         Set<Integer> alreadyChecked = new HashSet<>();
  38.         int[] prevIndex = new int[nodeCount];
  39.         boolean found;
  40.  
  41.         // game loop
  42.         while (true) {
  43.             final int agentIndex = in.nextInt(); // The index of the node on which the Skynet agent is positioned this turn
  44.  
  45.             Arrays.fill(prevIndex, -1);
  46.             toVisit.clear();
  47.             alreadyChecked.clear();
  48.             toVisit.add(agentIndex);
  49.             alreadyChecked.add(agentIndex);
  50.             found = false;
  51.  
  52.             while (!toVisit.isEmpty() && !found) {
  53.                 int current = toVisit.poll();
  54.                 Set<Integer> neighbors = connections[current];
  55.                 if (neighbors == null) {
  56.                     continue;
  57.                 }
  58.                 for (int neighbor : neighbors) {
  59.                     if (alreadyChecked.contains(neighbor)) {
  60.                         continue;
  61.                     }
  62.                     prevIndex[neighbor] = current;
  63.                     if (exits.contains(neighbor)) {
  64.                         current = neighbor;
  65.                         while (prevIndex[current] != agentIndex) {
  66.                             current = prevIndex[current];
  67.                         }
  68.                         System.out.println(agentIndex + " " + current);
  69.                         found = true;
  70.                         connections[agentIndex].remove(current);
  71.                         connections[current].remove(agentIndex);
  72.                         break;
  73.                     }
  74.                     alreadyChecked.add(neighbor);
  75.                     toVisit.add(neighbor);
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement