-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.2.js
More file actions
36 lines (32 loc) · 746 Bytes
/
problem.2.js
File metadata and controls
36 lines (32 loc) · 746 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
33
34
35
36
const dx = [1, 1, -1, -1];
const dy = [-1, 1, 1, -1];
function mark(map, x, y) {
map[y][x] = false;
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 4; j++) {
const nx = x + i * dx[j];
const ny = y + i * dy[j];
if (nx < 0 || nx >= 8 || ny < 0 || ny >= 8) {
continue;
}
map[ny][nx] = false;
}
}
}
function solution(bishops) {
let map = new Array(8).fill(0).map(item => new Array(8).fill(true));
bishops.map(bishop => {
const x = bishop[0].charCodeAt(0) - "A".charCodeAt(0);
const y = parseInt(bishop[1])-1;
mark(map, x, y);
});
let answer = 0;
map.forEach(row => {
row.forEach(item => {
if (item) {
answer++;
}
});
});
return answer;
}