[백준 10026번] BFS(너비 우선 탐색) - 적록색약

김민지·2023년 8월 2일
0

냅다 시작 백준

목록 보기
70/118

✨ 문제 ✨


✨ 정답 ✨

const { count } = require("console");
const fs = require("fs");
const { nextTick } = require("process");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
let input = fs.readFileSync(filePath).toString().trim();


// const fs = require('fs'); 
// let input = fs.readFileSync("/dev/stdin").toString().trim();

input = input.split('\n')
const N = +input.shift()

input = input.map((el) => el.trim().split(''))

// 적록색약 표
let RGBInput = input.map((el) => el.map((el2) => el2 === "G" ? el2 = "R" : el2))


let dx = [-1, 0, 1, 0]
let dy = [0, -1, 0, 1]

const solution = (startX, startY, color, inputArray, visitedArray) => {
  let queue = [[startX, startY]]
  while (queue.length>0) {
    let [currentX, currentY] = queue.shift();
    if (visitedArray[currentX][currentY] === true) {
      continue;
    }
    visitedArray[currentX][currentY] = true;

    for (let i = 0; i < 4; i++) {
      let nextX = currentX + dx[i];
      let nextY = currentY + dy[i];
      if (nextX < 0 || nextY < 0 || nextX >= N || nextY >= N) {
        continue;
      }
      if (visitedArray[nextX][nextY] === true) {
        continue;
      }
      if (color === "R" && inputArray[nextX][nextY] === "R") {
        queue.push([nextX, nextY]);
      }
      if (color === 'G' && inputArray[nextX][nextY] === 'G') {
        queue.push([nextX, nextY])
      }
      if (color === 'B' && inputArray[nextX][nextY] === 'B') {
        queue.push([nextX, nextY])
      }

    }
  }

}

let visited = Array.from({ length: N }, () => new Array(N).fill(false))
let answer = 0;
for (let i = 0; i < N; i++) {
  for (let j = 0; j < N; j++) {
    if (visited[i][j]) {
      continue;
    }
    answer++;
    solution(i, j, input[i][j], input, visited)
  }
}


let RGBvisited = Array.from({ length: N }, () => new Array(N).fill(false))
let RGBAnswer = 0;
for (let i = 0; i < N; i++) {
  for (let j = 0; j < N; j++) {
    if (RGBvisited[i][j]) {
      continue;
    }
   RGBAnswer++;

     solution(i, j,  RGBInput[i][j], RGBInput, RGBvisited)

  }
}
console.log(answer, RGBAnswer)

🧵 참고한 정답지 🧵

💡💡 기억해야 할 점 💡💡

뭔가가 이상하다...

profile
이건 대체 어떻게 만든 거지?

0개의 댓글