forked from 20je0928/C-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRat in a maze.cpp
53 lines (45 loc) · 1.22 KB
/
Rat in a maze.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//I am using '-1' for blockages.
#include<bits/stdc++.h>
using namespace std;
int dp[1000][1000]={0};
int numWays( int row, int col){
///base case
if(dp[row][col]==-1){
return -1;
}
// compute no of wawys for first row and column
for(int j=0;j<col;j++){
if(dp[0][j]==-1){break;}//if robot faces any blockagein first row
dp[0][j]=1;
}
for(int i=0;i<row;i++){
if(dp[i][0]==-1){break;}// if robot faces any blockages in first column
dp[i][0]=1;
}
// bottom up approach for the rest of the matrix
for(int i=1;i<row;i++){
for(int j=1;j<col;j++){
// if cell is bolcked leave it
if(dp[i][j]==-1){continue;}
dp[i][j]=0;
if(dp[i][j-1]!=-1){
dp[i][j]=dp[i][j-1];
}
if(dp[i-1][j]!=-1){
dp[i][j] = dp[i][j] + dp[i-1][j];
}
}
}
return dp[row-1][col-1];
}
int main(){
int m,n,p;// no of rows and columns and no of blockages in grid
cin>>m>>n>>p;
for(int i=0;i<p;i++){
int x,y;
cin>>x>>y;
// marked bolcked the given cellls ;
dp[x-1][y-1]=-1;
}
cout<<numWays(m,n)<<endl;
}