package leetcode;

import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/keys-and-rooms/
 */
public class Problem841 {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] visited = new boolean[rooms.size()];
        visited[0] = true;
        canVisitAllRooms(rooms, rooms.get(0), visited);
        boolean result = true;
        for (boolean v : visited) {
            if (!v) {
                return false;
            }
        }
        return result;
    }

    private static void canVisitAllRooms(List<List<Integer>> rooms,
                                         List<Integer> keys,
                                         boolean[] visited) {
        for (int key : keys) {
            if (!visited[key]) {
                visited[key] = true;
                canVisitAllRooms(rooms, rooms.get(key), visited);
            }
        }
    }

    public static void main(String[] args) {
        Problem841 prob = new Problem841();
        System.out.println(prob.canVisitAllRooms(Arrays.asList(
            Arrays.asList(1),
            Arrays.asList(2),
            Arrays.asList(3),
            Arrays.asList())
        )); // true
        System.out.println(prob.canVisitAllRooms(Arrays.asList(
            Arrays.asList(1, 3),
            Arrays.asList(3, 0, 1),
            Arrays.asList(2),
            Arrays.asList(0)
        ))); // false
    }
}

@@ -16,33 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -886,520 +886,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem841 prob = new Problem841();%0A        System.out.println(prob.canVisitAllRooms(Arrays.asList(%0A            Arrays.asList(1),%0A            Arrays.asList(2),%0A            Arrays.asList(3),%0A            Arrays.asList())%0A        )); // true%0A        System.out.println(prob.canVisitAllRooms(Arrays.asList(%0A            Arrays.asList(1, 3),%0A            Arrays.asList(3, 0, 1),%0A            Arrays.asList(2),%0A            Arrays.asList(0)%0A        ))); // false%0A    %7D
 %0A%7D%0A

Solve problem 841