-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-087-knightlyNumber.js
52 lines (46 loc) · 1.39 KB
/
structy-087-knightlyNumber.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
50
51
52
// p: num, num, num, num, num
// r: num // null
// e:
// x o x o x x x x
// o x x x o x x x
// x x O x x x x x
// o x x x o x x x
// x o x o x x x x
// x x x x x x x x
// x x x x x P x x
// x x x x x x x x
//
// o x x
// x x x
// x 1 x
// [ y + 2,x + 1 ],
// [ y + 1,x + 2 ],
// [ y - 1,x + 2 ],
// [ y - 2,x + 1 ],
// [ y - 2,x - 1 ],
// [ y - 1,x - 2 ],
// [ y + 1,x - 2 ],
// [ y + 2,x - 1 ],
// dfs O(rxc) O(rxc)
const knightlyNumber = (n, m, kr, kc, pr, pc, memo = {}) => {
const move = kr + "," + kc + "," + m;
if (move in memo) return memo[move];
const boundY = 0 <= kr && kr < n;
const boundX = 0 <= kc && kc < n;
if (!boundY || !boundX) return 0;
if (m === 0 && kr === pr && kc === pc) return 1;
if (m < 0) return 0;
const a = knightlyNumber(n, m - 1, kr + 2, kc + 1, pr, pc, memo);
const b = knightlyNumber(n, m - 1, kr + 1, kc + 2, pr, pc, memo);
const c = knightlyNumber(n, m - 1, kr - 1, kc + 2, pr, pc, memo);
const d = knightlyNumber(n, m - 1, kr - 2, kc + 1, pr, pc, memo);
const e = knightlyNumber(n, m - 1, kr - 2, kc - 1, pr, pc, memo);
const f = knightlyNumber(n, m - 1, kr - 1, kc - 2, pr, pc, memo);
const g = knightlyNumber(n, m - 1, kr + 1, kc - 2, pr, pc, memo);
const h = knightlyNumber(n, m - 1, kr + 2, kc - 1, pr, pc, memo);
memo[move] = a + b + c + d + e + f + g + h;
return memo[move];
};
module.exports = {
knightlyNumber,
};