-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexactPaths.js
More file actions
156 lines (104 loc) · 3.05 KB
/
exactPaths.js
File metadata and controls
156 lines (104 loc) · 3.05 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
146
147
148
149
150
151
152
153
154
155
156
// package whatever; // don't place package name!
// https://www.geeksforgeeks.org/number-of-paths-with-exactly-k-coins/
import java.io.*;
import java.util.*;
class MyCode {
public static void main (String[] args) {
// int[][] matrix = new int[][] { {1, 2, 3},
// {4, 6, 5},
// {3, 2, 1}};
int[][] matrix = new int[][] { {1, 2},
{3, 4}};
System.out.println(RobotPaths.exactPath(matrix, 8));
// System.out.println(RobotPaths.compute1(matrix));
// System.out.println(RobotPaths.compute2(matrix));
}
}
/*
[[1,1,1] ,
[0,0,0]]
*/
class RobotPaths {
// public static Integer count = 0;
// public static int[][] matrix;
// public static Integer compute1(int[][] inpMatrix) {
// matrix = inpMatrix;
// traverse(0, 0);
// return count;
// }
// public static void traverse(int x, int y) {
// if (x < 0 || y < 0 || x >= matrix[0].length || y >= matrix.length) {
// return;
// } else if (matrix[y][x] == 1) {
// return;
// } else if (y == matrix.length - 1 && x == matrix[0].length - 1) {
// count++;
// return;
// }
// matrix[y][x] = 1;
// traverse(x+1, y);
// traverse(x-1, y);
// traverse(x, y+1);
// traverse(x, y-1);
// matrix[y][x] = 0;
// }
public static Integer count = 0;
public static int[][] matrix;
public static Integer exactPath(int[][] inpMatrix, int k) {
matrix = inpMatrix;
traverse(0, 0, k, new ArrayList<Integer>());
return count;
}
public static void traverse(int x, int y, int money, ArrayList<Integer> path) {
if (x < 0 || y < 0 || x >= matrix[0].length || y >= matrix.length) {
return;
} else if (matrix[y][x] == 0) {
return;
}
int temp = matrix[y][x];
money -= temp;
if (money < 0) {
return;
}
path.add(temp);
System.out.println(path);
if (y == matrix.length - 1 && x == matrix[0].length - 1) {
if (money == 0) {
System.out.println(money);
System.out.println(path);
count++;
}
path.remove(path.size()-1);
return;
}
matrix[y][x] = 0;
traverse(x+1, y, money, path);
traverse(x-1, y, money, path);
traverse(x, y+1, money, path);
traverse(x, y-1, money, path);
matrix[y][x] = temp;
money += temp;
path.remove(path.size()-1);
}
// public static Integer compute2(int[][] inpMatrix) {
// matrix = inpMatrix;
// return travel(0, 0);
// }
// public static Integer travel(int x, int y) {
// if (x < 0 || y < 0 || x >= matrix[0].length || y >= matrix.length) {
// return 0;
// } else if (matrix[y][x] == 1) {
// return 0;
// } else if (y == matrix.length - 1 && x == matrix[0].length - 1) {
// return 1;
// }
// matrix[y][x] = 1;
// Integer count = 0;
// count += travel(x+1, y);
// count += travel(x-1, y);
// count += travel(x, y+1);
// count += travel(x, y-1);
// matrix[y][x] = 0;
// return count;
// }
}