16937 백준 '탈출' c++

PBC·2025년 6월 14일
0
post-thumbnail

단연코 세상에서 가장 쉬운 골드 4

걍 딸깍 문제다 못풀리가 없지만
혹시라도 이문제로 bfs를 접해볼꺼라는 사람은 걍 class4나 밀기 바란다.
짜증 나는건 B버튼 구현이 귀찮다는거다 이상없다.
코드 복붙 하려는 사람을 위해 아래에 코드를 제공했다.
아 참고로 실수 하는것도 있는데 x*2 > 99999이면 B버튼을 사용할수 없다

#include <queue>
#include <iostream>
using namespace std;

int n, t, g;
int map[100000];
queue<int> q;

int asd(int x) {
    int xx=x;
    int mul = 1, ans, mod = 0;
    while (true) {
        if (xx/10==0){
            ans = xx % 10;
            if (ans != 0)
                ans--;
            return ans * mul + mod;
        }
        mod += xx % 10 * mul;
        xx /= 10;
        mul*=10;
    }
}

void bfs() {
    q.push(n);
    map[n] = 1;
     
    while (!q.empty()) {
        int x = q.front();
        q.pop();
       
        if (x==g){
            cout<<map[x]-1<<"\n";
            return;
        }
        if (map[x+1]==0&&x+1<=99999&&map[x]<t+1){
            q.push(x + 1);
            map[x + 1] = map[x] + 1;
        }
        
        int b = asd(x * 2);
        if (map[b] == 0 && b <= 99999&&map[x]<t+1&&x*2<=99999) {
            q.push(b);
            map[b]=map[x]+1;
        }
    }
    cout << "ANG\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> n >> t >> g;
    //cout << asd(246) << "\n";
    bfs();
}
profile
그냥 블로그 입니다 보지 마세요

0개의 댓글