Jieunny์˜ ๋ธ”๋กœ๊ทธ

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์•ˆ์ „์ง€๋Œ€ ๋ณธ๋ฌธ

Study/Coding Test

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์•ˆ์ „์ง€๋Œ€

Jieunny 2023. 2. 3. 14:48

๐Ÿ“Œ  ๋ฌธ์ œ

๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ๊ณผ ์ง€๋ขฐ์— ์ธ์ ‘ํ•œ ์œ„, ์•„๋ž˜, ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  ์นธ์„ ๋ชจ๋‘ ์œ„ํ—˜์ง€์—ญ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค.


์ง€๋ขฐ๋Š” 2์ฐจ์› ๋ฐฐ์—ด board์— 1๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๊ณ  board์—๋Š” ์ง€๋ขฐ๊ฐ€ ๋งค์„ค ๋œ ์ง€์—ญ 1๊ณผ, ์ง€๋ขฐ๊ฐ€ ์—†๋Š” ์ง€์—ญ 0๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
์ง€๋ขฐ๊ฐ€ ๋งค์„ค๋œ ์ง€์—ญ์˜ ์ง€๋„ board๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์•ˆ์ „ํ•œ ์ง€์—ญ์˜ ์นธ ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


์ œํ•œ์‚ฌํ•ญ

  • board๋Š” n * n ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • 1 ≤ n ≤ 100
  • ์ง€๋ขฐ๋Š” 1๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • board์—๋Š” ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ 1๊ณผ ์ง€๋ขฐ๊ฐ€ ์—†๋Š” ์ง€์—ญ 0๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ’ก  ์•„์ด๋””์–ด

board์™€ ๋˜‘๊ฐ™์€ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค.

board๋ฅผ ๋Œ๋ฉด์„œ 1์ด๋ฉด ๋งŒ๋“  ๋ฐฐ์—ด์˜ ๊ทธ ์š”์†Œ์˜ ์ƒ, ํ•˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„ ์„ 1๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

์ด๋•Œ, index๊ฐ€ 0๋ณด๋‹ค ์ž‘์•„์ง€๊ฑฐ๋‚˜, ๋ฐฐ์—ด์˜ size๋ณด๋‹ค ์ปค์ง€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์œผ๋ฏ€๋กœ if ๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•ด์ค€๋‹ค.

 

โœ๏ธ  ํ’€์ด

function solution(board) {
  var answer = 0;
  let size = board.length;
  let safeZone = new Array(size)
  for(let i=0; i<safeZone.length; i++){
    safeZone[i] = new Array(size);
    safeZone[i].fill(0);
  }

  for(let i=0; i<board.length; i++){
    for(let j=0; j<board[i].length; j++){
      if(board[i][j] === 1){
        safeZone[i][j] = 1;
        if(i-1 >= 0){
          safeZone[i-1][j] = 1;
        }
        if(j-1 >= 0){
          safeZone[i][j-1] = 1;
        }
        if(i-1 >= 0 && j-1 >= 0){
          safeZone[i-1][j-1] = 1;
        }
        if(i+1 < size){
          safeZone[i+1][j] = 1;
        }
        if(j+1 < size){
          safeZone[i][j+1] = 1;
        }
        if(i+1 < size && j+1 < size){
          safeZone[i+1][j+1] = 1;
        }
        if(i-1 >= 0 && j+1 < size){
          safeZone[i-1][j+1] = 1;
        }
        if(i+1 < size && j-1 >= 0){
          safeZone[i+1][j-1] = 1;
        }
      }
    }
  }

  for(let i=0; i<safeZone.length; i++){
    for(let j=0; j<safeZone[i].length; j++){
      if(safeZone[i][j] === 0){
        answer++;
      }
    }
  }

  return answer;
}

โžฐ ๋‚ด๊ฐ€ ํ–ˆ์ง€๋งŒ ์ด๊ฒŒ ๋งž๋‚˜ ์‹ถ๋‹ค..;

 

โœ๏ธ  ๋‚จ์˜ ํ’€์ด (์™„์ „ํƒ์ƒ‰ ์‚ฌ์šฉ)

function solution(board) {
  const N = board.length
  const dx = [-1, 1, 0, 0, -1, -1, 1, 1]
  const dy = [0, 0, -1, 1, -1, 1, -1, 1]

  // ์ง€๋ขฐ๊ฐ€ ์„ค์น˜๋œ ๊ณณ
  const booms = []
  for (let x = 0; x < N; x++) {
    for (let y = 0; y < N; y++) {
      if (board[x][y] === 1) {
        booms.push([x, y])
      }
    }
  }

  // ์ง€๋ขฐ๊ฐ€ ์„ค์น˜๋œ ๊ณณ ์ฃผ๋ณ€์— ํญํƒ„ ์„ค์น˜
  booms.forEach(([x, y]) => {
    for (let i = 0; i < 8; i++) {
      const nx = x + dx[i]
      const ny = y + dy[i]
      if (0 <= nx && nx < N && 0 <= ny && ny < N) {
        board[nx][ny] = 1
      }
    }
  })

  // ํญํƒ„์ด ์„ค์น˜๋˜์ง€ ์•Š์€ ๊ณณ๋งŒ ์นด์šดํŒ…
  let count = 0
  for (let x = 0; x < N; x++) {
    for (let y = 0; y < N; y++) {
      if (board[x][y] === 0) {
        count++
      }
    }
  }

  return count
}

โžฐ ํ›จ์”ฌ ๋ณด๊ธฐ์ข‹๋‹ค..