bfs + 哈希表可过
#include <iostream>
#include <queue>
#include <unordered_set>
using namespace std;
typedef long long LL;
LL n, x;
unordered_set<LL> s;
int bfs() {
int cnt = 0;
queue<LL> q;
q.push(x);
while (!q.empty()) {
++cnt;
int sz = q.size();
for (int i = 0; i < sz; ++i) {
LL t = q.front(), c = t;
q.pop();
int digit_cnt = 0;
while (c) {
++digit_cnt;
int u = c % 10;
if (!s.count(u * t)) {
s.insert(u * t);
q.push(u * t);
}
c /= 10;
}
if (digit_cnt == n) return cnt - 1;
}
}
return -1;
}
int main() {
cin >> n >> x;
cout << bfs() << endl;
return 0;
}