-
[백준 14248 ] 점프 점프 javaAlgorithm/BOJ 2021. 10. 22. 11:44
https://www.acmicpc.net/problem/14248
문제
영우는 개구리다 개굴개굴개굴
영우는 지금 n개의 돌이 일렬로 놓여있는 돌다리에 있다. 그리고 돌다리의 돌에는 숫자가 하나씩 적혀있다. 영우는 이 숫자가 적혀있는 만큼 왼쪽이나 오른쪽으로 점프할 수 있는데, 이때 돌다리 밖으로 나갈 수는 없다.
영우는 이 돌다리에서 자기가 방문 가능한 돌들의 개수를 알고 싶어한다. 방문 가능하다는 것은 현재위치에서 다른 돌을 적절히 밟아 해당하는 위치로 이동이 가능하다는 뜻이다.
현재 위치가 주어졌을 때, 영우가 방문 가능한 돌들의 개수를 출력하시오.
입력
첫 번째 줄에는 돌다리의 돌 개수 n이 주어진다.(1≤n≤100,000) 돌의 번호는 왼쪽부터 1번에서 n번이다. 다음 줄에는 그 위치에서 점프할 수 있는 거리 Ai가 주어진다.(1≤Ai≤100,000)
다음 줄에는 출발점 s가 주어진다.(1≤s≤n)
출력
영우가 방문 가능한 돌들의 개수를 출력하시오.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class 점프점프_14248 { static int n; static int check[], stoneBridge[]; static int dir[] = {1, -1}; // 왼쪽 , 오른쪽 점프 static int count = 1; // 시작 지점도 방문한 것이니 1으로 초기화 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine()); stoneBridge = new int[n]; // 돌다리 check = new int[n]; // 방문 한곳 StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { stoneBridge[i] = Integer.parseInt(st.nextToken()); } int s = Integer.parseInt(br.readLine()); // 시작지점 3 System.out.println(bfs(s-1)); // 배열 인덱스는 2 } public static int bfs(int node) { Queue<Integer> queue = new LinkedList<>(); queue.offer(node); check[node] = 1; // 방문한 곳 체크 while (!queue.isEmpty()) { node = queue.poll(); int jump = stoneBridge[node]; for (int d : dir) { int direction = (d * jump) + node; if (direction >= 0 && direction < n && check[direction] == 0) { check[direction] = 1; queue.offer(direction); count++; } } } return count; } }
'Algorithm > BOJ' 카테고리의 다른 글
[백준 13144] List of Unique Numbers cpp (0) 2022.03.01 [백준 1296] 팀이름 정하기 c++ (0) 2021.12.05 [백준 2535] - 아시아 정보올림피아드 java (0) 2021.10.18 [백준-10866] 덱 - java (0) 2021.06.30 [백준 - 10809] - 알파벳 찾기 (java) (0) 2021.05.17