Skip to content

Commit d312be3

Browse files
committed
[Silver II] Title: 유기농 배추, Time: 156 ms, Memory: 12584 KB -BaekjoonHub
1 parent baabca7 commit d312be3

2 files changed

Lines changed: 58 additions & 55 deletions

File tree

백준/Silver/1012. 유기농 배추/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 34096 KB, 시간: 80 ms
7+
메모리: 12584 KB, 시간: 156 ms
88

99
### 분류
1010

11-
그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색, 격자 그래프, 플러드 필
1212

1313
### 제출 일자
1414

15-
2024년 4월 30일 09:29:31
15+
2026년 2월 13일 13:36:40
1616

1717
### 문제 설명
1818

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
1-
let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
2-
const testCase = Number(input.shift());
3-
let M, N, K, graph;
4-
5-
// DFS 탐색
6-
const dfs = (x, y) => {
7-
const stack = [[x, y]];
8-
const dx = [-1, 1, 0, 0]; // 좌,우,상,하 x좌표 탐색
9-
const dy = [0, 0, 1, -1]; // 좌,우,상,하 y좌표 탐색
10-
11-
while (stack.length) {
12-
const [curX, curY] = stack.pop();
13-
14-
// 현재 위치에서 인접한(좌우상하) 곳에 배추 심어져있는지 확인
15-
for (let i = 0; i < 4; i++) {
16-
const nx = curX + dx[i];
17-
const ny = curY + dy[i];
18-
19-
// 현재 좌표가 밭을 벗어나지 않고, 인접한 곳에 배추가 심어져있는 경우
20-
if (nx >= 0 && nx < N && ny >= 0 && ny < M && graph[nx][ny]) {
21-
stack.push([nx, ny]);
22-
graph[nx][ny] = 0; // 현재 위치 방문 처리
23-
}
24-
}
1+
/* /dev/stdin */
2+
let fs = require("fs");
3+
let input = fs
4+
.readFileSync("/dev/stdin")
5+
.toString()
6+
.split("\n")
7+
.map((x) => x.replaceAll("\r", ""));
8+
9+
const T = input[0];
10+
let startPoint = 1;
11+
12+
for (let i = 1; i <= T; i++) {
13+
const [M, N, K] = input[startPoint].split(" ").map(Number);
14+
const board = Array.from({ length: N }, () => Array(M).fill(0));
15+
16+
for (let j = startPoint + 1; j < startPoint + 1 + K; j++) {
17+
const [y, x] = input[j].split(" ").map(Number);
18+
19+
board[x][y] = 1;
2520
}
26-
};
27-
28-
// 필요한 지렁이 마리 수 체크하는 함수
29-
const howManyWorms = () => {
30-
let answer = 0;
31-
for (let i = 0; i < N; i++) {
32-
for (let j = 0; j < M; j++) {
33-
if (graph[i][j]) {
34-
answer++;
35-
dfs(i, j);
21+
let result = 0;
22+
for (let k = 0; k < board.length; k++) {
23+
for (let l = 0; l < board[k].length; l++) {
24+
if (board[k][l] === 1) {
25+
bfs(board, k, l, N, M);
26+
result++;
3627
}
3728
}
3829
}
39-
console.log(answer);
40-
};
4130

42-
for (let i = 0; i < testCase; i++) {
43-
// [가로길이, 세로길이, 배추가 심어져 있는 위치의 개수]
44-
[M, N, K] = input.shift().split(' ').map(Number);
31+
startPoint += K + 1;
32+
console.log(result);
33+
}
4534

46-
// 밭 크기와 동일한 그래프(초기값 0으로 채워진 2차원 배열) 생성
47-
graph = Array.from(Array(N), () => Array(M).fill(0));
35+
function bfs(board, k, l, N, M) {
36+
const queue = [];
37+
queue.push([k, l]);
4838

49-
// 배추 위치 입력값받아 배추있는 자리에 1로 표시
50-
for (let j = 0; j < K; j++) {
51-
const [x, y] = input[j].split(' ').map(Number);
52-
graph[y][x] = 1;
53-
}
39+
while (queue.length) {
40+
const [x, y] = queue.shift();
5441

55-
// 필요한 지렁이 마리 수 출력하는 함수 호출
56-
howManyWorms();
57-
58-
// 첫 번째 예제 출력값 호출 후, 다음 케이스로 넘어가기
59-
input = input.slice(K);
60-
}
42+
//상
43+
if (x - 1 >= 0 && board[x - 1][y] === 1) {
44+
board[x - 1][y] = 0;
45+
queue.push([x - 1, y]);
46+
}
47+
//하
48+
if (x + 1 < N && board[x + 1][y] === 1) {
49+
board[x + 1][y] = 0;
50+
queue.push([x + 1, y]);
51+
}
52+
//좌
53+
if (y - 1 >= 0 && board[x][y - 1] === 1) {
54+
board[x][y - 1] = 0;
55+
queue.push([x, y - 1]);
56+
}
57+
//우
58+
if (y + 1 < M && board[x][y + 1] === 1) {
59+
board[x][y + 1] = 0;
60+
queue.push([x, y + 1]);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)