Rat Maze With Multiple Jumps | Practice | GeeksforGeeks

A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination.
 
Example:
Input: {{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: Rat started with matrix[0][0] and 
can jump up to 2 steps right/down. First check 
matrix[0][1] as it is 1, next check 
matrix[0][2] ,this won't lead to the solution. 
Then check matrix[1][0], as this is 3(non-zero)
,so we can make 3 jumps to reach matrix[1][3]. 
From matrix[1][3] we can move downwards taking 
1 jump each time to reach destination at 
matrix[3][3].

Example 2:
Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1},
{0,0,0,1}}
Output: {{-1}}
Explanation: As no path exists so, -1.

 
Your Task:
You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists.
 
Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j])
Expected Space Complexity: O(1)
 
Constraints:
1 <= n <= 50
1 <= matrix[i][j] <= 20
+3
rainx6 months ago
 
EASY QUESTION
We first try the forward way i.e. y+jump and then we try x+jump since according to question we have to prioritize forward direction path. Just set that cell to 1 and if it doesn't make a path backtrack and make it 0 again. 
 
CODE IMPLEMENTATION
 
bool isSafe(vector<vector<int>> &matrix, int x, int y){
        if(x>=matrix.size() || y>=matrix[0].size() || matrix[x][y]==0){
            return false;
        }
        return true;
}
bool helper(vector<vector<int>> &matrix, vector<vector<int>> &res, int x, int y, int n){
        if(x==n-1 && y==n-1){
            res[x][y]=1;
            return true;
        }
        if(isSafe(matrix,x,y)==true){
            res[x][y]=1;
            for(int i=1;i<=matrix[x][y] && i<n;i++){
                if(helper(matrix,res,x,y+i,n)==true){
                    return true;
                }
                if(helper(matrix,res,x+i,y,n)==true){
                    return true;
                }
            }
            res[x][y]=0;
            return false;
        }
        return false;
}
vector<vector<int>> ShortestDistance(vector<vector<int>> &matrix){
	    int n=matrix.size();
	    if(matrix[0][0]==0 && n!=1){
	        return {{-1}};
	    }
	    vector<vector<int>> res(n,vector<int>(n,0));
	    helper(matrix,res,0,0,n);
	    return res;
}
keep coding and upvote
-2
shivam1706207 months ago
 
 
 
 
WHATS WRONG WITH CODE ?? OR PROVIDE SOLUTION !!
 
 
 
bool issafe(vector<vector<int>>&matrix,int x,int y,int n)   {       if(x>=0 && x<n && y>=0 && y<n && matrix[x][y]!=0)       {           return true;       }       return false;   }   bool mazehelper(vector<vector<int>>&matrix,int x,int y,vector<vector<int>> ans ,int n,int arr[MAX][MAX])   {       if(x==n-1 && y==n-1)       {           arr[x][y]=1;           for(int i=0;i<n;i++)           {               for(int j=0;j<n;j++)               {                   ans[i][j]=arr[i][j];               }           }           return true;       }               if(issafe(matrix,x,y,n)==true)        {            arr[x][y]=1;            for(int i=1;i<=matrix[x][y] && i<n;i++)            {                if(mazehelper(matrix,x+i,y,ans,n,arr)==true)                {                    return true;                }                                if(mazehelper(matrix,x,y+i,ans,n,arr)==true)                {                    return true;                }            }            arr[x][y]=0;           return false;        }        return false;          }vector<vector<int>> ShortestDistance(vector<vector<int>>&matrix){   // Code here     int n=matrix.size();     int arr[MAX][MAX];        vector<vector<int>> ans(n,vector<int>(n));/*    for(int i=0;i<n;i++)   {       for(int j=0;j<n;j++)       {           arr[i][j]=0;       }   }*/      memset(arr,0,n*n*sizeof(int));
         bool res=mazehelper(matrix,0,0,ans,n,arr);    if(res==true)    {        return ans;    }    else    {      vector<vector<int>> buffer(1,vector<int>(1,-1));// buffer[0][0]=-1;   return buffer;    }   }
+1
Navin Ojha8 months ago
Navin Ojha
Very Tricky Question :
Destination will not always be set to 1 so make sure you set it to 1 and then traverse.
0
Shivesh Bharti10 months ago
Shivesh Bharti
there is error in the matrix name in python driver code ,its written matirx instead of matrix please fix that soon
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.