๋ฌธ์ ๋งํฌ
1697๋ฒ: ์จ๋ฐ๊ผญ์ง (acmicpc.net)
1697๋ฒ: ์จ๋ฐ๊ผญ์ง
์๋น์ด๋ ๋์๊ณผ ์จ๋ฐ๊ผญ์ง์ ํ๊ณ ์๋ค. ์๋น์ด๋ ํ์ฌ ์ N(0 ≤ N ≤ 100,000)์ ์๊ณ , ๋์์ ์ K(0 ≤ K ≤ 100,000)์ ์๋ค. ์๋น์ด๋ ๊ฑท๊ฑฐ๋ ์๊ฐ์ด๋์ ํ ์ ์๋ค. ๋ง์ฝ, ์๋น์ด์ ์์น๊ฐ X์ผ
www.acmicpc.net
์ค๋ฒ๋ผ๊ณ ๋ง๋งํ๊ฒ ๋ดค๋ค
ํ์ด
BFS๋ฅผ ํ์ฉํ ๋ฌธ์ . ์์ผ๋ก ํ ์นธ์ ๊ฐ๋ ๋ค๋ก ํ ์นธ์ ๊ฐ๋ ์๊ฐ์ด๋์ ํ๋ ์ฌ์ฉ๋๋ ์๊ฐ์ ๋์ผํ๋ค.
ํ๋ฅผ ๋ง๋ค์ด์ ์์น์ ์๊ฐ์ ์ ์ฅํ๋ ค๊ณ ํ๋ค.
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
32
33
34
35
36
37
38
39
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N, K;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> K;
queue<pair<int, int>> q;
q.push({ N, 0 });
while (!q.empty()) {
int now = q.front().first;
int Time = q.front().second;
q.pop();
if (now == K) {
cout << Time << "\n";
break;
}
q.push({ now * 2, Time + 1 });
q.push({ now + 1, Time + 1 });
q.push({ now - 1, Time + 1 });
}
return 0;
}
|
cs |
๋ง ๊ทธ๋๋ก BFS์ ๊ธฐ์ดํ ์ฝ๋๋ค. ๊ทธ๋ฌ๋๋ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๋ฅผ ๋ง์ฃผํ๋ค. ์๊ฐ์ด๊ณผ๋ ์์ฃผ ๋ฐ์์ด๋ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๋ ์ฒ์์ธ ๊ฒ ๊ฐ๋ค.
์๋ง ํ ๋ฒ ๋๋๋ง๋ค ํ์ 3๊ฐ์ฉ ๋ฐ์ด๋ฃ์ด์ ๊ทธ๋ฐ ๊ฒ๊ฐ๋ค. ๋ฌด์กฐ๊ฑด ๋ฐ์ด ๋ฃ๋๊ฒ ์๋๋ผ ์์ธ๋ฅผ ๊ฑธ์ด์ค์ผ ํ ๊ฒ ๊ฐ๋ค.
Time_mem ์ด๋ผ๋ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ๊ทธ ์์น๊น์ง ๊ฐ๋๋ฐ ๊ฑธ๋ฆฐ ์๊ฐ์ ์ ์ฅํ๋ค. ๋ง์ฝ ๊ฐ์ ์์น์ ๋๋ฌํ์ ๋ ๊ฑธ๋ฆฐ ์๊ฐ์ด ์ด๋ฏธ ์ ์ฅ๋ ์๊ฐ๋ณด๋ค ํฌ๋ค๋ฉด ๊ตณ์ด ํ์ ๋ค์ ๊ฒฝ์ฐ๋ฅผ ๋ฐ์ด๋ฃ์ ํ์๊ฐ ์๋ค. ๊ทธ ๊ฒฝ์ฐ ๊ทธ๋ฅ ํ์ ๋ ๋ฃ์ง์๊ณ continue๋ก ๋์ด๊ฐ๋ค.
์ด๋ ๊ฒ ํ๋๋ ํ๋ ์ค์ผ ์ ์์์ง๋ง ๋ฌธ์ ๊ฐ ํ๋ ๋ ์์๋ค. now * 2 ๋ฅผ ๊ณ์ ํ๋ค๋ณด๋ now๊ฐ ๋ฌดํํ ์ปค์ง ์ ์์๋ค.
๊ทธ๋ ๊ฒ ๋๋ฉด Time_mem์ ๋ฒ์๋ฅผ K์ ๋ฒ์๋ก ์ง์ ํด๋จ๋๋ฐ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋๊ฒ๋๋ค. ๋ ์๊ฐ๋ญ๋น์ ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๋ค.
now์ 2๋ฐฐ๊ฐ K์ 2๋ฐฐ๋ฅผ ๋์ ์ ์๋ค๊ณ ํ๋จํ๋ค. ์๋ now์ 2๋ฐฐ๊ฐ K์ 2๋ฐฐ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ ๊ฑด ๊ทธ ์ ์ ์ด๋ฏธ ๋๋ฌํ ์ ์์๋ค๋ ๋ป์ด๋ค. ๊ทธ๋์ ๊ทธ ๊ฒฝ์ฐ ์์ธ๋ฅผ ๊ฑธ์ด์คฌ๋ค.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N, K;
int Time_mem[1000000 + 5];
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> K;
memset(Time_mem, -1, sizeof(Time_mem));
queue<pair<int, int>> q;
q.push({ N, 0 });
while (!q.empty()) {
int now = q.front().first;
int Time = q.front().second;
q.pop();
if (now == K) {
cout << Time << "\n";
break;
}
if (Time_mem[now] != -1 && Time_mem[now] <= Time)
continue;
Time_mem[now] = Time;
q.push({ now + 1, Time + 1 });
q.push({ now - 1, Time + 1 });
if (now * 2 >= K * 2)
continue;
q.push({ now * 2, Time + 1 });
}
return 0;
}
|
cs |
๊ทธ๋ ๊ฒ ๊น๋ํ ์ฝ๋๋ ์๋ ๊ฒ ๊ฐ์ง๋ง ์๋ฌดํผ ๋ง์๋ค.
'๋ฐฑ์ค > Silver' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 19539 : ์ฌ๊ณผ๋๋ฌด (0) | 2022.03.22 |
---|---|
[BOJ] 6716 : Collecting Beepers (0) | 2022.01.19 |