C++ Rat in a Maze with Multiple Steps or Jump Allowed


Given a n*n grid maze. Our rat is present in the top left corner of the grid. Now rats can only move down or forward, and if and only if the block has a non-zero value to it now in this variation rat is allowed to have multiple jumps. The maximum jump that the rat can take from the current cell is the number present in the cell, and now you are tasked to find if the rat can reach the bottom right corner of the grid, for example −

Input : { { {1, 1, 1, 1},
{2, 0, 0, 2},
{3, 1, 0, 0},
{0, 0, 0, 1}
},
Output : { {1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 1}
}

Input : {
{2, 1, 0, 0},
{2, 0, 0, 1},
{0, 1, 0, 1},
{0, 0, 0, 1}
}
Output: Path doesn't exist

Approach to Find the Solution

In this approach, we will use backtracking to track every path that the rat can take now. If the rat reaches our destination from any path, we return true for that path and then print the path. Else, we print that the path doesn’t exist.

Example

 
#include <bits/stdc++.h>
using namespace std;
#define N 4 // size of our grid
bool solveMaze(int maze[N][N], int x, int y, // recursive function for finding the path
    int sol[N][N]){
        if (x == N - 1 && y == N - 1) { // if we reached our goal we return true and mark our goal as 1
            sol[x][y] = 1;
            return true;
    }
    if (x >= 0 && y >= 0 && x < N && y < N && maze[x][y]) {
        sol[x][y] = 1; // we include this index as a path
        for (int i = 1; i <= maze[x][y] && i < N; i++) { // as maze[x][y] denotes the number of jumps you can take                                             //so we check for every jump in every direction
            if (solveMaze(maze, x + i, y, sol) == true) // jumping right
               return true;
            if (solveMaze(maze, x, y + i, sol) == true) // jumping downward
               return true;
        }
        sol[x][y] = 0; // if none are true then the path doesn't exist
                   //or the path doesn't contain current cell in it
        return false;
    }
    return false;
}
int main(){
    int maze[N][N] = { { 2, 1, 0, 0 }, { 3, 0, 0, 1 },{ 0, 1, 0, 1 },
                   { 0, 0, 0, 1 } };
    int sol[N][N];
    memset(sol, 0, sizeof(sol));
    if(solveMaze(maze, 0, 0, sol)){
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++)
                cout << sol[i][j] << " ";
            cout << "\n";
        }
    }
    else
        cout << "Path doesn't exist\n";
    return 0;
}

Output

1 0 0 0
1 0 0 1
0 0 0 1
0 0 0 1

Explanation of the Above Code

In the above approach, we check for every path that it can make from our current cell, and while we check that, we mark the paths as one now. When our path reaches the dead-end, we check if that dead-end is our destination or not. Now, if that is not our destination, we backtrack, and as we backtrack, we mark the cell as 0 as this path is not valid, and this is how our code proceeds.

Conclusion

In this tutorial, we solve Rat in a Maze with multiple steps or jumps allowed. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Updated on: 26-Nov-2021

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements