-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3639 Prime Path.cpp
99 lines (94 loc) · 2.22 KB
/
3639 Prime Path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//============================================================================
// Name : Riham.cpp
// Author : Remo
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double Ld;
#define r(n) scanf("%d",&n)
#define rL(n) scanf("%I64d",&n)
#define p(n) printf("%d\n",n)
#define pL(n) printf("%I64d",n)
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Set(v,d) memset(v, d, sizeof(v))
#define oo 1001 ///infinity
/** ---------------------------Declarations--------------------------- **/
int T,n,m,save;
const int Upperbound=10001;
bitset<Upperbound+1>prime;
/** ----------------------------Functions----------------------------- **/
void Sieve(){
prime.set();
prime[0]=prime[1]=0;
for(int i=2;i*i<=Upperbound;i++)
if(prime[i])
for(int j=i*i;j<=Upperbound;j+=i)
prime[j]=0;
}
void BFS(int s){
queue<int>q;
vector<int>d(Upperbound,oo);
q.push(s);
d[s]=0;
while(!q.empty()){
int v=q.front(); q.pop();
if(v == m){ save=d[v]; break; }
int num=v;
For(i, 0, 9){
int indx=(num/10)*10+i;
if(indx>=1000 && prime[indx] && d[indx]==oo){
q.push(indx);
d[indx]=d[v]+1;
}
indx=(num/100)*100+i*10+(num%10);
if(indx>=1000 && prime[indx] && d[indx]==oo){
q.push(indx);
d[indx]=d[v]+1;
}
indx=(num/1000)*1000+i*100+(num%100);
if(indx>=1000 && prime[indx] && d[indx]==oo){
q.push(indx);
d[indx]=d[v]+1;
}
indx=i*1000+num%1000;
if(indx>=1000 && prime[indx] && d[indx]==oo){
q.push(indx);
d[indx]=d[v]+1;
}
}
}
}
ll Ipow(int base, int exp)
{ll result = 1;
while(exp)
{if(exp & 1)result*=base;
exp>>=1;base*=base;}
return result;}
//**********************************************************************************//
int main() {
//freopen("Output.txt","w",stdout);
Sieve();
r(T);
while(T--){
r(n),r(m);
if(n == m){
puts("0");
continue;
}
int j=n, k=m,Once=0;
while(j){
if(j%10 != k%10)
Once++;
j/=10, k/=10;
}
if(Once == 1){
puts("1");
continue;
}
BFS(n);
save? p(save) : puts("Impossible") ;
}
return 0;
}