forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT66.java
More file actions
32 lines (28 loc) · 978 Bytes
/
T66.java
File metadata and controls
32 lines (28 loc) · 978 Bytes
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
package web;
/**
* @program LeetNiu
* @description: 机器人的运动范围
* @author: mf
* @create: 2020/01/17 23:46
*/
public class T66 {
public int movingCount(int threshold, int rows, int cols) {
boolean[][] visited = new boolean[rows][cols];
return countingStep(threshold,rows,cols,0,0,visited);
}
public int countingStep(int limit, int rows, int cols, int r, int c, boolean[][] visited) {
if (r < 0 || r >= rows || c < 0 || c >= cols || visited[r][c] || bitSum(r) + bitSum(c) > limit) {
return 0;
}
visited[r][c] = true;
return countingStep(limit,rows,cols,r - 1,c,visited) + countingStep(limit,rows,cols,r,c - 1,visited)+countingStep(limit,rows,cols,r+1,c,visited)+countingStep(limit,rows,cols,r,c+1,visited) + 1;
}
public int bitSum(int t) {
int count = 0;
while (t != 0) {
count += t % 10;
t /= 10;
}
return count;
}
}