솔루션
function solution(board) {
const isBombNearby = (r, c) => {
const nearby = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
];
const isInBoard = (r, c) =>
r >= 0 && r < board.length && c >= 0 && c < board.length;
return nearby.some(
([dR, dC]) => isInBoard(r + dR, c + dC) && board[r + dR][c + dC] === 1
);
};
let answer = 0;
for (let r = 0; r < board.length; r++) {
for (let c = 0; c < board.length; c++) {
if (board[r][c] !== 1 && !isBombNearby(r, c)) answer += 1;
}
}
console.log(answer);
return answer;
}
solution([
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
]);
solution([
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
]);
solution([
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
]);
솔루션
function solution2(board) {
let outside = [
[-1, 0],
[-1, -1],
[-1, 1],
[0, -1],
[0, 1],
[1, 0],
[1, -1],
[1, 1],
];
let answer = 0;
board.forEach((row, y, self) =>
row.forEach((it, x) => {
if (it === 1) return false;
return outside.some(([oy, ox]) => !!self[oy + y]?.[ox + x])
? false
: answer++;
})
);
console.log(answer);
return answer;
}