간단한 문제인데 이상한 곳에서 좀 헤맨 문제이다. 숫자를 눌러줄때 cnt를 안해주면 0에서 무한 루프가 돌 수 있으니 조심해야하고, 숫자를 누른것이 바로 목표 채널일 때를 생각해야한다.
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
vector<bool> button(10, true);
int answer;
int n, m, n_size;
void dfs(int cnt, int num) {
if (cnt > 0) {
answer = min(answer, cnt + abs(n - num));
}
if (cnt > n_size) return ;
for (int i=0; i<10; i++) {
if (button[i] && num <= n) {// 누를 수 있는 버튼이고 타겟 숫자보다 작거나 같다면
dfs(cnt+1, num*10+i);
}
}
}
int main() {
cin >> n >> m;
n_size = to_string(n).size();
for (int i=0; i<m; i++) {
int tmp;
cin >> tmp;
button[tmp] = false;
}
answer = abs(n - 100);// 현재 채널에서 바로이동
dfs(0, 0);
cout << answer << "\n";
}