-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_unique_path_in_grid.cpp
More file actions
145 lines (126 loc) · 3.12 KB
/
count_unique_path_in_grid.cpp
File metadata and controls
145 lines (126 loc) · 3.12 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// https://www.interviewbit.com/problems/unique-paths-in-a-grid/
int Solution::uniquePathsWithObstacles(vector<vector<int> > &A) {
int n1 = A.size();
int n2 = A[0].size();
int T[n1][n2];
memset(T, 0, sizeof(T));
for(int i=0; i<n1; i++){
for(int j=0; j<n2; j++){
if(i==0 && j==0 && A[i][j] != 1) T[i][j]=1;
else if(i==0){
if(A[i][j]!=1 && T[i][j-1]!=0){
T[i][j] = 1;
}
}
else if(j==0){
if(A[i][j]!=1 && T[i-1][j]!=0){
T[i][j] = 1;
}
}
else if(A[i][j] != 1){
if(A[i-1][j] == 1 && A[i][j-1]){
continue;
}
else if(A[i-1][j] == 1){
T[i][j] = T[i][j-1];
}
else if(A[i][j-1] == 1){
T[i][j] = T[i-1][j];
}
else{
T[i][j] = T[i-1][j] + T[i][j-1];
}
}
}
}
// for(int i=0; i<n1; i++){
// for(int j=0; j<n2; j++){
// cout<<T[i][j]<<" ";
// }
// cout<<endl;
// }
return T[n1-1][n2-1];
}
// https://www.interviewbit.com/problems/grid-unique-paths/
#include <bits/stdc++.h>
using namespace std;
#define loop(i, start, end) for(int i=start; i<end; i++)
// int Solution::uniquePaths(int A, int B) {
// int path[A][B] = {0};
// for(int i=0; i<A; i++){
// for(int j=0; j<B; j++){
// if(i==0 || j==0){
// path[i][j] = 1;
// }
// else{
// path[i][j] = path[i-1][j] + path[i][j-1];
// }
// }
// }
// return path[A-1][B-1];
// }
void count_unique_path(int A, int B){
int path[A][B] = {0};
loop(i, 0, A){
loop(j, 0, B){
if(i==0 || j==0){
path[i][j] = 1;
}
else{
path[i][j] = path[i-1][j] + path[i][j-1];
}
}
}
loop(i, 0, A){
loop(j, 0, B){
cout<<path[i][j]<<" ";
}
cout<<endl;
}
}
void optimal_sol(int *path, int row, int col){
if(row==1 ||col==1){
*((path+row*col)+col) = 1;
}
else{
int a, b;
// if(*((path+(row-1)*col)+col) != 0){
// a = *((path+(row-1)*col)+col);
// }
// else{
if(*((path+(row-1)*col)+col) != 0){
// *((path+(row-1)*col)+col) = optimal_sol(path, row-1, col);
optimal_sol(path, row-1, col);
}
// if(*((path+row*(col-1))+(col-1)) != 0){
// b = *((path+row*(col-1))+(col-1));
// }
// else{
if(*((path+row*(col-1))+(col-1)) != 0){
// *((path+row*(col-1))+(col-1)) = optimal_sol(path, row, col-1);
optimal_sol(path, row, col-1);
}
*((path+row*col)+col) = *((path+(row-1)*col)+col) + *((path+row*(col-1))+(col-1));
}
}
int main()
{
int test;
cin>>test;
while(test--){
int A, B;
cin>>A>>B;
// count_unique_path(A, B);
int path_matrix[A][B];
memset(path_matrix, 0, sizeof(path_matrix));
optimal_sol(path_matrix, A, B);
loop(i, 0, A){
loop(j, 0, B){
cout<<path_matrix[i][j]<<" ";
}
cout<<endl;
}
cout<<path_matrix[A-1][B-1]<<endl;
}
return 0;
}