Skip to content

Commit 63cd8eb

Browse files
committed
[Silver I] Title: 그림, Time: 312 ms, Memory: 48880 KB -BaekjoonHub
1 parent b41c8cc commit 63cd8eb

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Silver I] 그림 - 1926
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1926)
4+
5+
### 성능 요약
6+
7+
메모리: 48880 KB, 시간: 312 ms
8+
9+
### 분류
10+
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색, 격자 그래프, 플러드 필
12+
13+
### 제출 일자
14+
15+
2026년 1월 20일 11:19:44
16+
17+
### 문제 설명
18+
19+
<p>어떤 큰 도화지에 그림이 그려져 있을 때, 그 그림의 개수와, 그 그림 중 넓이가 가장 넓은 것의 넓이를 출력하여라. 단, 그림이라는 것은 1로 연결된 것을 한 그림이라고 정의하자. 가로나 세로로 연결된 것은 연결이 된 것이고 대각선으로 연결이 된 것은 떨어진 그림이다. 그림의 넓이란 그림에 포함된 1의 개수이다.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 도화지의 세로 크기 n(1 ≤ n ≤ 500)과 가로 크기 m(1 ≤ m ≤ 500)이 차례로 주어진다. 두 번째 줄부터 n+1 줄 까지 그림의 정보가 주어진다. (단 그림의 정보는 0과 1이 공백을 두고 주어지며, 0은 색칠이 안된 부분, 1은 색칠이 된 부분을 의미한다)</p>
24+
25+
### 출력
26+
27+
<p>첫째 줄에는 그림의 개수, 둘째 줄에는 그 중 가장 넓은 그림의 넓이를 출력하여라. 단, 그림이 하나도 없는 경우에는 가장 넓은 그림의 넓이는 0이다.</p>
28+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
let input = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n');
2+
const [N, M] = input[0].split(' ').map(Number);
3+
const grid = input.slice(1, N + 1).map((l) => l.trim().split(' ').map(Number));
4+
5+
const dist = Array.from({ length: N }, () => Array(M).fill(-1));
6+
7+
const dr = [-1, 1, 0, 0];
8+
const dc = [0, 0, -1, 1];
9+
10+
let count = 0;
11+
let maxArea = 0;
12+
13+
for (let i = 0; i < N; i++) {
14+
for (let j = 0; j < M; j++) {
15+
const q = [];
16+
let head = 0;
17+
if (grid[i][j] === 0 || dist[i][j] !== -1) continue;
18+
count++;
19+
let area = 0;
20+
dist[i][j] = 0;
21+
q.push([i, j]);
22+
23+
while (head < q.length) {
24+
const [r, c] = q[head];
25+
head++;
26+
area++;
27+
for (let k = 0; k < 4; k++) {
28+
const nr = r + dr[k];
29+
const nc = c + dc[k];
30+
if (nr < 0 || nc < 0 || nr >= N || nc >= M) continue;
31+
32+
if (grid[nr][nc] === 0) continue;
33+
34+
if (dist[nr][nc] !== -1) continue;
35+
36+
dist[nr][nc] = 0;
37+
q.push([nr, nc]);
38+
}
39+
}
40+
maxArea = Math.max(maxArea, area);
41+
}
42+
}
43+
console.log(count);
44+
console.log(maxArea);
45+
46+
// 45m +
47+
// 가장 큰 값 찾는 로직
48+
// 모든 -1을 다 도는 로직

0 commit comments

Comments
 (0)