POJ3126 Prime Path playing table +bfs

Source: Internet
Author: User

Description

The ministers of the Cabinet were quite upset by the message from the chief of Security stating, they would all hav E 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 has 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

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

Title Description

To two four-digit number A, each change a one in a and after the change must be a prime number, ask the minimum number of changes can be to B? (never reach B on output impossible)

Thinking of solving problems

First, hit a prime list, and then use the queue to simulate the search process, the depth of the search into the structure, you can better output the number of changes.

Code
#include <cstdio>#include <cstring>#include <queue>using namespace STD;Const intMAXN =10010;BOOLPRIME[MAXN];BOOLVIS[MAXN];BOOLIsfind;intCntstructnum {intCint  Floor;}; queue<num>Que;num A, B;voidBFsvoid){//1033 8179    //que:2033 3033 ... 0033 1133 1233...1933 1043 1053 ... 1023 1034 ... 1032Que.push (a); while(!que.empty ()) {//p = 1033num p = que.front (); Que.pop ();if(P.c <= +|| !PRIME[P.C] | | VIS[P.C])Continue; VIS[P.C] =true;if(p.c = = B.C) {Isfind =true; CNT = P. Floor;return; } for(inti =1; I <Ten;            i + +) {num tmp; TMP.C = (p.c+ +*i)%10000; Tmp. Floor= P. Floor+1;            Que.push (TMP); TMP.C = (p.c/ +)* ++ (p.c% ++ -*i)% +;            Que.push (TMP); TMP.C = (p.c/ -)* -+ (p.c% -+Ten*i)% -;            Que.push (TMP); TMP.C = (p.c/Ten)*Ten+ (p.c%Ten+i)%Ten;        Que.push (TMP); }    }}intMain () {prime[0] = prime[1] =false; for(inti =2; i < MAXN; i + +) prime[i] =true; for(inti =2; I*i <= MAXN; i + +) {if(!prime[i])Continue; for(intj = i*2; J < Maxn; J + = i) {Prime[j] =false; }    }intTscanf("%d", &t); while(t--) {//InitializeCNT =0; Isfind =false; while(!que.empty ()) Que.pop ();memset(Vis,false,sizeof(VIS));scanf("%d%d", &AMP;A.C,&AMP;B.C);if(A.C = = B.C) {printf("0\n");Continue; } BFS ();if(Isfind)printf("%d\n", CNT);Else printf("impossible\n"); }return 0;}

POJ3126 Prime Path playing table +bfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.