Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Week24] 스밈 알고리즘 24주차 제출 #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Week24] 1. 뱀과 사다리 게임
  • Loading branch information
thguss committed Aug 8, 2023
commit 1972ce068971fcdc7186e5541859ad780ed0450e
67 changes: 67 additions & 0 deletions sohyeon/src/week24/boj16928.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package week24;

import java.io.*;
import java.util.*;

public class boj16928 {

static int N, M;
static int[] visited = new int[101];
static int[] board = new int[101];

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

for (int i = 1; i <= 100; i++) {
board[i] = i;
}

for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
board[x] = y;
}

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
board[u] = v;
}

System.out.println(bfs());

}

private static int bfs() {
Queue<Integer> queue = new LinkedList<>();
queue.add(1);

while (!queue.isEmpty()) {
int now = queue.poll();

if (board[now] == 100) {
return visited[100];
}

for (int i = 1; i <= 6; i++) {
int next = now + i;
if (next > 100)
continue;
next = board[next];
if (visited[next] == 0) {
visited[next] = visited[now] + 1;
queue.add(next);
}
}
}

return -1;

}
}