-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPOJ PPATH solution with explanation
107 lines (101 loc) · 2.45 KB
/
SPOJ PPATH solution with explanation
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
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;
const int N = 9999;
bool chk[N];
vector<int> graph[N];
int vis[N], level[N];
void sieve() // Generating the primes
{
int i, j;
for (i = 2; i <= N; i++)
chk[i] = true;
for (i = 2; i * i <= N; i++)
{
if (chk[i] == true)
{
for (j = i * i; j <= N; j += i)
{
chk[j] = false;
}
}
}
}
bool ok(int a, int b) // check weather the primes has difference of one digit or not
{
int cnt = 0;
while (a)
{
if (a % 10 != b % 10)
cnt++;
a /= 10;
b /= 10;
if (cnt > 1)
return false;
}
return true;
}
int bfs(int source, int destination) //applying the bfs
{
queue<int> q;
q.push(source);
vis[source] = 1;
while (!q.empty())
{
int cur = q.front();
q.pop();
for (auto child : graph[cur])
{
if (!vis[child])
{
q.push(child);
vis[child] = 1;
level[child] = level[cur] + 1;
}
}
}
return level[destination];
}
void reset() //reset all the arrays and vector for multi test case
{
for (int i = 0; i < N; i++)
{
vis[i] = 0;
level[i] = 0;
graph[i].clear();
}
}
int main()
{
sieve(); //call the sieve function so that we can use it for all test case hence reduce the time
int t;
cin >> t;
while (t--)
{
reset(); // reset all array before using them
int a, b;
cin >> a >> b;
int i;
vector<int> prime;
for (i = 1000; i <= N; i++) // inserting all the prime between 1000 to 9999 so that we can make a graph using those
{
if (chk[i] == true)
{
prime.push_back(i);
}
}
int j;
for (i = 0; i < prime.size(); i++) //generating the graph for all the prime
{
for (j = 0; j < prime.size(); j++)
{
if (ok(prime[i], prime[j]) && i != j)
{
// we will make a edge between any two prime if and only if those prime has one digit difference from each other.
graph[prime[i]].push_back(prime[j]);
}
}
}
int ans = bfs(a, b);
cout << ans << '\n';
}
}