-
BOJ 14395 : 4연산백준 문제풀이/GRAPH 2020. 3. 6. 15:56
풀이
유사문제
2020/03/06 - [백준 문제풀이/GRAPH] - BOJ 12906 : 새로운 하노이 탑
코드
#include <iostream> #include <algorithm> #include <queue> #include <array> #include <map> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long st, end; cin >> st >> end; string s = ""; map< long long, string> d; queue<pair<string, long long> > q; q.push({ s, st }); d[st] = s; while (!q.empty()) { string now_s = q.front().first; long long now_i = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { auto next_s = now_s; auto next_i = now_i; if (i == 1) { next_s += '+'; next_i += now_i; } else if (i == 2) { next_s += '-'; next_i -= now_i; } else if (i == 0) { next_s += '*'; next_i *= now_i; } else if(i==3 && now_i != 0 ) { next_s += '/'; next_i /= now_i; } if (d.count(next_i) == 0) { q.push({ next_s, next_i }); d[next_i] = next_s; } } } if (st == end) { cout << 0 << '\n'; } else if (d.count(end) == 0) { cout << -1 << '\n'; } else { cout << d[end] << '\n'; } return 0; }
'백준 문제풀이 > GRAPH' 카테고리의 다른 글
BOJ 13023 : ABCDE (0) 2020.05.15 BOJ 17142 : 연구소 3 (0) 2020.05.10 BOJ 12906 : 새로운 하노이 탑 (0) 2020.03.06 BOJ 5213 : 과외맨 (0) 2020.02.19 BOJ 1525 : 퍼즐 (0) 2020.01.24