-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTest66.java
28 lines (24 loc) · 916 Bytes
/
Test66.java
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
package offer;
import java.util.ArrayList;
// 机器人的运动范围
public class Test66 {
ArrayList<String> list = new ArrayList<>();
public int movingCount(int threshold, int rows, int cols) {
return stat(threshold, rows, cols, 0, 0);
}
private int stat(int threshold, int rows, int cols, int row, int col) {
if (row % 10 + row / 10 + col % 10 + col / 10 > threshold) {
return 0;
}
if (row < 0 || row >= rows || col < 0 || col >= cols) {
return 0;
}
String key = row + " " + col;
if (list.contains(key)) {
return 0;
} else {
list.add(key);
}
return 1 + stat(threshold, rows, cols, row + 1, col) + stat(threshold, rows, cols, row - 1, col) + stat(threshold, rows, cols, row, col + 1) + stat(threshold, rows, cols, row, col - 1);
}
}