-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prime Path.cpp
77 lines (73 loc) · 2.47 KB
/
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
//AC IN one go // spoj PPATH //
/*the concept is simple we just have to find all the four digit combination
of the prime numbers by replacing a digit at a time and putting them at the same level of tree
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
bool prime[10000+1];
void sieve(ll n){
memset(prime,true,sizeof(prime));
for(int i=2;i*i<=n;i++){
if(prime[i]){
for(int k=i*i;k<=n;k+=i){
prime[k]=false;
}
}
}
}
int main(){
sieve(10000);
ll T;
cin>>T;
while(T--){
ll N,M;
cin>>N>>M;
queue<pair<ll,ll> > q;
q.push(make_pair(N,0));
bool mark[10000];
memset(mark,false,sizeof(mark));//marking the integers so that they can be used only once
while(!q.empty()){
ll top=q.front().first;
if(top==M){
cout<<q.front().second<<'\n';
break;
}
else{
ll ones=top%10;
ll tens=(top/10)%10;
ll hun=(top/100)%10;
ll th=(top/1000)%10;
for(int i=0;i<=9;i++){
ll num=th*1000+hun*100+tens*10+i;//hitting the values at ones place
if(mark[num]==false&&prime[num]){
q.push(make_pair(num,q.front().second+1));
mark[num]=true;
}
}
for(int i=0;i<=9;i++){
ll num=th*1000+hun*100+i*10+ones;//hitting the values at tens place
if(mark[num]==false&&prime[num]){
q.push(make_pair(num,q.front().second+1));
mark[num]=true;
}
}
for(int i=0;i<=9;i++){
ll num=th*1000+i*100+tens*10+ones;//hitting the values at hundredth place
if(mark[num]==false&&prime[num]){
q.push(make_pair(num,q.front().second+1));
mark[num]=true;
}
}
for(int i=1;i<=9;i++){
ll num=i*1000+hun*100+tens*10+ones;//hitting the values at thousandth place we can't hit 0 in this
if(mark[num]==false&&prime[num]){
q.push(make_pair(num,q.front().second+1));
mark[num]=true;
}
}
}
q.pop();
}
}
}