Description the ministers of the Cabinet were quite upset by the message from the chief of Security stating that they woul D All has the to change the four-digit-numbers on their offices.
-it is a matter of security to change such things every now and then, to keep the enemy in the dark.
-but look, I had chosen my number 1033 for good reasons. I am The Prime minister, you know!
-I know, so therefore your new number 8179 is also a prime. You'll just has to paste four new digits over the four old ones on your office door.
-no, it ' s not so simple. Suppose that I change the first digit to an 8 and then the number would read 8033 which is not a prime!
-I see, being the Prime Minister you cannot stand have a non-prime number on your door even for a few seconds.
-correct! So I must invent a scheme for going from 1033 to 8179 by a path of the prime numbers where only one digit are changed from one Prime to the next prime.
Now, the Minister of Finance, who had been eavesdropping, intervened.
-no unnecessary expenditure, please! I happen to know then the price of a digit is one pound.
-HMM, in this case I need a computer program to minimize the cost. You don ' t know some very cheap software gurus?
-in fact, I do. You see, there are this programming contest going on ... Help the Prime minister to find the cheapest prime path between any and given Four-digit primes! The first digit must be nonzero, of course. Here's a solution in the case above.
1033
1733
3733
3739
3779
8779
8179 the cost of this solution is 6 pounds. Note that the digit 1 which got pasted over step 2 can is reused in the last Step–a new 1 must is purchased.
Input one line with a positive number:the number of the test cases (at most 100). Then for each test case, one line with the numbers separated by a blank. Both numbers is four-digit primes (without leading zeros).
Either with a number stating the minimal cost or containing the word impossible.
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
Test instructions: Change one bit at a time by the first number, changing to a second number takes a few steps (two numbers are primes)
In fact, is to find the initial prime number to the shortest path of the target prime, should use a priority search, the subject is not necessarily used in the queue, because the characteristics of automatic queue sorting is no use, with the array is enough, but may increase the variable to control the current queue situation, there is time to do with an array ...
Attention
(1) The subject is the nature of the graph, not the tree
(2) Due to the large number of data, we should first consider the way to calculate the prime number offline (Sieve method)
(3) When using queue, pay attention to the characteristics of the queue, is to traverse each of the current worth of time, the value is pressed into the current branch, rather than the individual, so the traversal of the time to pay attention to the order of traversal (see illustration below)
is a node-to-node access (including the child of the current node)
(4) I think the essence of the subject, but also as the most distinguishing feature of the tree is the addition of a visit[] array, as the mark has been visited the mark, so that on the one hand will not cause the program of the Dead Loop, on the other hand will meet the conditions of the minimum value, because in the previous has been visited, Certainly smaller than the current
#include <iostream> #include <stdio.h> #include <queue> #include <math.h> #include <string.h
> #define N 10010 using namespace std; BOOL Isprm[n],visit[n];//isprm[n] is used to record whether it is a prime number, visit[] is used to record whether it has been traversed int path_c[n];
The length of the record path is void GETPRM () {//The Prime method (Fast sieve method) int s,e=sqrt ((double) N) +1;//sqrt is for double number open square memset (isprm,1,sizeof (ISPRM));
isprm[0]=isprm[1]=0;
for (int i=4;i<n;i+=2) isprm[i]=0;
for (int i=3;i<e;i+=2) if (Isprm[i]) for (int s=i*2,j=i*i;j<n;j+=s) isprm[j]=0;
} int main () {int n,m,t;
GETPRM ();
scanf ("%d", &t);
while (t--) {int mark=0;
Queue<int> Q;
cin>>n>>m;
memset (visit,false,sizeof (visit));
memset (path_c,0,sizeof (Path_c));
Q.push (n);//The first data is pressed into the queue visit[n]=true;//the point traverses the while (!q.empty ()) {if (mark==1) break; int Temp=q.front ();//Take out the first, out-of-team element Q.pop ();//Eject if (temp==m)break;//up to M, stop for (int i=1;i<=1000;i=i*10) {//4 positions, Chichong bit last 0-9 BFS traversal, I determines which if (Mark==1) BRE is currently detached
Ak int d=temp/i%10,w=temp-d*i;
The number of the current bit is separated, D is the number of bits separated out//removed from the bit after the number for (int j=0;j<10;j++) {//each one on the 0-9 BFS traversal, J is to replace the number of the current bit
if (i==1000&&j==0) continue;//the highest bit is not 0 if (j!=d) {//Not the number on the original bit int now=j*i+w;
Produces a new number if (Isprm[now]&&!visit[now]) {//isprm[now] If it is a prime and!visit[now] has not been accessed Q.push (now);//The queue path_c[now]=path_c[temp]+1;//on the original base plus 1, the sign traversed the path length if (now==m
) {mark=1;
Break
} visit[now]=true;//flag visited}}}}
printf ("%d\n", Path_c[m]);
} return 0;
}
Notice the comparison between the two ways, the following situation I feel is not very good understanding, because after finding the value to find will continue to find until the node will stop, in fact, has encountered the current value, there is no need to continue after encountering the value to be found, until the end of the visit, or a better face, I feel that I write more concise time-saving
int main () {int n,m,t;
GETPRM ();
scanf ("%d", &t);
while (t--) {int mark=0;
Queue <int> q;
cin>>n>>m;
memset (visit+900, false,sizeof (visit));
memset (path_c+900,0,sizeof (Path_c));
Q.push (n);
Visit[n]=true;
while (!q.empty ()) {if (mark==1) is break;
int Temp=q.front ();
Q.pop ();
if (temp==m) break;
for (int i=1;i<=1000;i=i*10) {if (mark==1) break;
int d=temp;
d=d/i;
d=d%10;
int w=temp-d*i;
for (int j=0;j<10;j++) {if (i==1000&&j==0) continue;
if (j!=d) {int now=j*i+w;
if (Isprm[now]&&!visit[now]) {Q.push (now);
path_c[now]=path_c[temp]+1;
if (now==m) {mark=1;
Break
} visit[now]=true;
}}}}} printf ("%d\n", Path_c[m]);
} return 0;
}
Read before the code is a little funny, how can violence 1000 and then separated out, is also drunk, rewrite a update under:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack > #include <string> #include <map> #include <set> #include <ctime> #define EPS 1e-6 #define MAX 10005 #define INF 0x3f3f3f3f #define LL Long long #define PII pair<int,int> #define RD (x) scanf ("%d", &x) #define Rd2 (x, y) scanf ("%d%d", &x,&y) #define RD3 (x, Y, z) scanf ("%d%d%d", &x,&y,&z)///map<int,int>
Mmap
Map<int,int >::iterator it;
using namespace Std;
BOOL Isprm[max];
BOOL Vis[max];
BOOL Getprm () {isprm[0]=isprm[1]=0;
for (int i=2;i<max;i++) if (Isprm[i]) for (int j=i*i;j<max;j+=i) isprm[j]=0;
/* for (int i=0;i<max;i++) if (Isprm[i]) cout<<i<< "; */} struct str{int num;
int step; Str () {} str (int num,int step) {This->num= Num,this->step = step;
}
};
int pow (int time) {int num=1;
for (int i=0;i<time;i++) num*=10;
return num;
} int main () {memset (isprm,1,sizeof (ISPRM));
GETPRM ();
int t,start,eend;
RD (T);
while (t--) {memset (vis,0,sizeof (VIS));
int res=inf;
scanf ("%d%d", &start,&eend);
if (start==eend) {printf ("0\n");
Continue
} queue<str>que;
Que.push (STR (start,0));
Vis[start];
while (!que.empty () &&res==inf) {Str tmp = Que.front ();
Que.pop ();
for (int i=3;i>=0&&res==inf;i--) {int j=0;
if (i==3) j=1;
int bit = tmp.num/(int) pow (i)%10;
for (; j<=9;j++) {if (j==bit) continue;
int newnum = tmp.num-(bit-j) *pow (i);
if (Isprm[newnum]) {if (Newnum = = eend) {res=tmp.step+1;
Break
} if (!vis[newnum]) {Que.push (STR (newnum,tmp.step+1));
Vis[newnum]=1;
}}}}} if (Res==inf) printf ("impossible\n");
else printf ("%d\n", res);
} return 0; }