-
Notifications
You must be signed in to change notification settings - Fork 0
/
[BOJ] 14395 4연산(BFS, 복습 필요🔥).cpp
56 lines (49 loc) · 1.37 KB
/
[BOJ] 14395 4연산(BFS, 복습 필요🔥).cpp
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
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const long long limit = 1000000000LL;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
long long s, t;
cin >> s >> t;
set<long long> check; // 10억 크기의 배열을 만들 수 없어 set을 사용
queue<pair<long long, string>> q;
q.push({s, ""});
check.insert(s);
while(!q.empty()) {
long long x;
string s;
x = q.front().first;
s = q.front().second;
q.pop();
if (x == t) {
if (s.length() == 0) {
s = "0";
}
cout << s << endl;
return 0;
}
if (0 <= x*x && x*x <= limit && check.count(x*x) == 0) {
q.push(make_pair(x*x, s+"*"));
check.insert(x*x);
}
if (0 <= x+x && x+x <= limit && check.count(x+x) == 0) {
q.push(make_pair(x+x, s+"+"));
check.insert(x+x);
}
if (0 <= x-x && x-x <= limit && check.count(x-x) == 0) {
q.push(make_pair(x-x, s+"-"));
check.insert(x-x);
}
if (x != 0 && 0 <= x/x && x/x <= limit && check.count(x/x) == 0) {
q.push(make_pair(x/x, s+"/"));
check.insert(x/x);
}
}
cout << "-1" << endl;
return 0;
}