๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/16928
๐ฎ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
์ฃผ์ฌ์๋ฅผ ์กฐ์ํ ์ ์๋ค๊ณ ๋ฌธ์ ์์ ์ ์ํ๊ธฐ ๋๋ฌธ์ ๋งค ์์น๋ง๋ค 1~6๋ฒ๊น์ง์ ์ฃผ์ฌ์๋ฅผ ๋์ง ํ ์์น๋ฅผ ๋ด์ Node ๊ฐ์ฒด์ ๋ค์ ์์น์ ๋ค์ ๊ฑฐ๋ฆฌ(์ฃผ์ฌ์ ๋์ง ๊ฐ์)๋ฅผ ๋ฃ์ด ํ์ ๋ฃ์ด์ฃผ์๋ค.
๋งค ์ฃผ์ฌ์ ๊ตด๋ฆฐ ํ์ ์์น์์๋ ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๊ฐ ์๋ ์นธ์ธ์ง ๋จผ์ ํ์ธํ๋ค.
๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๊ฐ ์๋ ์นธ์ธ ๊ฒฝ์ฐ ์ฃผ์ฌ์๋ฅผ ํ๋ฒ ๊ตด๋ฆฐ ๊ฒ์ผ๋ก ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๋ฅผ ํ ์ดํ์ ์นธ์ผ๋ก ์ด๋์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ํ ์ดํ ์นธ ๊ฑฐ๋ฆฌ ๋ฐฐ์ด์ ํ์ฌ๊น์ง ์์น์ ์ฃผ์ฌ์ ๊ฐ์ + 1์ ํ ๊ฐ์ ๋ฃ์ด์ค๋ค.
๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๊ฐ ์๋ ์นธ์ธ ๊ฒฝ์ฐ ์ฃผ์ฌ์ ๊ตด๋ฆฐ ์ดํ์ ์นธ ๊ฑฐ๋ฆฌ ๋ฐฐ์ด์ ํ์ฌ ๊น์ง์ ์ฃผ์ฌ์ ๊ฐ์ + 1 ํ ๊ฐ์ ๋ฃ์ด์ค๋ค.
ํ์ฌ ํ์ ์นธ์ด 100๋ฒ์งธ ์นธ์ด๋ฉด ๋์ฐฉํ ์นธ ์ด๋ฏ๋ก 100๋ฒ์งธ ๊ฑฐ๋ฆฌ๋ฐฐ์ด์ ๋ฆฌํดํ๋ค.
- ์ฌ๋ค๋ฆฌ์ ๋ฑ์ ์์๊ณผ ๋์ Map์ผ๋ก ๊ด๋ฆฌํ๋ค.
- ํ๊ฐ ๋น์ด์์ง ์๊ณ , ํ์ฌ ํ์ ์์น๊ฐ 100๋ฒ ์นธ์ด ์๋ ๊ฒฝ์ฐ์ BFS ํ์ํ๋ค.
- ์ฃผ์ฌ์๋ฅผ 1-6๋ฒ ๊น์ง ๊ตด๋ฆฐ๋ค.
- ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฐ ํ์ ์์น๊ฐ ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๊ฐ ์๋ ์นธ์ด๋ผ๋ฉด, ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฐ ํ ์์น๊ฐ ์๋ ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๋ฅผ ํ ์นธ์ด ์ฃผ์ฌ์๋ฅผ ํ๋ฒ ๊ตด๋ฆฐ ํ์ ์์น์ด๋ค.
- ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฐ ํ์ ์์น๊ฐ ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๊ฐ ์๋ ์นธ์ด๋ผ๋ฉด, ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฐ ํ์์น๊ฐ ๊ทธ๋๋ก ๋ค์ ์นธ ์์น์ด๋ค.
- ๋ฐฉ๋ฌธํ ์นธ ๊ฑฐ๋ฆฌ๋ณด๋ค ํ์ฌ ์์น์ ๊ฑฐ๋ฆฌ์์ ํ๋ฒ ์ฃผ์ฌ์๋ฅผ ๋์ง ๊ฐ์ ๋ณด๋ค ์๋ค๋ฉด ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์ ํ๋ค.
- 100๋ฒ์งธ ์นธ์ ๊ฑฐ๋ฆฌ๋ฅผ ์ถ๋ ฅํ๋ค.
๐ฉ Java ์ฝ๋
package com.algorithm.Baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_16928_๋ฑ๊ณผ_์ฌ๋ค๋ฆฌ_๊ฒ์ {
private static final int INF = (int) 1e9;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()); // ์ฌ๋ค๋ฆฌ ๊ฐ์
int M = Integer.parseInt(st.nextToken()); // ๋ฑ ๊ฐ์
// ์ฌ๋ค๋ฆฌ์ ๋ฑ Map์ผ๋ก ๊ด๋ฆฌ
Map<Integer, Integer> snakeAndLadder = new HashMap<>();
for (int loop = 0; loop < N + M; loop++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
snakeAndLadder.put(start, end);
}
int rst = playGame(N, M, snakeAndLadder);
System.out.println(rst);
br.close();
}
private static int playGame(int n, int m, Map<Integer, Integer> snakeAndLadder) {
boolean[] visited = new boolean[101];
int[] distance = new int[101];
PriorityQueue<Node> queue = new PriorityQueue<>();
Arrays.fill(distance, INF);
queue.add(new Node(1, 0));
// ํ์ฌ ์นธ์ด 100๋ฒ์งธ ์นธ์ด ์๋๋ฉด์ ํ๊ฐ ๋จ์์๋ค๋ฉด
while (!queue.isEmpty() && queue.peek().getNode() != 100) {
Node position = queue.poll();
if (visited[position.getNode()]) continue;
visited[position.getNode()] = true;
// ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ
for (int dice = 1; dice <= 6; dice++) {
int nextPosition = position.getNode() + dice;
// ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ์ ํด๋นํ๋ ์นธ์ธ์ง ํ์ธ
if (snakeAndLadder.containsKey(nextPosition)) {
if (!visited[snakeAndLadder.get(nextPosition)] && distance[snakeAndLadder.get(nextPosition)] > position.getDistance() + 1) {
// ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๋ฅผ ํ ํ ์นธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์
distance[snakeAndLadder.get(nextPosition)] = position.getDistance() + 1;
queue.add(new Node(snakeAndLadder.get(nextPosition), position.getDistance() + 1));
}
}
// ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ์ ํด๋นํ๋ ์นธ์ด ์๋
// ๋ค์ ์นธ์ด 100๋ฒ์ฌ ์นธ ์ดํ์ด๋ฉด์ ๋ฐฉ๋ฌธํ์ง ์๊ณ , ํ์ฌ ๊ฑฐ๋ฆฌ + 1 ๋ณด๋ค ํด๋น ์นธ ๊ฐ์๊ฐ ์์ ๊ฒฝ์ฐ
else if (nextPosition <= 100 && !visited[nextPosition] && distance[nextPosition] > position.getDistance() + 1) {
distance[nextPosition] = position.getDistance() + 1;
queue.add(new Node(nextPosition, position.getDistance() + 1));
}
}
}
return distance[100];
}
static class Node implements Comparable<Node> {
private int node;
private int distance;
public Node(int node, int distance) {
this.node = node;
this.distance = distance;
}
public int getNode() {
return node;
}
public int getDistance() {
return distance;
}
@Override
public int compareTo(Node o) {
return Integer.compare(this.distance, o.distance);
}
}
}
'Algorithm > BFS & DFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค, Java] 19621๋ฒ : ํ์์ค ๋ฐฐ์ 2 (0) | 2022.05.23 |
---|---|
[๋ฐฑ์ค, Java] 9934๋ฒ : ์์ ์ด์ง ํธ๋ฆฌ (0) | 2022.05.20 |
[๋ฐฑ์ค, Java] 1967๋ฒ : ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2022.05.08 |
[๋ฐฑ์ค, Java] 1068๋ฒ : ํธ๋ฆฌ (0) | 2022.05.08 |
[๋ฐฑ์ค, Java] 2573๋ฒ : ๋น์ฐ (0) | 2022.05.02 |