Prime Path
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9982 |
|
Accepted: 5724 |
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
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100001Const intinf=0x7fffffff;//infinitely LargeConst intMAXN =10000;BOOLFLAG[MAXN];intPRIMES[MAXN], pi;structpoint{intx; inty;};voidgetprime_1 () {intI, J; Pi=0; memset (Flag,false,sizeof(flag)); for(i =2; i < MAXN; i++) if(!Flag[i]) {Primes[i]=1;//The prime number is identified as 1 for(j = i; J < maxn; J + =i) flag[j]=true; }}intVIS[MAXN];intMain () {getprime_1 (); intT; CIN>>T; while(t--) {memset (Vis,0,sizeof(VIS)); intn,m; CIN>>n>>m; Vis[n]=1; Queue<point>Q; Q.push (point) {n,0}); intflag1=0; while(!Q.empty ()) { point now=Q.front (); if(now.x==m) {Flag1=now.y; Break; } point Next; for(intI=0; i<=9; i++) {Next.x=now.x/Ten; Next.x*=Ten; Next.x+=i; Next.y=now.y+1; if(next.x< +|| next.x>=10000) Continue; if(vis[next.x]==1) Continue; if(next.x==m) {Flag1=Next.y; Break; } if(primes[next.x]==1) { //cout<<next.x<<endl;vis[next.x]=1; Q.push (next); } } for(intI=0; i<=9; i++) { inttemp=now.x%Ten; Next.x=now.x/ -; Next.x*= -; Next.x+=i*Ten; Next.x+=temp; if(next.x< +|| next.x>=10000) Continue; if(vis[next.x]==1) Continue; if(next.x==m) {Flag1=Next.y; Break; } if(primes[next.x]==1) { //cout<<next.x<<endl;vis[next.x]=1; Q.push (point) {next.x,now.y+1}); } } for(intI=0; i<=9; i++) { inttemp=now.x% -; Next.x=now.x/ +; Next.x*= +; Next.x+=i* -; Next.x+=temp; if(next.x< +|| next.x>=10000) Continue; if(vis[next.x]==1) Continue; if(next.x==m) {Flag1=Next.y; Break; } if(primes[next.x]==1) { //cout<<next.x<<endl;vis[next.x]=1; Q.push (point) {next.x,now.y+1}); } } for(intI=0; i<=9; i++) { inttemp=now.x% +; Next.x=now.x/10000; Next.x*=10000; Next.x+=i* +; Next.x+=temp; if(next.x< +|| next.x>=10000) Continue; if(vis[next.x]==1) Continue; if(next.x==m) {Flag1=Next.y; Break; } if(primes[next.x]==1) { //cout<<next.x<<endl;vis[next.x]=1; Q.push (point) {next.x,now.y+1}); } } if(flag1>0) Break; Q.pop (); } printf ("%d\n", Flag1); } return 0;}
Cd0j/poj 851/3126 Fang teacher with Prime/prime Path BFS