-
Notifications
You must be signed in to change notification settings - Fork 0
/
dp_unique_paths.cpp
73 lines (61 loc) · 1.27 KB
/
dp_unique_paths.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
#include<stdio.h>
#include<iostream>
using namespace std;
class Solution {
public:
int path_count;
void go_down(int x,int y,int m,int n)
{
y = y + 1;
//cout<<"y:"<<y<<"\n";
if(y == m - 1)
{
path_count++;
//if(path_count%100000 == 0)
//cout<<"path count:"<<path_count<<"\n";
}
else
{
go_down(x,y,m,n);
if(x < n - 1)
go_right(x,y,m,n);
}
return;
}
void go_right(int x,int y,int m,int n)
{
x = x + 1;
//cout<<"x:"<<x<<"\n";
if(x == n - 1)
{
path_count++;
//if(path_count%100000 == 0)
//cout<<"path count:"<<path_count<<"\n";
}
else
{
go_right(x,y,m,n);
if(y < m - 1)
go_down(x,y,m,n);
}
return;
}
int uniquePaths(int m, int n) {
path_count = 0;
if(m==1 && n==1)
return 1;
int x=0,y=0;
if(y < m - 1)
go_down(x,y,m,n);
if(x < n - 1)
go_right(x,y,m,n);
return path_count;
}
};
int main()
{
Solution sol;
int cnt=sol.uniquePaths(51,9);
cout<<"path count: "<<cnt<<"\n";
return 0;
}