Description
The ministers of the Cabinet were quite upset by the message from the chief of Security stating, they would all has T o 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 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).
Output
Either with a number stating the minimal cost or containing the word impossible.
Sample Input
31033 81791373 80171033 1033
Sample Output
670
Test instructions is to give you two four-digit prime n,m, no leading 0, let you change one number at a time, so that n becomes m, ask the minimum number of times. This problem is typical of BFS. Because it is a 4-digit number, it is OK to judge the primes by 2~100.
#include <stdio.h> #include <string.h>int num,n,m;int a[10];int q[1111111][2],vis[50000]; Q[][0] number, q[][1] number of int prime (int n) {int i,flag=1;for (i=2;i<=100;i++) {if (n%i==0) {Flag=0;break;}}if (flag) return 1;else return 0;} int BFs () {int front=1,rear=1,x,t,i,xx,y;memset (vis,0,sizeof (VIS));Vis[n]=1;q[front][0]=n;q[front][1]=0;while (Front<=rear){X=Q[FRONT][0];if (x==m) return q[front][1];front++;Vis[x]=1;t=0;Xx=x;while (XX) {a[++t]=xx%10;XX=XX/10;}for (i=1;i<=9;i++) {Y=I*1000+A[3]*100+A[2]*10+A[1];if (vis[y]==0 && Prime (y)) {Vis[y]=1;rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;}}for (i=0;i<=9;i++) {Y=I*100+A[4]*1000+A[2]*10+A[1];if (vis[y]==0 && Prime (y)) {Vis[y]=1;rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;}}for (i=0;i<=9;i++) {Y=A[4]*1000+A[3]*100+I*10+A[1];if (vis[y]==0 && Prime (y)) {Vis[y]=1;rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;}}for (i=0;i<=9;i++) {Y=a[4]*1000+a[3]*100+a[2]*10+i;if (vis[y]==0 && Prime (y)) {Vis[y]=1;rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;}}}}int Main () {int t,i,j;scanf ("%d", &t);while (t--){scanf ("%d%d", &n,&m);printf ("%d\n", BFS ());}return 0;}
poj3126 Prime Path