Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Player {
- @SuppressWarnings("unchecked")
- public static void main(String args[]) {
- final Scanner in = new Scanner(System.in);
- final int nodeCount = in.nextInt(); // the total number of nodes in the level, including the gateways
- final int linkCount = in.nextInt(); // the number of links
- final int exitCount = in.nextInt(); // the number of exit gateways
- final Set<Integer>[] connections = (Set<Integer>[]) new Set[nodeCount];
- for (int i = 0; i < linkCount; i++) {
- final int N1 = in.nextInt(); // N1 and N2 defines a link between these nodes
- if (connections[N1] == null) {
- connections[N1] = new HashSet<>();
- }
- final int N2 = in.nextInt();
- if (connections[N2] == null) {
- connections[N2] = new HashSet<>();
- }
- connections[N1].add(N2);
- connections[N2].add(N1);
- }
- final Set<Integer> exits = new HashSet<>();
- for (int i = 0; i < exitCount; i++) {
- final int exit = in.nextInt(); // the index of a gateway node
- exits.add(exit);
- }
- Queue<Integer> toVisit = new LinkedList<>();
- Set<Integer> alreadyChecked = new HashSet<>();
- int[] prevIndex = new int[nodeCount];
- boolean found;
- // game loop
- while (true) {
- final int agentIndex = in.nextInt(); // The index of the node on which the Skynet agent is positioned this turn
- Arrays.fill(prevIndex, -1);
- toVisit.clear();
- alreadyChecked.clear();
- toVisit.add(agentIndex);
- alreadyChecked.add(agentIndex);
- found = false;
- while (!toVisit.isEmpty() && !found) {
- int current = toVisit.poll();
- Set<Integer> neighbors = connections[current];
- if (neighbors == null) {
- continue;
- }
- for (int neighbor : neighbors) {
- if (alreadyChecked.contains(neighbor)) {
- continue;
- }
- prevIndex[neighbor] = current;
- if (exits.contains(neighbor)) {
- current = neighbor;
- while (prevIndex[current] != agentIndex) {
- current = prevIndex[current];
- }
- System.out.println(agentIndex + " " + current);
- found = true;
- connections[agentIndex].remove(current);
- connections[current].remove(agentIndex);
- break;
- }
- alreadyChecked.add(neighbor);
- toVisit.add(neighbor);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement