function solution(board) {
const n = board.length;
const direction = [[-1, -1],[-1, 0],[0, -1],[-1, 1], [1, -1], [1, 0], [0, 1], [1, 1]] // 방향 정의
for(let y = 0; y < n; y ++){
for(let x = 0; x < n; x++){
if(board[y][x] === 1){
direction.forEach((d) => {
let [dy, dx] = d;
let ny = y + dy;
let nx = x + dx;
// board 내부에 존재 하는지, 지뢰인 곳이 아닌지
if(ny >= 0 && nx >= 0 && ny < n && nx < n && board[ny][nx] === 0 ){
board[ny][nx] = 2;
}
})
}
}
}
return board.flat().filter(n => n === 0).length;
}
function solution(board) {
const dir = [[-1, 0], [0, -1], [1, 0], [0, 1], [-1, 1], [1, -1], [1, 1], [-1, -1]];
let answer = 0;
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === 1) {
dir.map((item) => {
const x = item[0] + i;
const y = item[1] + j;
if (x >= 0 &&
x < board.length &&
y >= 0 &&
y < board[i].length &&
board[x][y] === 0) {
board[x][y] = 'x';
}
})
}
}
}
board.map((item1) => item1.map((item2) => item2 === 0 ? answer++ : 0));
return answer;
}
function solution(board) {
//위험지역 조사를 위해서 각 방향을 먼전 설정해준다.
const dangerZone= [[-1,-1], [0,-1], [1,-1], [-1,0], [1,0], [-1,1], [0,1],[1,1]];
const n =board.length;
// 보드크기만큼 탐색을 해주고 1,지뢰를 만나면 각 방향을 보드크기안에서 탐색을 해서 0이면 2로 바꿔준다.
for(let i = 0 ; i<n; i++){
for(let j=0; j<n; j++){
let a= board[i][j];
if(a===1){
for(let b of dangerZone){
const [x, y]=b;
const [areaX, areaY]=[j+x,i+y];
if(areaX>=0 && areaX<n && areaY>=0 && areaY<n && board[areaY][areaX]===0){
board[areaY][areaX]=2;
}
}
}
}
}
//다 바꿔준 후에 배열에서 0의갯수를 리턴해주면 된다 .
return board.reduce((acc,cur)=>{
let m =cur.filter(i=>i===0).length;
return acc+m}, 0)
}
function solution(board) {
const directions = [[-1, 1], [0, 1], [1, 1], [-1, 0], [1, 0], [-1, -1], [0, -1], [1, -1]]
const size = board.length;
let result = size * size; // 안전하지 않은 곳들을 차감
for (let row = 0; row < board.length; row++ ) {
for (let col = 0; col < board[row].length; col++) {
const spot = board[row][col];
if (spot === 0) continue;
else if (spot === 1) {
result--; // 지뢰가 있는 곳
directions.forEach(direction => {
const nrow = row + direction[0]
const ncol = col + direction[1]
if (nrow < size && nrow >= 0 && ncol < size && ncol >= 0 && !board[nrow][ncol]) {
board[nrow][ncol] = 2;
result--; // 지뢰 주변
}
})
}
}
}
return result;
}
function solution(board) {
let answer = 0;
const N = board.length;
const d = [[1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0]]; //상하좌우 대각선, 이번엔 순서 상관 X
//배열을 순회하면서 폭탄을 만나게 될 경우 주변 타일을 모두 2로 만들어버립니다.
//이때 원래 있던 폭탄이 지워지지 않도록 주의해야합니다.
for(let y = 0; y < N; y++){
for(let x = 0; x < N; x++){
if(board[y][x] === 1){
for(let [dy, dx] of d){
let ny = y + dy;
let nx = x + dx;
if(ny >= 0 && ny < N && nx >= 0 && nx < N && !board[ny][nx]){
board[ny][nx] = 2;
}
}
}
}
}
// 안전지대는 0입니다. 0의 개수를 세줍니다.
for(let y = 0; y < N; y++){
for(let x = 0; x < N; x++){
if(!board[y][x]){
answer++;
}
}
}
return answer;
}
// 2차원 배열 문제에 익숙해진다면 풀 수 있는 문제가 급격히 늘어납니다.
// 약간 3중 for문이 되어서 조금 아쉽긴한데.. 다른 방법이 떠오르지 않네요
// 이거 0레벨 문제인데 엥간한 1레벨 문제보다 어렵네요 ㅎㅎ
function solution(board) {
let answer = 0;
// 방향 먼저 만들기
const direction = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
const n = board.length;
for(let x= 0; x < n; x++){
for(let y = 0; y < n; y++){
if(board[x][y] === 1){
direction.map((d) =>{
let [dx, dy] = d;
const nx = x + dx;
const ny = y + dy;
// 조건: board 길이 안에 존재하고 또 다른 지뢰의 값이 변경되지 않도록 위치값이 0일때만
if( nx >= 0 && nx < n && ny >= 0 && ny < n && board[nx][ny] === 0){
board[nx][ny] = -1;
}
})
}
}
}
board.forEach(v => v.forEach(s => s === 0 ? answer++: null));
return answer
}