알고리즘
백준 16397번 탈출 C++
영춘권의달인
2023. 3. 23. 11:33
https://www.acmicpc.net/problem/16397
16397번: 탈출
첫 번째 줄에 N (0 ≤ N ≤ 99,999), T (1 ≤ T ≤ 99,999), G (0 ≤ G ≤ 99,999)가 공백 하나를 사이에 두고 주어진다. 각각 N은 LED로 표현된 수, T는 버튼을 누를 수 있는 최대 횟수, G는 탈출을 위해 똑같이
www.acmicpc.net
bfs
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, t, g;
vector<bool> found;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> t >> g;
found = vector<bool>(100000, false);
queue<pair<int,int>> q;
q.push({ n,0 });
found[n] = true;
int ans = -1;
while (!q.empty()) {
auto curr = q.front();
int currVal = curr.first;
int currCnt = curr.second;
if (currVal == g) {
ans = currCnt;
break;
}
q.pop();
{
int nextVal = currVal * 2;
if (nextVal < 100000 && nextVal != 0) {
string str = to_string(nextVal);
str[0] = str[0] - 1;
nextVal = stoi(str);
if (found[nextVal] == false && currCnt < t) {
found[nextVal] = true;
q.push({ nextVal,currCnt + 1 });
}
}
}
{
int nextVal = currVal + 1;
if (nextVal < 100000) {
if (found[nextVal] == false && currCnt < t) {
found[nextVal] = true;
q.push({ nextVal,currCnt + 1 });
}
}
}
}
if (ans == -1) cout << "ANG";
else cout << ans;
}