- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Keys and Rooms in Python

Suppose we have N rooms and we start in room 0. In each room there exists a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room. So in other words, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = number of rooms. A key rooms[i][j] = v, this opens the room with number v So if the input is [[1], [2], [3], []]. then output will be true. There are few more points that we should keep in mind −

- Initially, all the rooms start locked (except for room 0).
- We can walk back and forth between rooms freely.
- We should return true if and only if we can enter every room.

So we will start from room 0 and pick up the key 1, then go to room 1, take key for 2, the form room 2, take key for 3, after visiting 3, if all rooms are visited, then return true.

To solve this, we will follow these steps −

- make one empty queue, and make a visited array for all rooms and set as false
- queue := addRooms(rooms, 0, queue, visited)
- visited[0] :=Ture
- while queue has some element
- queue := addRooms(rooms, queue[0], queue, visited)
- mark visited[queue[0]] as true,
- delete the element from queue

- return true, when all elements are true in the visited array
- The addRoom() will take rooms, index, queue and visited array, this will be like
- for i in rooms[index] array
- if i is not visited, then insert i into queue

- return queue

## Example(Python)

Let us see the following implementation to get a better understanding −

class Solution(object): def canVisitAllRooms(self, rooms): queue = [] visited = [False for i in rooms] queue = self.add_rooms(rooms,0,queue,visited) visited[0] = True while len(queue)>0: queue = self.add_rooms(rooms,queue[0],queue,visited) visited[queue[0]] = True queue.pop(0) return all(visited) def add_rooms(self, rooms,index,queue,visited): for i in rooms[index]: if not visited[i]: queue.append(i) return queue ob1 = Solution() print(ob1.canVisitAllRooms([[1],[2],[3],[]]))

## Input

[[1],[2],[3],[]]

## Output

true

- Related Articles
- In a hotel, there are four types of rooms: Basic rooms $-80$ ; Superior rooms $-60$ ; Deluxe rooms $-40$ and Family suits $-20$. Draw a pie chart to represent this data.
- Meeting Rooms II in C++
- Handling missing keys in Python dictionaries
- Properties of Dictionary Keys in Python
- How to Common keys in list and dictionary using Python
- Meeting Rooms 2 problem in JavaScript
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- Program to check whether we can unlock all rooms or not in python
- Program to find first fit room from a list of rooms in Python
- Python - Difference in keys of two dictionaries
- Keys associated with Values in Dictionary in Python
- How do I loop through a JSON file with multiple keys/sub-keys in Python?
- Get dictionary keys as a list in Python
- Why must dictionary keys be immutable in Python?
- Python - Intersect two dictionaries through keys