숨바꼭질과 비슷한 유형의 문제다. 일반적인 BFS문제와 비슷하게 풀면 되는데, 중복을 피하기 위해 배열을 하나 만들어 중복은 탐색하지 않게 했고, 예외처리에서 실수를 하면 틀릴 수 있다.
예외처리 부분에서 실수했던 것은 “B를 눌러 2를 곱한순간 99999를 넘으면 탈출할 수 없다”는 조건을 보고 2를 곱해 99999를 넘는 순간 종료를 ANG를 출력하고 종료를 시켰었는데… 홍익이는 2를 곱해 99999를 넘으면 B버튼을 안누르지 굳이 눌러서 탈출을 실패하는 멍청이가 아니라는것… 따라서 return을 해서 종료하는 것이 아니라 continue로 해당 경우를 제외시켜야 한다. 꼭 최소 한번은 틀리는 것 같다.. 계속 하다보면 한번에 맞았습니다. 를 보는 경우가 많아지기를!!
풀이보기
```c++
#include <iostream>
#include <queue>
using namespace std;
int n, t, g;
bool visited[100000];
void bfs() {
queue<int> q;
q.push(n);
int ans = 0;
if(n == g) {
printf("0\n");
return ;
}
do {
int qSize = q.size();
while (qSize--) {
int led = q.front(); q.pop();
if(led > 99999) {
continue;
}
if(led == g) {
printf("%d\n", ans);
return;
}
if(visited[led]) continue;
visited[led] = true;
q.push(led+1);
int next = led * 2;
if(next == 0) {
continue;
} else if(next > 99999) {
continue;
} else if (next >= 10000) {
q.push(next - 10000);
} else if (next >= 1000) {
q.push(next - 1000);
} else if (next >= 100) {
q.push(next - 100);
} else if (next >= 10) {
q.push(next - 10);
} else {
q.push(next - 1);
}
}
ans++;
} while(t--);
printf("ANG\n");
}
int main(int argc, const char * argv[]) {
scanf("%d %d %d", &n, &t, &g);
bfs();
return 0;
}
```