알고리즘 문제/BOJ
14395번 4연산
parkit
2019. 7. 18. 18:33
728x90
반응형
https://www.acmicpc.net/problem/14395
일반적인 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <bits/stdc++.h> using namespace std; long long s, t; queue<pair<long long, string> > q; map<long long, int> m; vector<char> v = { '*', '+', '-', '/' }; int main(void) { scanf("%lld %lld", &s, &t); if (s == t) { printf("0\n"); return 0; } q.push({ s, "" }); m[s] = 1; while (!q.empty()) { long long now = q.front().first; string s = q.front().second; q.pop(); if (now == t) { cout << s << '\n'; return 0; } for (int i = 0; i < 4; i++) { if (i == 0) { if (!m.count(now * now)) { q.push({ now * now, s + v.at(i) }); m[now * now] = 1; } } else if (i == 1) { if (!m.count(now + now)) { q.push({ now + now, s + v.at(i) }); m[now + now] = 1; } } else if (i == 2) { if (!m.count(now - now)) { q.push({ now - now, s + v.at(i) }); m[now - now] = 1; } } else if (i == 3) { if (now != 0 && !m.count(now / now)) { q.push({ now / now, s + v.at(i) }); m[now / now] = 1; } } } } printf("-1\n"); return 0; } | cs |
728x90
반응형