-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25jan_primePath.cpp
98 lines (79 loc) · 2.16 KB
/
25jan_primePath.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
//{ Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
vector<string> primes;
bool computed = 0;
class Solution{
public:
void sieve(){
int n = 9999;
vector<bool> prime(n + 1, 1);
prime[0] = prime[1] = 0;
for(int i = 2; i * i <= n; i++){
if(prime[i]){
for(int j = i * i; j <= n; j += i)
prime[j] = 0;
}
}
for(int i = 1000; i < n + 1; i++){
if(prime[i])
primes.push_back(to_string(i));
}
computed = 1;
}
int solve(int Num1,int Num2)
{
if(Num1 == Num2)
return 0;
if(!computed){
sieve();
}
const int inf = 1e9;
map<string,int> d;
for(auto i : primes)
d[i] = inf;
string start = to_string(Num1);
string end = to_string(Num2);
d[start] = 0;
queue<string> q;
q.push(start);
while(!q.empty()){
string cur = q.front();
q.pop();
for(int i = 0; i < 4; i++){
for(char j = (i == 0 ? '1' : '0'); j <= '9'; j++){
if(j != cur[i]){
string next = cur;
next[i] = j;
auto ptr = d.find(next);
if(ptr != d.end() and ptr -> second > d[cur] + 1){
ptr -> second = d[cur] + 1;
q.push(next);
if(next == end)
return d[next];
}
}
}
}
}
return -1;
}
};
//{ Driver Code Starts.
signed main()
{
int t;
cin>>t;
while(t--)
{
int Num1,Num2;
cin>>Num1>>Num2;
Solution obj;
int answer=obj.solve(Num1,Num2);
cout<<answer<<endl;
}
}
// } Driver Code Ends