JavaScript Coding Practice

Solve Rat Maze With Multiple Jumps using JavaScript Language

Solve Rat Maze With Multiple Jumps using JavaScript to enhance your skills with javascript coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Rat Maze With Multiple Jumps

Difficulty : Medium

Categories :

  • Backtracking

Given an n×n maze matrix where a rat needs to reach from source (0,0) to destination (n-1,n-1). The rat can move in two directions:

  • Forward (right)
  • Down

Each cell contains a number indicating the maximum number of steps the rat can jump from that cell. A value of 0 indicates a blocked cell.

Return a matrix of the same size where 1 indicates cells in the path taken and 0 for unused cells. If no path exists, return a 1×1 matrix containing -1.

Rules:

  • If multiple solutions exist, choose the path with shortest hops
  • For same hop count, prefer forward movement over downward
  • Each jump must be within the max steps allowed by the cell value

Constraints:

  • 1 ≤ n ≤ 50
  • 1 ≤ matrix[i][j] ≤ 20

Examples:

Input: matrix = [
  [2,1,0,0],
  [3,0,0,1],
  [0,1,0,1],
  [0,0,0,1]
]
Output: [
  [1,0,0,0],
  [1,0,0,1],
  [0,0,0,1],
  [0,0,0,1]
]
Explanation: Path takes jumps according to allowed max steps in each cell

Problem Solving

Input

What You'll Find Here

Hands-On Exercises Work on coding problems inspired by real-world scenarios.

Detailed Explanations Break down complex solutions into easy-to-understand steps.

Interactive Learning Test your skills in an engaging and fun way.

Choose from the following categories