KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (509)
    • Project (1)
      • blog (1)
    • CS (1)
    • Web (29)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • Next.js (5)
      • ETC (1)
    • Docker (14)
    • Git (5)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • PS (433)
      • 백준 (187)
      • Programmers (106)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • C
  • js
  • 다이나믹 프로그래밍
  • tree
  • C++
  • 수학
  • 제코베 JS 100제
  • string
  • recursion
  • 문자열
  • 정렬
  • codeup
  • Level1
  • Level 2
  • programmers
  • LeetCode
  • Level 1
  • Level 0
  • 백준
  • 그래프

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/백준

백준 / 그래프 - BFS / 1926번 / 그림 / JS

2022. 10. 27. 02:39

https://www.acmicpc.net/problem/1926

 

1926번: 그림

어떤 큰 도화지에 그림이 그려져 있을 때, 그 그림의 개수와, 그 그림 중 넓이가 가장 넓은 것의 넓이를 출력하여라. 단, 그림이라는 것은 1로 연결된 것을 한 그림이라고 정의하자. 가로나 세로

www.acmicpc.net

 

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : '../input.txt';
// const filePath = process.platform === 'linux' ? '/dev/stdin' : 'BOJ/input.txt';
const input = fs.readFileSync(filePath).toString().trim().split('\n');

const [ROW, COLUMN] = input.shift().split(' ').map(Number);
const PAPER = input.map((el) => {
  return el.split(' ').map(Number);
});

function solution(ROW, COLUMN, PAPER) {
  let visited = Array.from({ length: ROW }, () =>
    new Array(COLUMN).fill(false)
  );

  /**
   * 행과 열을 받아 유효한 인덱스인지 bool값으로 return 해주는 함수
   * 
   * @param {number} row 행
   * @param {number} col 열
   * @returns {boolean} 인덱스가 유효하면 true, 아니면 false return
   */
  const isValidIndex = (row, col) => {
    return 0 <= row && row < ROW && 0 <= col && col <= COLUMN;
  };

  const directionObj = {
    up: [-1, 0],
    down: [1, 0],
    left: [0, -1],
    right: [0, 1],
  };
  const BFS = (row, col) => {
    // 처음 들어온 값을 queue에 넣어줌
    let queue = [[row, col]];
    // 이미 조건을 만족하여 BFS가 실행되는 것이므로 1부터 시작
    let area = 1;

    while (queue.length !== 0) {
      let [x, y] = queue.shift();

      // 들어온 행, 열 값의 상, 하, 좌, 우 검사
      Object.values(directionObj).map((dir) => {
        let [nx, ny] = [x + dir[0], y + dir[1]];
        // 새로운 행, 열 값이 유효한 인덱스인 경우
        if (isValidIndex(nx, ny)) {
          // 1이고 아직 방문하지 않은 값일 경우
          if (PAPER[nx][ny] === 1 && visited[nx][ny] === false) {
            // 방문 처리 해주고,
            visited[nx][ny] = true;
            // 구하려는 넓이에 1을 더해줌
            area += 1;
            // 다시 queue에 넣어줌, BFS
            queue.push([nx, ny]);
          }
        }
      });
    }

    return area;
  };

  let drawingCount = 0;
  let area = 0;
  let maxArea = 0;
  for (let i = 0; i < ROW; i += 1) {
    for (let j = 0; j < COLUMN; j += 1) {
      // 현재 인덱스가 1이고 방문하지 않은 인덱스일 경우
      if (PAPER[i][j] === 1 && visited[i][j] === false) {
        // 방문 처리 해주고,
        visited[i][j] = true;
        // BFS를 통해 구한 넓이값
        area = BFS(i, j);
        // 구한 넓이값들 중 최댓값을 구해준다.
        if (area > maxArea) maxArea = area;
        // 그림의 개수 + 1
        drawingCount += 1;
      }
    }
  }

  console.log(drawingCount);
  console.log(maxArea);
}

solution(ROW, COLUMN, PAPER);
저작자표시 (새창열림)

'PS > 백준' 카테고리의 다른 글

백준 / 이분 탐색 / 16401번 / 과자 나눠주기 / JS  (0) 2022.11.17
백준 / 그래프 - DFS / 13565번 / 침투 / JS  (0) 2022.11.16
백준 / 백트래킹 / 10819번 / 차이를 최대로 / JS  (0) 2022.10.05
백준 / 백트래킹 / 9663번 / N-Queen / JS  (0) 2022.10.02
    'PS/백준' 카테고리의 다른 글
    • 백준 / 이분 탐색 / 16401번 / 과자 나눠주기 / JS
    • 백준 / 그래프 - DFS / 13565번 / 침투 / JS
    • 백준 / 백트래킹 / 10819번 / 차이를 최대로 / JS
    • 백준 / 백트래킹 / 9663번 / N-Queen / JS
    KimMinJun
    KimMinJun

    티스토리툴바