Skip to content

Commit

Permalink
[Silver II] Title: 점프 점프, Time: 144 ms, Memory: 14604 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
dukbong committed Jan 23, 2024
1 parent 5cbeb51 commit 8d238ca
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 백준/Silver/11060. 점프 점프/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# [Silver II] 점프 점프 - 11060

[문제 링크](https://www.acmicpc.net/problem/11060)

### 성능 요약

메모리: 14604 KB, 시간: 144 ms

### 분류

너비 우선 탐색, 다이나믹 프로그래밍, 그래프 이론, 그래프 탐색

### 제출 일자

2024년 1월 23일 12:32:50

### 문제 설명

<p>재환이가 1×N 크기의 미로에 갇혀있다. 미로는 1×1 크기의 칸으로 이루어져 있고, 각 칸에는 정수가 하나 쓰여 있다. i번째 칸에 쓰여 있는 수를 A<sub>i</sub>라고 했을 때, 재환이는 A<sub>i</sub>이하만큼 오른쪽으로 떨어진 칸으로 한 번에 점프할 수 있다. 예를 들어, 3번째 칸에 쓰여 있는 수가 3이면, 재환이는 4, 5, 6번 칸 중 하나로 점프할 수 있다.</p>

<p>재환이는 지금 미로의 가장 왼쪽 끝에 있고, 가장 오른쪽 끝으로 가려고 한다. 이때, 최소 몇 번 점프를 해야 갈 수 있는지 구하는 프로그램을 작성하시오. 만약, 가장 오른쪽 끝으로 갈 수 없는 경우에는 -1을 출력한다.</p>

### 입력

<p>첫째 줄에 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 A<sub>i</sub> (0 ≤ A<sub>i</sub> ≤ 100)가 주어진다.</p>

### 출력

<p>재환이가 최소 몇 번 점프를 해야 가장 오른쪽 끝 칸으로 갈 수 있는지 출력한다. 만약, 가장 오른쪽 끝으로 갈 수 없는 경우에는 -1을 출력한다.</p>

59 changes: 59 additions & 0 deletions 백준/Silver/11060. 점프 점프/점프 점프.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

int[] graph, dist;
boolean[] visited;

public static void main(String[] args) throws Exception {
new Main().solution();
}

public void solution() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int size = Integer.parseInt(br.readLine());
dist = new int[size];
visited = new boolean[size];
graph = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();

bfs(0, size - 1);
}

private void bfs(int start, int end) {
Queue<Integer> q = new LinkedList<>();
q.offer(start);

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

if(now == end){
System.out.println(dist[now]);
return;
}

int max = graph[now];

int next = now;

for(int i = 1; i <= max; i++){
next = now + i;

if(next <= end && !visited[next]){
q.offer(next);
dist[next] = dist[now] + 1;
visited[next] = true;
}

}
}
System.out.println(-1);
}

}

0 comments on commit 8d238ca

Please sign in to comment.