Special Matrix | Practice | GeeksforGeeks

You are given a matrix having n rows and m columns. The special property of this matrix is that some of the cells of this matrix are given as input, which are blocked i.e. they cannot be reached. Now you have to start from the cell (1,1) and reach the end (n,m) provided during the journey you can move horizontally right from the current cell or vertically down from the current cell. Can you answer the number of ways you can traverse the matrix obeying the above constraints starting from (1,1) and ending at (n,m).
 
Example 1:
Input: n = 3, m = 3, k = 2,
blocked_cells = {{1,2},{3,2}}.
Output: 1
Explanation: There is only one path from
(1,1) to(3,3) which is (1,1)->(2,1)->(2,2)->
(2,3)->(3,3).

Example 2:
Input: n = 5, m = 5, k = 1,
blocked_cells = {{5,5}}
Output: 0
Explanation: There is no path to reach at 
(5,5) beacuse (5,5) is blocked.

 
Your Task:
You don't need to read or print anything, Your task is to complete the function FindWays() which takes n, m and blocked_cells as input parameter and returns total number of ways to reach at (n, m) modulo 109 + 7.
 
Expected Time Complexity: O(n*m)
Expected Space Complexity: O(n*m)
 
Constraints:
1 <= n, m <= 500
1 <= k <= n*m 
0
anutiger1 day ago
C++ Solution :
+1
kalamarham1 month ago
int mod=1e9+7;   int solve(int n,int m,vector<vector<int>> &arh,vector<vector<int>> &ans)   {       if(n==1 and m==1 and arh[n][m]==1) return 1;       if(n<1 or m<1 or arh[n][m]==0) return 0;       if(ans[n][m]!=-1) return ans[n][m]%mod;       int l=(solve(n-1,m,arh,ans)%mod+solve(n,m-1,arh,ans)%mod)%mod;       return ans[n][m]=l%mod;   }int FindWays(int n, int m, vector<vector<int>>blocked_cells){    // Code here    vector<vector<int>> arh(n+1,vector<int>(m+1,1));    vector<vector<int>> ans(n+1,vector<int>(m+1,-1));    for(int i=0;i<blocked_cells.size();i++)    {        arh[blocked_cells[i][0]][blocked_cells[i][1]]=0;    }    int y=solve(n,m,arh,ans);    return y%mod;}
0
triple2double11 month ago
FOR NOOBS
	int FindWays(int m, int n, vector<vector<int>>bcs){
	   vector<vector<long long>>dp(m+1,vector<long long>(n+1,0));
       int mod=1000000007;
	   for(auto bc:bcs){
	       dp[bc[0]][bc[1]]=-1;
	   }
	   if(dp[m][n]==-1){
	       return 0;
	   }
	   dp[m][n]=1;
	   for(int i=m;i>=1;i--){
	       for(int j=n;j>=1;j--){
	            if(dp[i][j]==-1){
	                   dp[i][j]=0;
	            }
	            else if(i==m){
	               if(j+1<=n&&dp[i][j+1]==1)
	                   dp[i][j]=1;
	                   
	            }
	           else if(j==n){
	               if(i+1<=m&&dp[i+1][j]==1)
	                   dp[i][j]=1;
	           }
	           else
	               dp[i][j]=(dp[i+1][j]%mod+dp[i][j+1]%mod)%mod;
	       }
	   }
	   return (int)dp[1][1];
	}
0
rainx1 month ago
JUST A SIMPLE DFS+MEMO DOES THE JOB
 
 
Chuck all blocked Cells inside a set for O(1) access, and then just run a DFS and get the answer
 
class Solution {
public:
    int dp[501][501];
    int helper(int x, int y, set<pair<int,int>> &blockPoint){
        if(x<0 || y<0 || blockPoint.find({x,y})!=blockPoint.end()){
            return 0;
        }    
        if(x==0 && y==0){
            return 1;
        }
        if(dp[x][y]!=-1){
            return dp[x][y]; 
        }
        return dp[x][y]=(helper(x-1,y,blockPoint)+helper(x,y-1,blockPoint))%1000000007;
    }
    
	int FindWays(int n, int m, vector<vector<int>> blocked_cells){
	    set<pair<int,int>> blockedCell;
	    memset(dp,-1,sizeof(dp));
	    for(auto blockPoint: blocked_cells){
	        blockedCell.insert({blockPoint[0]-1,blockPoint[1]-1});
	    }
	    return helper(n-1,m-1,blockedCell);
	}
};
0
xiome5551 month ago
very east tabulation approach/c++
steps:
 1→Initilize the corner of 2d-table (n-1th row and m-1th  column).
   mark it 1(as ways to reach cell[n-1][m-1] is 1) till the cell encountered is blocked,after tht mark it -1(as path is blocked).
2→iterate it as usual dp problem from down-up approach as shown in code.
code..
class Solution {public:int dp[501][501];   int FindWays(int n, int m, vector<vector<int>>bc){    // Code here    memset(dp,0,sizeof(dp));         for(int i=0;i<bc.size();i++)    {        dp[bc[i][0]-1][bc[i][1]-1]=-1;    }    if(dp[0][0]==-1)    return 0;   for(int i=m-1;i>=0;i--)   {       if(dp[n-1][i]==-1)       {           while(i--)           {               dp[n-1][i]=-1;           }       }       else       dp[n-1][i]=1;   }   for(int i=n-1;i>=0;i--)   {       if(dp[i][m-1]==-1)       {           while(i--)           {               dp[i][m-1]=-1;           }       }       else       dp[i][m-1]=1;   }   if(dp[0][0]==-1)   return 0; for(int i=n-2;i>=0;i--) {     for(int j=m-2;j>=0;j--)     {        if(dp[i][j]==-1)        dp[i][j]=-1;                else        dp[i][j]=((dp[i+1][j]==-1?0:dp[i+1][j])+(dp[i][j+1]==-1?0:dp[i][j+1]))%1000000007;     } }
  return dp[0][0]%1000000007;}};
0
sourav1919sharma1 month ago
const unsigned int mod = 1e9 + 7;
    
	int FindWays(int n, int m, vector<vector<int>>arr){
	    vector<vector<int>>temp(n, vector<int>(m, 0));
	    for(int i = 0; i < arr.size(); i++){
	        temp[arr[i][0] - 1][arr[i][1] - 1] = -1;
	    }
	    
	    int dp[n][m];
	    
	    for(int i = 0; i < n; i++){
	        for(int j = 0; j < m; j++){
	            if(temp[i][j] == -1){
	                dp[i][j] = 0;
	            }
	            
	            else if(i == 0 and j == 0){
	                dp[i][j] = 1;
	            }
	            
	            else{
	                int right = 0, down = 0;
	                if(j > 0)
	                    right = dp[i][j - 1];
	                    
	               if(i > 0)
	                    down = dp[i - 1][j];
	                    
	               dp[i][j] = (right + down) % mod;
	            }
	        }
	    }
	    
	    return dp[n - 1][m - 1];
	}
0
imaniket1 month ago
✅C++ Most Optimized Solution | DP | Tabulation ✅
int mod= 1e9+7;
	int FindWays(int n, int m, vector<vector<int>>blocked_cells){
	   map<pair<int, int>, bool> mp;
	   vector<vector<int>> dp(n, vector<int>(m, 0));
	   for(auto i: blocked_cells) mp[{i[0]-1, i[1]-1}]=1;
	   for(int i=0; i<n; i++){
	       for(int j=0; j<m; j++){
	           if(mp[{i, j}]==1) {
	               dp[i][j]=0;
	           }
	           else if(i==0 && j==0){
	               dp[i][j]=1;
	           }
	           else{
	               if(i>0) dp[i][j] += dp[i-1][j];
	               if(j>0) dp[i][j] += dp[i][j-1];
	           }
	       }
	   }
	   return dp[n-1][m-1]%mod;
	}
0
vaibhavjack1 month ago
May it looks like an alien solution but its just BFSC++    bool isValid(int x,int y,int n,int m,vector<vector<int>> db)   {       if(x>n||y>m)       return false;              if(db[x][y]<0)       return false;              return true;   }      int FindWays(int n, int m, vector<vector<int>>blocked_cells){        vector<vector<int>> db(n+1,vector<int>(m+1,0));    queue<pair<int,int>> Q1;    queue<pair<int,int>> Q;    queue<pair<int,int>> Q2;    pair<int,int> t;        Q1.push({1,1});    db[1][1]++;    for(int i=0;i<blocked_cells.size();i++)    {        db[blocked_cells[i][0]][blocked_cells[i][1]]--;    }            while(!Q1.empty())    {        Q2=Q1;        Q1=Q;        int flag=0;        while(!Q2.empty())        {            t=Q2.front();            Q2.pop();            int a=t.first,b=t.second;            if(isValid(a+1,b,n,m,db))            {                if((db[a+1][b]+db[a][b])>=1000000007)                db[a+1][b]-=1000000007;                                db[a+1][b]+=db[a][b];                if(Q1.empty()==false)                {                    if(Q1.back().first!=(a+1) && Q1.back().second!=b)                    Q1.push({a+1,b});                }                else                {                    Q1.push({a+1,b});                }            }            if(isValid(a,b+1,n,m,db))            {                if((db[a][b+1]+db[a][b])>=1000000007)                db[a][b+1]-=1000000007;                                db[a][b+1]+=db[a][b];                if(Q1.empty()==false)                {                    if(Q1.back().first!=a && Q1.back().second!=(b+1))                    Q1.push({a,b+1});                }                else                {                    Q1.push({a,b+1});                }            }                    }
    }        if(db[n][m]<0)    return 0;        return db[n][m];    } 
0
mohitchandra7231 month ago
JAVA Solution (n*m): 
 
class Solution{   public int FindWays(int n, int m, int[][] blockedCell)   {       int mod = 1000000007;       int matrix[][] = new int[n+1][m+1];
       //marking invalid cell in dp matrix       for(int i = 0; i < blockedCell.length; i++){           matrix[blockedCell[i][0]][blockedCell[i][1]] = -1;       }
       //initial fill       for(int i = 0; i <= n; i++){           if(matrix[i][1] != -1) matrix[i][1] = 1;       }       for(int j = 0; j <= m; j++){           if(matrix[1][j] != -1) matrix[0][1] = 1;       }
       //dp work       for(int i = 1; i <= n; i++){           for (int j = 1; j <= m; j++) {               if(matrix[i][j] == -1){                   continue;               }               if(matrix[i-1][j] != -1 && matrix[i][j-1] != -1){                   matrix[i][j] = (matrix[i-1][j] + matrix[i][j-1])%mod;               }else if(matrix[i-1][j] != -1){                   matrix[i][j] = matrix[i-1][j];               }else if(matrix[i][j-1] != -1){                   matrix[i][j] = matrix[i][j-1];               }else{                   matrix[i][j] = -1;               }           }       }
       //handling 1 base condition       if(matrix[n][m] < 0){           return 0;       }else{           return matrix[n][m];       }          }}
+1
imohdalam1 month ago
Java | Time: O(n*m) | Space: O(n*m)
 
class Solution
{
    public int FindWays(int n, int m, int[][] blocked_cells)
    {
        int[][] matrix = new int[n][m];
        
        for(int i=0; i<n; i++)
          for(int j=0; j<m; j++)
             matrix[i][j] = -1;
        
        for(int i=0; i<blocked_cells.length; i++)
           matrix[blocked_cells[i][0] - 1][blocked_cells[i][1] - 1] = 0;
         
         if(matrix[n-1][m-1] == 0)
            return 0;
        else{
            matrix[n-1][m-1] = 1;
            for(int i=n-2; i>=0; i--){
                if(matrix[i][m-1] != 0 && matrix[i+1][m-1] != 0)
                    matrix[i][m-1] = 1;
                if(matrix[i][m-1] != 0 && matrix[i+1][m-1] == 0)
                    matrix[i][m-1] = 0;
            }
            
            for(int i=m-2; i>=0; i--){
                if(matrix[n-1][i] != 0 && matrix[n-1][i+1] != 0)
                     matrix[n-1][i] = 1;
                if(matrix[n-1][i] != 0 && matrix[n-1][i+1] == 0)
                     matrix[n-1][i] = 0;
            }
            
            for(int i=n-2; i>= 0; i--){
                for(int j=m-2; j>=0; j--){
                    if(matrix[i][j] == 0)
                       continue;
                    matrix[i][j] = (matrix[i+1][j] + matrix[i][j+1])%1000000007;
                }
            }
            return matrix[0][0];
        }
        
    }
}
 
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.