CSES - NOI 2019 - Results
Submission details
Task:Thieves and Prisons
Sender:Luis Galan
Submission time:2019-03-06 13:06:06 +0200
Language:Java
Status:COMPILE ERROR

Compiler report

input/brute.java:57: error: cannot find symbol
            Event[i] = new Event(type, thief);
            ^
  symbol:   variable Event
  location: class brute
1 error

Code

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;

public class brute {

    public static boolean isInPrison(int thief_prisonIndex) {
        return thief_prisonIndex != -1;
    }

    public static boolean isOpenValid(int[] thieves, int thief, int tentativePrison) {
        boolean prisonEmpty = true;
        for (int i = 0; i < thieves.length; i++) {
            if (thieves[i] == tentativePrison) {
                prisonEmpty = false;
                break;
            }
        }
        if (prisonEmpty) {
            return false;
        }
        return true;
    }

    public static class Event {
        boolean type; // false = C, true = O
        int thief;
        int prison;

        public Event(boolean type, int thief) {
            this.type = type;
            this.thief = thief;
            this.prison = -1;
        }
    }

    public static void main(String[] args) {
        // int n = 3; // number of thieves
        // int k = 2; // number of prisons
        // int m = 5; // number of events

        // Event[] events = new Event[] { new Event(false, 1), new Event(false, 2), new Event(true, 3), new Event(true, 2),
        //         new Event(false, 1), };

        Scanner scanner = new Scanner(System.in);

        String[] line1 = scanner.next().split(" ");
        int n = Integer.parseInt(line1[0]);
        int k = Integer.parseInt(line1[1]);
        int m = Integer.parseInt(line1[2]);

        Event[] events = new Event[m];
        for (int i = 0; i < m; i++) {
            String[] next = scanner.next().split(" ");
            boolean type = next[0] == "O";
            int thief = Integer.parseInt(next[1]);
            Event[i] = new Event(type, thief);
        }

        // backtracking algorithm
        
        boolean possible = true;

        int[] thieves = new int[n];
        Arrays.fill(thieves, -1);

        ArrayList<int[]> history = new ArrayList<>();
        history.add(thieves);

        int index = 0;

        while (true) {
            if (index >= events.length) {
                break;
            }

            if (index < 0) {
                possible = false;
                break;
            }

            int[] curThieves = history.get(history.size() - 1);
            Event curEvent = events[index];

            curEvent.prison++;
            if (curEvent.prison >= k || // we have exhausted all possible configurations
                    isInPrison(curThieves[curEvent.thief - 1])) { // thief is in prison, can't take an action
                // backtrack
                curEvent.prison = -1;
                history.remove(history.size() - 1);
                index--;
                continue;
            }

            if (events[index].type == false) { // a thief has been caught
                // catch the thief
                int[] nextThieves = curThieves.clone();
                nextThieves[curEvent.thief - 1] = curEvent.prison;
                history.add(nextThieves);
                index++;
            } else { // a thief has opened a gate
                if (isOpenValid(curThieves, curEvent.thief - 1, curEvent.prison)) {
                    int[] nextThieves = curThieves.clone();
                    // free all prisoners
                    for (int i = 0; i < nextThieves.length; i++) {
                        if (nextThieves[i] == curEvent.prison) {
                            nextThieves[i] = -1;
                        }
                    }
                    history.add(nextThieves);
                    index++;
                    continue;
                } else {
                    // illegal action
                    continue;
                }
            }
        }

        if (possible) {
            String outStr = "";
            for (int i = 0; i < events.length; i++) {
                outStr += (events[i].prison + 1) + " ";
                System.out.println(events[i].prison + 1);
            }
        } else {
            System.out.println("IMPOSSIBLE");
        }

        scanner.close();
    }
}