Search⌘ K
AI Features

Solution: Paths in Maze That Lead to Same Room

Discover how to analyze a maze with rooms connected by corridors to find all unique cycles of length three. Learn to build and use adjacency lists, perform intersections to detect common neighbors, and optimize your solution considering time and space complexity. This lesson helps you understand graph traversal techniques and cycle detection crucial for coding interviews involving graph problems.

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 ...