Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- lis응용
- 애니메이션
- Unreal
- 백준
- 2075번
- dx12
- 바이토닉 수열
- 4134번
- unrealengine
- 백준 24444 c++
- WinAPI
- 뱀과사다리게임
- 2565번
- 11286번
- Perforce
- 랜더링 파이프라인
- directx12 그리기 연산2
- 2d 박스충돌
- DirectX12
- 다익스트라
- 그리기 연산
- 백준 1260 c++
- dx12 정리
- 드래곤플라이트 모작
- 루트서명
- 백준 c++ 24479번
- C++
- BFS
- 다음소수
- 24779
Archives
- Today
- Total
game-1 님의 블로그
[백준] c++ 13549번 숨바꼭질 3 (bfs풀이) 본문


풀이)
이 전에도 이와 비슷한 문제를 bfs를 이용하여 풀었는데, 이번에도 마찬가지로 bfs를 이용하여 문제를 풀었습니다.
다른 풀이로는 다익스트라 알고리즘을 사용할 수 있습니다.
실수 중에 조심해야할 점은 탐색하는 다음 노드가 MAX 범위를 넘어가지 않도록 범위 설정을 해주는 것입니다.
백준에서 정답률이 낮은 문제는 보통, 숫자의 범위를 생각해야되는 경우가 많은 것 같습니다.

코드)
#include<iostream>
#include<vector>
#include<queue>
#include<limits>
using namespace std;
int n, k;
const int MAX = 100'001;
const int INF = numeric_limits<int>::max();
vector<int> visited(MAX, INF);
void bfs(int start) {
queue<int> q;
q.push(start);
visited[start] = 0;
while (not q.empty()) {
int current = q.front();
q.pop();
int cnt = visited[current];
int nx = current + 1;
if (0 <= nx && nx < MAX && visited[nx] > cnt + 1){
q.push(nx);
visited[nx] = cnt + 1;
}
nx = current - 1;
if (0 <= nx && nx < MAX && visited[nx] > cnt + 1) {
q.push(nx);
visited[nx] = cnt + 1;
}
nx = current * 2;
if (0 <= nx && nx < MAX && visited[nx] > cnt ) {
q.push(nx);
visited[nx] = cnt;
}
}
}
int main()
{
cin >> n >> k;
bfs(n);
cout << visited[k];
}'백준 문제풀이' 카테고리의 다른 글
| [백준]1916 번 C++ 최소 비용 구하기 컴파일에러 (1) | 2025.02.08 |
|---|---|
| [백준] 1238번 C++ 파티 (1) | 2025.02.07 |
| [백준] 1504번 C++ 특정한 최단 경로 (0) | 2025.02.05 |
| [백준] 2075번 N번째 큰 수 C++ (2) | 2025.02.03 |
| [백준] 12865번 c++ 평범한 배낭 (0) | 2025.01.23 |