Skip to content

Commit dac1080

Browse files
committed
[Silver II] Title: 유기농 배추, Time: 328 ms, Memory: 31248 KB -BaekjoonHub
1 parent 78574eb commit dac1080

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

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

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

55
### 성능 요약
66

7-
메모리: 31320 KB, 시간: 328 ms
7+
메모리: 31248 KB, 시간: 328 ms
88

99
### 분류
1010

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

1313
### 제출 일자
1414

15-
2025년 7월 9일 18:24:30
15+
2025년 7월 9일 18:50:05
1616

1717
### 문제 설명
1818

백준/Silver/1012. 유기농 배추/유기농 배추.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ public static void dfs(int x, int y) {
1818

1919
if (nx >= 0 && nx < M && ny >= 0 && ny < N) { // 배추 밭 범위 안에 있는지 확인
2020
if (field[nx][ny] == 1 && !visited[nx][ny]) { // 현재 위치가 배추가 심어진 곳이지만 아직 방문 x
21-
dfs(nx, ny);
21+
dfs(nx, ny); // 그 주변을 계속 탐색
2222
}
2323
}
2424
}
2525
}
2626

2727
public static void bfs(int x, int y) {
2828
Queue<int[]> q = new LinkedList<>();
29-
q.offer(new int[]{x, y});
30-
visited[x][y] = true;
29+
q.offer(new int[]{x, y}); // 먼저 시작 위치를 큐에 담는다
30+
visited[x][y] = true; // 해당 위치 방문 처리
3131

3232
while (!q.isEmpty()) {
33-
int[] now = q.poll();
34-
int cx = now[0];
35-
int cy = now[1];
33+
int[] now = q.poll(); // 큐에 있는 좌표를 하나 꺼내서 현재 위치로 설정
34+
int new_x = now[0];
35+
int new_y = now[1];
3636

37-
for (int dir = 0; dir < 4; dir++) {
38-
int nx = cx + dx[dir];
39-
int ny = cy + dy[dir];
37+
for (int i = 0; i < 4; i++) { // 상하좌우 4방향 확인
38+
int nx = new_x + dx[i];
39+
int ny = new_y + dy[i];
4040

4141
if (nx >= 0 && nx < M && ny >= 0 && ny < N) {
42-
if (field[nx][ny] == 1 && !visited[nx][ny]) {
43-
visited[nx][ny] = true;
44-
q.offer(new int[]{nx, ny});
42+
if (field[nx][ny] == 1 && !visited[nx][ny]) { // 현재 위치가 배추가 심어진 곳이지만 아직 방문 x
43+
visited[nx][ny] = true; // 방문처리하고 좌표를 큐에 넣음
44+
q.offer(new int[]{nx, ny}); // 큐에 넣는 이유? 해당 좌표를 방문하고 이 좌표를 기준으로 탐색하기 위해서
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)