์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- CVE
- ๊ตฌํ
- ์ต๋จ ๊ฒฝ๋ก
- ์์ ์ ๋ ฌ
- DP
- ๋ฐ์ดํฌ์คํธ๋ผ
- ์ด๋ถ ํ์
- ๋งต
- ๋์ ํฉ
- JPA
- thymeleaf
- dfs
- ์๋ฎฌ๋ ์ด์
- ๋ฌธ์์ด
- dynamic debugging
- ์ฌ๊ท
- ์คํ
- java
- web
- error
- ๋ฐฑํธ๋ํน
- c++
- ๋ถํ ์ ๋ณต
- Reversing
- OS
- GCP
- Spring
- ๊ทธ๋ฆฌ๋
- ์ฐ์ ์์ ํ
- BFS
- Today
- Total
hades
[Baekjoon] 1697๋ฒ: ์จ๋ฐ๊ผญ์ง ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1697
๐ ์ค๊ณ
ํ์ฌ ์์น๋ฅผ x๋ผ๊ณ ํ ๋, ๊ทธ ์์น์์ +1, -1, *2์ ์์น๋ก ์ด๋ํ ์ ์์ผ๋ฏ๋ก, ํ์์ ํ์ฌ ์์น๋ฅผ ๊บผ๋ด ๋งค ์ด๋ง๋ค ์ด๋ํ ์ ์๋ ์๋ก์ด ์์น๋ฅผ ๊ฐฑ์ ํ๋ค.
ํ๊ฐ ๋น ๋๊น์ง ํ๋๋ฐ, ์ด๋ค ์ด์ ๋์ฌ์ ธ ์๋ ์์น๋ก๋ถํฐ ๋ค์ ์ด์ ์ด๋ํ ์์น ์ป์ด๋ด๋ ๊ณผ์ ์์ ์ด๋ํ๊ธฐ ์ size๋ฅผ ์ ์ฅํ๊ณ , ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์๋ค.
๋ค์ ์ด์ ์ด๋ํ ์ ์๋ ์์น์ ๋ฒ์๋ฅผ ๋ฐ๋์ ํ์ธํด์ผ ํ๋ค.
์ด๋ค ์์น์ ์ด๋ฏธ ์ด๋ํ๊ณ , ๊ทธ๋ก ์ธํด ์๋ก์ด ์์น๋ฅผ ์ป์ด๋๋๋ฐ, ๋์ค์ ๋ ๋ฐฉ๋ฌธํ๋ค๋ฉด ์ค๋ณต์ด ์ผ์ด๋ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ, ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ฏ๋ก ์ด๋ค ์์น์ ์ด๋ํ ์๊ฐ์ visited์ ์ ์ฅํ์ฌ ์ ์ฅ๋ ์๊ฐ๋ณด๋ค ๋์ฐฉํ ์๊ฐ์ด ํฌ๋ค๋ฉด ํ์ ์ถ๊ฐํ์ง ์๋๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
int n, k, sec=0;
vector<int> visited(100001, 1e9+7);
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n >> k;
if (n==k){
cout << 0 << "\n";
return 0;
}
queue<int> q;
q.push(n);
while (!q.empty()){
sec+=1;
int size = q.size();
for (int i=0; i<size; i++){
int x = q.front();
q.pop();
if (x+1 <= 100000){
if (visited[x+1] > sec){
visited[x+1] = sec;
q.push(x+1);
}
}
if (x-1 >= 0){
if (visited[x-1] > sec){
visited[x-1] = sec;
q.push(x-1);
}
}
if (x*2 <= 100000){
if (visited[x*2] > sec){
visited[x*2] = sec;
q.push(x*2);
}
}
}
}
cout << visited[k] << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1931๋ฒ: ํ์์ค ๋ฐฐ์ (0) | 2024.07.09 |
---|---|
[Baekjoon] 1764๋ฒ: ๋ฃ๋ณด์ก (0) | 2024.07.09 |
[Baekjoon] 1620๋ฒ: ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์ (0) | 2024.07.07 |
[Baekjoon] 1541๋ฒ: ์์ด๋ฒ๋ฆฐ ๊ดํธ (0) | 2024.07.05 |
[Baekjoon] 1260๋ฒ: DFS์ BFS (0) | 2024.07.03 |