forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(SPOJ) Prime Path.cpp
97 lines (86 loc) · 1.5 KB
/
(SPOJ) 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
#include <bits/stdc++.h>
#define lli long long int
#define endl "\n"
#define MAX 100005
using namespace std;
vector <bool> visited(MAX, false);
vector <lli> dist(MAX, -1);
vector < vector <lli> > v1(MAX);
vector <lli> primes;
bool isprime(lli n)
{
for(lli i =2; i*i <=n; i++)
if(n%i == 0)
return false;
return true;
}
bool valid(lli a, lli b)
{
lli dif = 0;
string s1 = to_string(a);
string s2 = to_string(b);
if(s1.length() != s2.length())
return false;
for(lli i =0; i<4; i++)
if(s1[i] != s2[i])
dif++;
if(dif == 1)
return true;
return false;
}
void buildprimes()
{
for(lli i = 1000; i<10000; i++)
if(isprime(i))
primes.push_back(i);
}
void buildgraph()
{
for(lli i = 0; i<primes.size()-1; i++)
for(lli j = i+1; j<primes.size(); j++)
{
if(valid(primes[i], primes[j])){
v1[primes[j]].push_back(primes[i]);
v1[primes[i]].push_back(primes[j]);
}
}
}
int main()
{
lli t;
cin>>t;
buildprimes();
buildgraph();
while(t--)
{
lli n1, n2;
cin>>n1>>n2;
for(lli i = 1000; i<10000; i++)
{
visited[i] = false;
dist[i] = -1;
}
visited[n1] = true;
dist[n1] = 0;
queue <lli >q1;
q1.push(n1);
while(!q1.empty())
{
lli temp = q1.front();
q1.pop();
for(lli j = 0; j<v1[temp].size(); j++)
{
if(visited[v1[temp][j]] == false)
{
visited[v1[temp][j]] = true;
q1.push(v1[temp][j]);
dist[v1[temp][j]] = dist[temp] + 1;
}
}
}
if(dist[n2] != -1)
cout<<dist[n2]<<endl;
else
cout<<"Impossible"<<endl;
}
}