#include <stdio.h> #include <iostream> #include <set> #include <map> #include <queue> #include <vector> #include <string> #include <algorithm> using namespace std; long long startNum, endNum; string ans; bool flag = false; queue<pair<long long, string>> q; map<long long, int> visited; void BFS() { visited[startNum] = 1; q.push({startNum, ""}); while(!q.empty()) { long long num = q.front().first; string oper = q.front().second; q.pop(); if(num == endNum) { flag = true; ans = oper; return; } for(int i = 1; i <= 4; i++) { long long numTemp = num; // * if(i == 1) { numTemp *= numTemp; if(visited[numTemp] == 0) { visited[numTemp] = 1; q.push({numTemp, oper+"*"}); } } // + else if(i == 2) { numTemp += numTemp; if(visited[numTemp] == 0) { visited[numTemp] = 1; q.push({numTemp, oper+"+"}); } } // - else if(i == 3) { numTemp -= numTemp; if(visited[numTemp] == 0) { visited[numTemp] = 1; q.push({numTemp, oper+"-"}); } } // / else if(i == 4) { if(numTemp == 0) { continue; } numTemp /= numTemp; if(visited[numTemp] == 0) { visited[numTemp] = 1; q.push({numTemp, oper+"/"}); } } } } } int main(void) { // freopen("B14395_input.txt", "r", stdin); cin >> startNum >> endNum; if(startNum == endNum) { cout << "0" << endl; return 0; } BFS(); if(flag == true) { cout << ans << endl; } else { cout << "-1" << endl; } return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 1389] 케빈 베이컨의 6단계 법칙 (BFS) (C/C++) (0) | 2019.12.06 |
---|---|
[백준 1261] 알고스팟 (BFS) (C/C++) (★) (0) | 2019.12.06 |
[백준 7576] 토마토 (BFS) (C/C++) (0) | 2019.12.06 |
[백준 5427] 불 (BFS) (C/C++) (★) (0) | 2019.12.05 |
[백준 3055] 탈출 (BFS) (C/C++) (★) (0) | 2019.12.05 |