forked from pranavanurag/SPOJSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPATH.cpp
More file actions
113 lines (97 loc) · 1.91 KB
/
Copy pathPPATH.cpp
File metadata and controls
113 lines (97 loc) · 1.91 KB
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
108
109
110
111
112
113
#include <iostream>
#include <cmath>
#include <string.h>
#include <time.h>
#include <algorithm>
#include <stdio.h>
#include <queue>
using namespace std;
#define scan(x) scanf("%d", &x)
#define print(x) printf("%d\n", x)
#define INF 100000000
bool Prime[10001];
bool Visited[10001];
int Level[10001];
void Initialize()
{
for (int i = 1; i <= 10000; i++)
{
Visited[i] = 0;
Level[i] = -1;
}
}
void Seive()
{
for (int i = 2; i <= 10000; i++)
Prime[i] = 1;
for (int i = 2; i <= 100; i++)
if (Prime[i])
for (int j = i*i; j <= 10000; j += i)
Prime[j] = 0;
}
int Construct(int Digits[])
{
int ans = 0;
for (int i = 1; i <= 4; i++)
ans = ans*10 + Digits[i];
return ans;
}
int PPATH(int p1, int p2)
{
int NewNumber, CurrentPrime;
queue <int> toExplore;
toExplore.push(p1);
Level[p1] = 0;
while(!toExplore.empty())
{
CurrentPrime = toExplore.front();
toExplore.pop();
Visited[CurrentPrime] = 1;
cout<<"From "<<CurrentPrime<<". Can jump to -> ";
int CurrentPrime_Copy = CurrentPrime;
if (CurrentPrime == p2)
break;
int Digits[5];
for (int i = 4; i >= 1; i--)
{
Digits[i] = CurrentPrime_Copy%10;
CurrentPrime_Copy /= 10;
}
for (int i = 1; i <= 4; i++)
{
int Digits_Copy[5];
for (int j = 1; j <= 4; j++)
Digits_Copy[j] = Digits[j];
for (int d = 0; d <= 9; d++)
if (!(i == 1 && d == 0))
{
Digits_Copy[i] = d;
NewNumber = Construct(Digits_Copy);
if (Prime[NewNumber] && NewNumber != CurrentPrime && !Visited[NewNumber])
{
toExplore.push(NewNumber);
cout<<NewNumber<<" ";
if (Level[NewNumber] == -1)
Level[NewNumber] = Level[CurrentPrime] + 1;
else
Level[NewNumber] = min(Level[NewNumber], Level[CurrentPrime] + 1);
}
}
}
cout<<endl;
}
return Level[p2];
}
int main()
{
Seive();
int n, p1, p2;
scan(n);
while (n--)
{
Initialize();
scan(p1); scan(p2);
print(PPATH(p1, p2));
}
return 0;
}