forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppath.cpp
82 lines (82 loc) · 1.48 KB
/
ppath.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
// 2008-08-04
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <queue>
using namespace std;
int deg[10000];
static int adj[10000][50];
bool prime[10000];
void sieve()
{
memset(prime,true,sizeof(prime));
int i,j;
for (i=2; i<10000; i++)
{
while (i<10000&&!prime[i])
i++;
if (i==10000)
return;
for (j=2*i; j<10000; j+=i)
prime[j]=false;
}
}
void build()
{
int i,j;
for (i=1000; i<10000; i++)
{
if (!prime[i])
continue;
deg[i]=0;
int d1=i/1000;
int d2=i/100%10;
int d3=i/10%10;
int d4=i%10;
for (j=1; j<10; j++)
if (prime[1000*j+100*d2+10*d3+d4])
adj[i][deg[i]++]=1000*j+100*d2+10*d3+d4;
for (j=0; j<10; j++)
if (prime[1000*d1+100*j+10*d3+d4])
adj[i][deg[i]++]=1000*d1+100*j+10*d3+d4;
for (j=0; j<10; j++)
if (prime[1000*d1+100*d2+10*j+d4])
adj[i][deg[i]++]=1000*d1+100*d2+10*j+d4;
for (j=0; j<10; j++)
if (prime[1000*d1+100*d2+10*d3+j])
adj[i][deg[i]++]=1000*d1+100*d2+10*d3+j;
}
}
typedef pair<int,int> pii;
bool here[10000];
int main()
{
sieve();
build();
int x,y,t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&x,&y);
memset(here,false,sizeof(here));
queue<pii> Q;
Q.push(pii(x,0));
pii p;
while (!Q.empty())
{
p=Q.front();
Q.pop();
if (here[p.first])
continue;
here[p.first]=true;
if (p.first==y)
break;
for (i=0; i<deg[p.first]; i++)
Q.push(pii(adj[p.first][i],p.second+1));
}
if (p.first==y)
printf("%d\n",p.second);
else
printf("Impossible\n");
}
return 0;
}