Skip to content

Commit bd3e69a

Browse files
committed
Create 剑指 Offer 13. 机器人的运动范围.js
1 parent d31f1bf commit bd3e69a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @param {number} m
3+
* @param {number} n
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var movingCount = function (m, n, k) {
8+
function getSum(num) {
9+
let result = 0;
10+
while (num) {
11+
result += num % 10;
12+
num = Math.floor(num / 10);
13+
}
14+
return result;
15+
}
16+
17+
let baseI = -1;
18+
let baseJ = -1;
19+
const map = [];
20+
for (let i = 0; i < m; i++) {
21+
map[i] = new Array(n).fill(1);
22+
if (i % 10 === 0) {
23+
baseI++;
24+
}
25+
baseJ = -1;
26+
for (let j = 0; j < n; j++) {
27+
if (j % 10 === 0) {
28+
baseJ++;
29+
}
30+
if (baseI + (i % 10) + baseJ + (j % 10) > k) {
31+
map[i][j] = 0;
32+
}
33+
}
34+
}
35+
36+
const directions = [
37+
[1, 0],
38+
[0, 1],
39+
];
40+
let result = 0;
41+
function bfs(i, j) {
42+
if (map[i] && map[i][j]) {
43+
map[i][j] = 0;
44+
result++;
45+
for (const [di, dj] of directions) {
46+
bfs(i + di, j + dj);
47+
}
48+
}
49+
return result;
50+
}
51+
return bfs(0, 0);
52+
};

0 commit comments

Comments
 (0)