-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutionMethod1.java
More file actions
65 lines (59 loc) · 1.54 KB
/
SolutionMethod1.java
File metadata and controls
65 lines (59 loc) · 1.54 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
package matrixPath;
class SolutionMethod1 {
public int movingCount(int threshold, int rows, int cols) {
boolean[] visted = new boolean[rows * cols];
for (int i = 0; i < visted.length; i++)
visted[i] = false;
int count = movingCountCore(threshold, rows, cols, 0, 0, visted);
return count;
}
/*
* 递归回溯方法:
*
* @param threshold 约束值
*
* @param rows 方格行数
*
* @param cols 方格列数
*
* @param row 当前处理的行号
*
* @param col 当前处理的列号
*
* @param visted 访问标记数组
*
* @return 最多可走的方格
*/
public int movingCountCore(int threshold, int rows, int cols, int row, int col, boolean[] visted) {
int count = 0;
if (check(threshold, rows, cols, row, col, visted)) {
visted[row * cols + col] = true;
count = 1 + movingCountCore(threshold, rows, cols, row - 1, col, visted)
+ movingCountCore(threshold, rows, cols, row, col - 1, visted)
+ movingCountCore(threshold, rows, cols, row + 1, col, visted)
+ movingCountCore(threshold, rows, cols, row, col + 1, visted);
}
return count;
}
boolean check(int threshold, int rows, int cols, int row, int col, boolean[] visted) {
if (row >= 0 && row < rows && col >= 0 && col < cols && (getDigitSum(row) + getDigitSum(col) <= threshold)
&& !visted[row * cols + col])
return true;
return false;
}
/*
* 一个数字的位数之和
*
* @param number 数字
*
* @return 数字的位数之和
*/
public int getDigitSum(int number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
}