Python Coding Practice

Solve Rat Maze With Multiple Jumps using Python Language

Solve Rat Maze With Multiple Jumps using Python to enhance your skills with python 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

Interactive Exercises Practice coding with problems designed for beginners and experts.

Step-by-Step Solutions Understand every step of the solution process.

Real-World Scenarios Apply your skills to real-world problems and boost your confidence.

Choose from the following categories