반응형
https://www.acmicpc.net/problem/16928
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static StringBuilder output = new StringBuilder();
static int N, M, cnt;
static int[] map = new int[101];
static boolean[] visited = new boolean[101];
public static void main(String args[]) throws Exception {
tokens = new StringTokenizer(input.readLine());
N = Integer.parseInt(tokens.nextToken());
M = Integer.parseInt(tokens.nextToken());
for (int n = 0; n < N + M; n++) {
tokens = new StringTokenizer(input.readLine());
map[Integer.parseInt(tokens.nextToken())] = Integer.parseInt(tokens.nextToken());
}
bfs(1);
System.out.println(cnt);
}
private static void bfs(int i) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(i);
visited[i] = true;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
int start = queue.poll();
if(start == 100) return;
for (int k = 1; k < 7; k++) {
int next = start + k;
if(next <= 100 && !visited[next]) {
//들어와서
//1. 뱀, 사다리가 있을경우
if(map[next] != 0) {
next = map[next];
if(visited[next]) continue;
}
//2. 없을경우
queue.offer(next);
visited[next]=true;
}
}
}
cnt++;
}
}
}
반응형
'코딩테스트 > 백준문제' 카테고리의 다른 글
[Java] 백준_3059_등장하지않는문자의합 (0) | 2023.01.26 |
---|---|
[Java] 백준_10828_스택 (0) | 2021.05.25 |
[Java] 백준_10815_숫자카드 (0) | 2021.05.24 |
[Java] 백준_2750_수 정렬하기 (0) | 2021.05.22 |
[Java] 백준_1920_수찾기 (0) | 2021.05.21 |