-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountPaths.cpp
87 lines (83 loc) · 2.74 KB
/
CountPaths.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <algorithm>
using namespace std;
static const int dRow = 7;
static const int dCol = 7;
static const int ROWS = 8;
static const int COLS = 8;
static void printArrays(int grid [ROWS][COLS] ) {
for (int r =0 ; r <ROWS ; r++) {
for(int c = 0; c <COLS; c++) {
printf("%4d",grid[r][c]);
}
cout<<endl;
}
}
static int countAllPaths( bool (&grid) [ROWS][COLS], int row, int col) {
if( (row >=ROWS || col >= COLS) || !grid[row][col])
return 0;
else if(row == dRow && col == dCol)
return 1;
else
return countAllPaths(grid, row+1, col) +
countAllPaths(grid, row, col+1);
}
static int countAllPathsMemo(bool (&grid) [ROWS][COLS], int row, int col, int paths[][COLS]){
if( (row >=ROWS || col >= COLS) || !grid[row][col])
return 0;
else if(row == dRow && col == dCol)
return 1;
else if(paths[row] [col] ==0)
paths[row] [col] = countAllPathsMemo(grid, row+1, col, paths) +
countAllPathsMemo(grid, row, col+1,paths);
return paths[row][col];
}
static int countAllPathsDP(bool (&grid) [ROWS][COLS]){
int count [ROWS][COLS] = {0};
count[dRow][dCol] = 1;
for (int row = ROWS-1 ; row >=0 ; row -- ){
for (int col = COLS -1 ; col >=0 ; col -- ){
if((row != dRow || col!= dCol) && grid[row][col]) {
if (row +1 <ROWS)
count[row][col]+=count[row+1][col];
if(col+1 < COLS)
count[row][col]+=count[row][col+1];
}
}
}
printArrays(count);
return count[0][0];
}
int main() {
bool grid[ROWS][COLS] = {
{
true,true,true,true,true,true,true,true,
},
{
true,true,false,true,true,true,false,true,
},
{
true,true,true,true,false,true,true,true
},
{
false,true,false,true,true,false,true,true
},
{
true,true,false,true,true,true,true,true
},
{
true,true,true,false,false,true,false,true
},
{
true,false,true,true,true,false,true,true
},
{
true,true,true,true,true,true,true,true
}
};
cout<<countAllPaths(grid, 0, 0) <<endl;
int paths [ROWS][COLS] = {0};
cout<<countAllPathsMemo(grid,0,0, paths)<<endl;
cout<<countAllPathsDP(grid);
return 0;
}