-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdp_unique_path_2.cpp
More file actions
111 lines (98 loc) · 2.18 KB
/
dp_unique_path_2.cpp
File metadata and controls
111 lines (98 loc) · 2.18 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <vector>
#include <iostream>
#include <string.h>
using namespace std;
class Solution {
public:
/**
* @param obstacleGrid: A list of lists of integers
* @return: An integer
*/
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
// write your code here
int m = obstacleGrid.size();
if(m == 0)
{
return 0;
}
int n = obstacleGrid[0].size();
//allocat memory
int ** f = new int* [m];
for(int i=0; i < m; i++)
{
f[i] = new int[n];
memset(f[i], 0, n);
}
for(int j = 0; j < n; j++)
{
// initialize th first line
if(obstacleGrid[0][j] != 0)
{
break;
}
f[0][j] = 1;
}
for(int i = 0; i < m; i++)
{
// initialize th last line
if(obstacleGrid[i][0] != 0)
{
break;
}
f[i][0] = 1;
}
// f[x][y] indicates path num from point(0,0) to (x,y).
for(int i = 1; i < m; i++)
{
for(int j = 1; j < n; j++)
{
if(obstacleGrid[i][j])
{
f[i][j] = 0;
}
else
{
f[i][j] = f[i-1][j] + f[i][j-1];
}
}
}
int path = f[m-1][n-1];
// free memory
for(int i = 0; i < m; i++)
{
delete[] f[i];
}
delete [] f;
return path;
}
};
vector<int> gen_vector(int *a, int n)
{
vector<int> v;
for(int i = 0; i< n; i++)
{
v.push_back(a[i]);
}
return v;
}
int main(void)
{
int array[8][7] = {
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{1,0,0,0,0,0,1},
{0,0,0,0,0,0,0},
{0,1,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{1,0,0,0,0,0,0},
};
Solution mysol;
vector<vector<int> > my_vv;
for(int i = 0; i < 8; i++)
{
my_vv.push_back(gen_vector(&array[i][0], 7));
}
int paths = mysol.uniquePathsWithObstacles(my_vv);
cout << "The path num is : "<<paths<<endl;
}