- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHowManyPaths.java
More file actions
Latest commit
41 lines (37 loc) · 1.31 KB
/
Copy pathHowManyPaths.java
File metadata and controls
41 lines (37 loc) · 1.31 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
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.StringTokenizer;
publicclassHowManyPaths {
staticintn, m;
staticintDP[][];
staticintarr[][];
staticint[][] obstacles = {{1, 2}, {3, 2}, {1, 4}};
staticbooleanisValid(inta, intb) {
return0<=a && a < n && 0<=b && b < m;
}
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
StringTokenizerst = newStringTokenizer(br.readLine(), " ");
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
arr = newint[n][m];
DP = newint[n][m];
for (inti = 0; i < obstacles.length; i++) {
arr[obstacles[i][0]][obstacles[i][1]] = 1;
}
howManyPathsAreThere(arr, 0, 0);
System.out.print(DP[0][0]);
}
publicstaticinthowManyPathsAreThere(intarr[][], intr, intc) {
if (!isValid(r, c) || arr[r][c] == 1)
return0;
if (r == n-1 && c == m-1)
return1;
// memoization
if (DP[r][c] != 0)
returnDP[r][c];
DP[r][c] = howManyPathsAreThere(arr, r+1, c) + howManyPathsAreThere(arr, r, c+1);
returnDP[r][c];
}
}