개인 공부/TIL
[ TIL - PGS ] 99클럽 코테 스터디 31일차 TIL + 오늘의 학습 가이드
킴도비
2024. 8. 21. 22:03
💡 오늘의 학습 키워드
- 깊이/너비 우선 탐색(DFS/BFS)
✅ 오늘 공부한 내용
- 오늘의 백준 문제! 14248번 점프 점프
👀 오늘의 회고
🤣 오늘의 문제점
- 영우가 어떻게 뛸 수 있는지 초반에 이해가 안되어 어려웠다..!
🔥 어떤 시도를 했는가?
- 아래는 해당하는 소스코드이다!
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 입력받기
int n = sc.nextInt(); // 돌다리의 돌 개수
int[] A = new int[n + 1]; // 돌에 적혀 있는 점프 거리 배열
boolean[] visited = new boolean[n + 1]; // 방문 여부 체크 배열
for (int i = 1; i <= n; i++) {
A[i] = sc.nextInt();
}
int s = sc.nextInt(); // 출발점
// BFS를 사용하여 방문 가능한 돌 개수 세기
Queue<Integer> queue = new LinkedList<>();
queue.add(s);
visited[s] = true;
int count = 0;
while (!queue.isEmpty()) {
int current = queue.poll();
count++;
// 왼쪽으로 점프
int left = current - A[current];
if (left >= 1 && !visited[left]) {
visited[left] = true;
queue.add(left);
}
// 오른쪽으로 점프
int right = current + A[current];
if (right <= n && !visited[right]) {
visited[right] = true;
queue.add(right);
}
}
// 방문 가능한 돌들의 개수를 출력
System.out.println(count);
}
}
👏 무엇을 새로 알았는가?
- BFS 활용 방법
👩💻 내일은 무엇을 학습할 것인가?
- 항해 99 문제 풀기
- 원티드 팀 프로젝트 진행하기