📖 문제
14497번: 주난의 난(難)
주난이는 크게 화가 났다. 책상 서랍 안에 몰래 먹으려고 숨겨둔 초코바가 사라졌기 때문이다. 주난이는 미쳐 날뛰기 시작했다. 사실, 진짜로 뛰기 시작했다. ‘쿵... 쿵...’ 주난이는 점프의 파
www.acmicpc.net
💡 풀이 방식
• 다익스트라 (우선순위 큐)
최단 경로인 지점을 항상 우선으로 탐색하고자 점프 횟수 기준 오름차순 정렬하는 우선순위 큐를 활용한다.
class Node implements Comparable<Node> {
int x, y, w;
public Node(int x, int y, int w) {
this.x = x;
this.y = y;
this.w = w;
}
@Override
public int compareTo(Node n) { // 💥 점프 횟수 기준 오름차순 정렬
return this.w - n.w;
}
}
이동하는 다음 칸의 배열 값이 1인 경우, 현재까지 걸린 점프 횟수 + 1한다.
하지만, 다음 칸의 배열의 값이 0인 경우, 현재까지 걸린 점프 횟수의 값을 그대로 가져간다.
// 💥 0이면 점프 횟수 그대로, 1이면 점프 횟수 +1
pq.add(new Node(nx, ny, map[nx][ny] == '0' ? now.w : now.w +1));
🔥 우선순위 큐 사용 이유
: 일반 큐를 사용한다면, 최소 점프 횟수 순이 아닌 값으로 범인에게 도달할 수 있다. (사례)
그러므로 우선순위를 고려해 추출하는 우선순위 큐를 사용해야 한다.
🔺 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import java.util.*;
import java.io.*;
public class Main {
static int[] dx = {-1,1,0,0};
static int[] dy = {0,0,-1,1};
static int N,M, x1, y1, x2, y2;
static char[][] map;
static boolean[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine() ," ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new char[N][M];
visited = new boolean[N][M];
st = new StringTokenizer(br.readLine() ," ");
x1 = Integer.parseInt(st.nextToken()) - 1;
y1 = Integer.parseInt(st.nextToken()) - 1;
x2 = Integer.parseInt(st.nextToken()) - 1;
y2 = Integer.parseInt(st.nextToken()) - 1;
for(int i = 0 ; i < N ; i++) {
String str = br.readLine();
for(int j = 0 ; j < M ; j++) {
map[i][j] = str.charAt(j);
}
}
System.out.println(dijkstra());
}
private static int dijkstra() {
PriorityQueue<Node> pq = new PriorityQueue<>(); // 🔥 우선순위 큐 !!
pq.add(new Node(x1, y1, 0));
visited[x1][y1] = true;
while(!pq.isEmpty()) {
Node now = pq.poll();
if(now.x == x2 && now.y == y2) return now.w;
for(int d = 0 ; d < 4 ; d++) {
int nx = now.x + dx[d];
int ny = now.y + dy[d];
if(nx < 0 || nx >= N || ny < 0 || ny >= M || visited[nx][ny]) continue;
visited[nx][ny] = true;
pq.add(new Node(nx, ny, map[nx][ny] == '0' ? now.w : now.w +1)); // 💥 0이면 점프 횟수 그대로, 1이면 점프 횟수 +1
}
}
return 0;
}
}
class Node implements Comparable<Node> {
int x, y, w;
public Node(int x, int y, int w) {
this.x = x;
this.y = y;
this.w = w;
}
@Override
public int compareTo(Node n) { // 💥 점프 횟수 기준 오름차순 정렬
return this.w - n.w;
}
}
|
cs |
➕ 다른 풀이 방식
- 다익스트라에서 2차원 int형 배열 사용해 거리 배열에 값 저장한 풀이
백준 14497 - 주난의 난
https://www.acmicpc.net/problem/14497다익스트라 또는 0-1 BFS로 풀이할 수 있는 문제였다.다익스트라의 경우 dist 를 $N \\times M$ 2차원 배열의 형태로 정의한뒤 시작점을 0, 도착점을 1로 비용 설정해주어 ma
velog.io
- 0-1 BFS를 활용한 풀이
백준 14497번 주난의 난(難)
백준 14497번 주난의 난(難)
velog.io
💦 어려웠던 점
- 최단 점프 횟수인 친구를 가장 먼저 처리하기 위한 처리 및 정렬하는 것을 빠뜨렸다.
- 격자 내 값이 0이냐/1이냐에 따라 점프 횟수가 바뀐다는 점을 캐치하지 못했다 (어째서!)
🧐 새로 알게 된 내용
- 우선순위 큐를 이용해 점프 횟수 기준 오름차순 정렬한 값을 뽑아와서 사용하는 것...
- 0-1 BFS : deque 을 사용해 가중치가 0인 지점은 offerFirst()하고, 1인 지점은 offerLast()한다.
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V |
(참고)
[Algorithm] 백준 14497 주난의 난
[ 문제 ] 주난이는 크게 화가 났다. 책상 서랍 안에 몰래 먹으려고 숨겨둔 초코바가 사라졌기 때문이다. 주난이는 미쳐 날뛰기 시작했다. 사실, 진짜로 뛰기 시작했다. ‘쿵... 쿵...’ 주난이는 점
handsukite.tistory.com
'코테 > 백준' 카테고리의 다른 글
[백준/JAVA] 15990번: 1, 2, 3 더하기 5 (0) | 2024.04.23 |
---|---|
[백준/JAVA] 8980번: 택배 (0) | 2024.04.22 |
[백준/JAVA] 2141번: 우체국 (0) | 2024.04.19 |
[백준/JAVA] 1987번: 알파벳 (0) | 2024.04.18 |
[백준/JAVA] 1455번: 뒤집기 II (0) | 2024.04.17 |