-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution13.java
More file actions
47 lines (38 loc) · 1.41 KB
/
Solution13.java
File metadata and controls
47 lines (38 loc) · 1.41 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
class Solution {
public int movingCount(int m, int n, int k) {
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = i; j < n; j++) {
visited[i][j] = false;
}
}
int count = movingCountCore(k, m, n, 0, 0, visited);
return count;
}
private int movingCountCore(int k, int rowLen, int colLen, int row, int col, boolean[][] visited) {
int count = 0;
if (check(k, rowLen, colLen, row, col, visited)) {
visited[row][col] = true;
count = 1 + movingCountCore(k, rowLen, colLen, row + 1, col, visited)
+ movingCountCore(k, rowLen, colLen, row - 1, col, visited)
+ movingCountCore(k, rowLen, colLen, row, col + 1, visited)
+ movingCountCore(k, rowLen, colLen, row, col - 1, visited);
}
return count;
}
private boolean check(int k, int rowLen, int colLen, int row, int col, boolean[][] visited) {
if (row >= 0 && row < rowLen && col >= 0 && col < colLen && !visited[row][col]
&& getDigitSum(row) + getDigitSum(col) <= k) {
return true;
}
return false;
}
private int getDigitSum(int number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
}