-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMaze.cpp
37 lines (36 loc) · 899 Bytes
/
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
#include <bits/stdc++.h>
using namespace std;
int maze(std::vector<std::vector<int> > &v,int rows,int cols){
if(v[0][0] == -1) return 0;
for(int i=0;i<rows;i++){
if(v[i][0] == 0) v[i][0] = 1;
else break;
}
for(int i=1;i<cols;i++){
if(v[0][i] == 0 ) v[0][i] = 1;
else break;
}
for(int i=1;i<rows;i++){
for(int j=1;j<cols;j++){
if(v[i][j] == -1) continue;
if(v[i-1][j] > 0) v[i][j] += v[i-1][j];
if(v[i][j-1] > 0) v[i][j] += v[i][j-1];
}
}
return (v[rows-1][cols-1] > 0) ? v[rows-1][cols-1] : 0 ;
}
//Time Complexity O(r*c)
int main(int argc, char const *argv[])
{
int r,c;
cout << "Enter the number of rows and columns \n" ;
cin >> r >> c;
std::vector< std::vector<int> > v(r,std::vector<int>(c));
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin >> v[i][j];
}
}
cout << maze(v,r,c) << endl;
return 0;
}