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
+1
sharma39vishal1 day ago
// { Driver Code Starts#include<bits/stdc++.h>using namespace std;
// } Driver Code Endsclass Solution {public:void sol(int i,int j,vector<vector<int>>&matrix,vector<vector<int>>ans,vector<vector<int>>&bans,int &gotit){if(gotit==1){    return;} if(i==matrix.size()-1&&j==matrix[0].size()-1){     ans[matrix.size()-1][matrix.size()-1]=1;     for(int r=0;r<matrix.size();r++){     for(int v=0;v<matrix.size();v++){       //  cout<<ans[r][v]<<" ";         bans[r][v]=ans[r][v];     }   //  cout<<endl;     }     gotit=1;    return ;  }    int n=matrix.size(); for(int k=1;k<=matrix[i][j];k++){     if(j+k<n&&matrix[i][j+k]!=0){         ans[i][j]=1;         sol(i,j+k,matrix,ans,bans,gotit);         ans[i][j]=0;     }     if(i+k<n&&matrix[i+k][j]!=0){         ans[i][j]=1;         sol(i+k,j,matrix,ans,bans,gotit);         ans[i][j]=0;     } }}vector<vector<int>> ShortestDistance(vector<vector<int>>&matrix){   vector<vector<int>>ans(matrix.size(),vector<int>(matrix[0].size(),0));       vector<vector<int>>bans(matrix.size(),vector<int>(matrix[0].size(),0));    matrix[matrix.size()-1][matrix[0].size()-1]=1;   int gotit=0;   sol(0,0,matrix,ans,bans,gotit);   if(gotit==0){       vector<vector<int>>noWay(1,vector<int>(1,-1));
       return noWay;   }   return bans;}
};
// { Driver Code Starts.int main(){int tc;cin >> tc;while(tc--){ int n; cin >> n; vector<vector<int>>matrix(n, vector<int>(n,0)); for(int i = 0; i < n; i++){  for(int j = 0; j < n; j++){   cin >> matrix[i][j];  } } Solution obj; vector<vector<int>> ans = obj.ShortestDistance(matrix); for(auto i: ans){  for(auto j: i){   cout << j << " ";  }  cout << "\n"; }}return 0;}  // } Driver Code Ends
0
sharma39vishal
This comment was deleted.
0
tthakare733 weeks ago
//java solution -> TC -> 0.45
class Solution{
    public static int N, M;
    
    public static boolean check(int i, int j, int[][] matrix){
        return (i >= 0 && j >= 0 && i < N && j < M && matrix[i][j] != 0);    
    } 
    
    public static int[][] shortestPath(int[][] mat, int[][] matrix, int i, int j){
        if(i == N - 1 && j == M -1) {
            mat[i][j] = new Integer(1);           
            return mat;
        }
        
        if(check(i, j, matrix)){
             for(int noPath = 1; noPath <= matrix[i][j]; noPath++){
                 //right
                 int[][] right = shortestPath(mat, matrix, i, j+noPath);
                 if(right[N-1][M-1] == 1) {
                     right[i][j] = new Integer(1);
                     return right;
                 }
                 //bottom
                 int[][] bottom = shortestPath(mat, matrix, i+noPath, j);
                 if(bottom[N-1][M-1] == 1) {
                     bottom[i][j] = new Integer(1);
                     return bottom;
                 }
             }
        }
        
        return mat;
    }
    public int[][] ShortestDistance(int[][] matrix){
        // Code here
        N = matrix.length;
        M = matrix[0].length;
        int[][] mat = new int[N][M];
        int[][] noWay = new int[1][1];
        noWay[0][0] = -1;
        int[][] Result = shortestPath(mat, matrix, 0, 0);
        if(Result[N-1][M-1] != 0){
            return Result;
        }
        return noWay;
    }
}
+2
ahmadashad07861 month ago
bool findPath(vector<vector<int>>&m, vector<vector<int>>&res, int i, int j, int n){       if(i<0||j<0||i>=n||j>=n) return false;       if(i==n-1&&j==n-1){           res[i][j]=1;           return true;       }       res[i][j]=1;       int d=m[i][j];       for(int k=1; k<=d; k++){           if(findPath(m, res, i,j+k, n)) return true;           if(findPath(m, res, i+k,j, n)) return true;       }       res[i][j]=0;       return false;   }   vector<vector<int>> ShortestDistance(vector<vector<int>>&matrix){   // Code here   int n=matrix.size();   vector<vector<int>> res(n,vector<int>(n,0));   vector<vector<int>> ans(1);   ans[0].push_back(-1);   if(matrix[0][0]==0&&n>1) return ans;   if(findPath(matrix, res,0,0,n)) return res;      return ans;}
0
100rab_h1 month ago
O(1) Space Complexity
bool isSafe(int i, int j, int n, vector<vector<int>> &matrix) {
        return (i < n && j < n && matrix[i][j] != 0);
    }

    bool distanceRec(vector<vector<int>> &matrix, int n, int i, int j) {
        
        if (i == n - 1 && j == n - 1) {
            matrix[i][j] = -1;
            return true;
        }
        if(isSafe(i, j, n, matrix)) {
            int x = matrix[i][j];
            matrix[i][j] = -1;
            for (int a = 1; a <= x; a++) {
                if (distanceRec(matrix, n, i, j + a) == true) return true;
                if (distanceRec(matrix, n, i + a, j) == true) return true;
            }
            matrix[i][j] = 0;
        }
        return false;
    }
    
	vector<vector<int>> ShortestDistance(vector<vector<int>>&matrix){
	   // Code here
	   int n = matrix.size();
	   bool flag = distanceRec(matrix, n, 0, 0);
	   if (flag) {
	       for (int i = 0; i < n; i++) {
	           for (int j = 0; j < n; j++) {
	               if(matrix[i][j] == -1) matrix[i][j] = 1;
	               else matrix[i][j] = 0;
	           }
	       }
	       return matrix;
	   }
	   else return {{-1}};
	   
	}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.