知识点:广搜,质数
比较简单的广搜,算是抽象的状态图的搜索,因为是最短次数,所以可以使用广搜
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int h[N], s, e;
void init() {
for (int i = 2; i <= 9999; i++) {
if (!h[i]) {
for (int j = i + i; j <= 9999; j += i) {
h[j] = 1;
}
}
}
}
void bfs() {
queue<int> q;
q.push(s);
int dist[N];
memset(dist, -1, sizeof(dist));
dist[s] = 0;
while (!q.empty()) {
int now = q.front(); q.pop();
if (now == e) { cout << dist[e] << '\n'; return; }
int x1 = 1, x2 = 10;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
int t = now / x2 * x2 + now % x1 + j * x1;
if (t < 1000 || h[t] || dist[t] != -1) continue;
q.push(t);
dist[t] = dist[now] + 1;
}
x1 *= 10; x2 *= 10;
}
}
cout << "Impossible\n";
}
int main() {
init();
int T;
cin >> T;
while (T--) {
cin >> s >> e;
bfs();
}
return 0;
}