Paths in Maze That Lead to Same Room

Try to solve the Paths in Maze That Lead to Same Room problem.

Statement#

A maze consists of nn rooms numbered from 1n1 - n, and some rooms are connected by corridors. You are given a 2D integer array, corridors, where corridors[i]=[room1,room2]corridors[i] = [room1, room2] indicates that there is a corridor connecting room1room1 and room2room2, allowing a person in the maze to go from room1room1 to room2room2 and vice versa.

The designer of the maze wants to know how confusing the maze is. The confusion score of the maze is the number of different cycles of length 3.

For example, 12311 → 2 → 3 → 1 is a cycle of length 33, but 12341 → 2 → 3 → 4 and 123211 → 2 → 3 → 2 → 1 are not.

Two cycles are considered to be different if one or more of the rooms visited in the first cycle is not in the second cycle.

Return the confusion score of the maze.

Constraints:

  • 22 \leq n 1000\leq 1000
  • 11 \leq corridors.length 5×104\leq 5 \times 10^4
  • corridors[i].length =2= 2
  • 1room1i,room2in1 \leq room1_i, room2_i \leq n
  • room1iroom2iroom1_i \neq room2_i
  • There are no duplicate corridors.

Examples#

Created with Fabric.js 3.6.6 Sample example 1 Input: n = 5, corridors = [[1,2], [5,2], [4,1], [2,4], [3,1], [3,4]] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Output 2 1 2 3 4 5 There are two different cycles of length 3. One is 1 → 2 → 4 → 1 and other is 1 → 3 → 4 → 1.

1 of 2

Created with Fabric.js 3.6.6 Input: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 2 3 4 n = 4, corridors = [[1,2], [3,4]] Output 0 There are no cycles of length 3. Sample example 2

2 of 2

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Paths in Maze That Lead to Same Room

1

What’s the correct output for the following inputs?

n =5=5

corridors =[[1,2],[3,2],[4,1],[2,4],[5,1],[5,4]]= [[1,2], [3,2], [4,1], [2,4], [5,1], [5,4]]

A)

2

B)

3

C)

0

D)

4

Question 1 of 30 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Create an adjacency matrix to store the rooms as keys and its neighbor rooms as the corresponding values. Create a counter to store the number of cycles.

Start iterating over corridors, and store the neighbors of room1room1 and room2room2 in the matrix.

Take the intersection of the neighbors of room1room1 and room2room2 and add the length of the result to the counter.

Repeat this process until we have iterated over all corridors.


Try it yourself#

Implement your solution in the following coding playground.

Python
usercode > main.py
Input #1
Input #2
Paths in Maze That Lead to Same Room

Solution: Network Delay Time

Solution: Paths in Maze That Lead to Same Room