Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Python
- 데이터베이스시스템
- Cookie
- 방송대컴퓨터과학과
- 엘리스sw트랙
- aws
- 항해99
- 코드잇
- nestjs
- JavaScript
- HTML
- redis
- CSS
- 유노코딩
- MySQL
- 중간이들
- 코딩테스트준비
- node.js
- presignedurl
- 꿀단집
- SQL
- TiL
- 개발자취업
- 프로그래머스
- 코딩테스트
- 파이썬프로그래밍기초
- 방송대
- Git
- 99클럽
- 파이썬
Archives
- Today
- Total
배꼽파지 않도록 잘 개발해요
99클럽 코테 스터디 31일차 TIL - 영역 구하기 본문
const fs = require('fs');
const data = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [M, N, K] = data[0].split(' ').map(Number);
const rectangles = data.slice(1).map(line => line.split(' ').map(Number));
function findAreasOfSeparatedRegions(M, N, rectangles) {
let grid = Array.from({ length: M }, () => Array(N).fill(0));
rectangles.forEach(([x1, y1, x2, y2]) => {
for (let y = y1; y < y2; y++) {
for (let x = x1; x < x2; x++) {
grid[y][x] = 1;
}
}
});
function bfs(startY, startX) {
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
let queue = [[startY, startX]];
let area = 0;
grid[startY][startX] = 2;
while (queue.length > 0) {
let [y, x] = queue.shift();
area++;
for (let [dy, dx] of directions) {
let ny = y + dy;
let nx = x + dx;
if (ny >= 0 && ny < M && nx >= 0 && nx < N && grid[ny][nx] === 0) {
grid[ny][nx] = 2;
queue.push([ny, nx]);
}
}
}
return area;
}
let areas = [];
for (let y = 0; y < M; y++) {
for (let x = 0; x < N; x++) {
if (grid[y][x] === 0) {
let area = bfs(y, x);
if (area > 0) {
areas.push(area);
}
}
}
}
areas.sort((a, b) => a - b);
return [areas.length, areas];
}
const [numAreas, areas] = findAreasOfSeparatedRegions(M, N, rectangles);
console.log(numAreas);
console.log(areas.join(' '));
728x90
'코딩테스트 > 99클럽' 카테고리의 다른 글
99클럽 코테 스터디 31일차 TIL - 양 한마리 (0) | 2024.08.24 |
---|---|
99클럽 코테 스터디 31일차 TIL - Number of Good Leaf Nodes Pairs (0) | 2024.08.24 |
99클럽 코테 스터디 31일차 TIL - Bad Grass (0) | 2024.08.23 |
99클럽 코테 스터디 31일차 TIL - Clear Cold Water (0) | 2024.08.22 |
99클럽 코테 스터디 29일차 TIL - Arranging Coins (0) | 2024.08.21 |