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 rooms numbered from , and some rooms are connected by corridors. You are given a 2D integer array, corridors
, where indicates that there is a corridor connecting and , allowing a person in the maze to go from to 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, is a cycle of length , but and 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:
-
n
-
corridors.length
corridors[i].length
- There are no duplicate corridors.
Examples
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
What’s the correct output for the following inputs?
n
corridors
2
3
0
4
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.
Try it yourself
Implement your solution in the following coding playground.
import java.util.*;public class PathsInMaze {public static int numberOfPaths(int n, int[][] corridors) {// Replace this placeholder return statement with your codereturn -1;}}