forked from webVueBlog/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path机器人的运动范围.js
49 lines (45 loc) · 1.1 KB
/
机器人的运动范围.js
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
/**
* @param {number} m
* @param {number} n
* @param {number} k
* @return {number}
* dfs 往下或往右
*/
var movingCount = function(m, n, k) {
let count = 0;
const visited = new Array(m).fill(0).map(() => new Array(n).fill(false));
const ways = [
[0, 1],
[1, 0]
]
const calcSum = (num) => {
// 15
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
return sum;
}
const crossBorder = (x, y) => {
return x < 0 || x >= m || y < 0 || y >= n;
}
const dfs = (x, y) => {
if (visited[x][y]) return;
visited[x][y] = true;
if (calcSum(x) + calcSum(y) <= k) {
count++;
} else {
return;
}
for (const way of ways) {
const [step_x, step_y] = way;
const next_x = x + step_x;
const next_y = y + step_y;
if (crossBorder(next_x, next_y) || visited[next_x][next_y]) continue;
dfs(next_x, next_y);
}
}
dfs(0, 0);
return count;
};