POJ 3126 Prime Path (BFS)

Source: Internet
Author: User

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13813 Accepted: 7796

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 that given two four-bit primes a B is asked to change from a to B at least several times.
The requirement for transformation is that only one number can be changed at a time, and the four-digit number that the intermediate process obtains must also be prime.
Because it is mentioned that the minimum number of transformations, it is easy to think of BFS,BFS first search must be the shortest steps.

First, hit a prime list.
Then write a function to determine the number of two four digits is different, if only one bit, return true, otherwise return false
And then it wa two times!
The following table is wrong!
Pri[k++]=i, the first to Pri[k] assignment, and then k++;
Pri[++k]=i is the first increment, then the assignment. This is a mistake. So wa .... sad
/************************************************************************* > File name:code/2015summer/  Searching/f.cpp > Author:111qqz > Email: [email protected] > Created time:fri 01:16:23 AM CST ************************************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intN =1e4+5;intPri[n],which[n];inta,b,k;BOOLFlag;intD[n];BOOLPrimeintx) {     for(inti =2; i*i<=x; i++ )    {      if(x%i==0)return false; }    return true;}BOOLOk (intXinty) {    if(d[y]!=-1)return false; intres =0;//record the number of two numbers that do not correspond to an unequal number    intxx=x,yy=y;  while(x&&y) {if(%Ten!=y%Ten) res++; X= x/Ten; Y= y/Ten; } //if (res==1) cout<< "x:" <<xx<< "y:" <<yy<<endl;    if(res==1)return true; return false;}voidBFs () {Queue<int>x; memset (d,-1,sizeof(d));    X.push (a); D[a]=0;  while(!X.empty ()) {      intPX =X.front ();//cout<< "px:" <<px<<endl;X.pop (); if(px==b)return;  for(inti =0; i< K; i++ )      {        if(OK (Px,pri[i])) {D[pri[i]]=d[px]+1;        X.push (Pri[i]); }      }    }}intMain () {k=0;  for(inti = +; I <=9999; i++ )    {      if(Prime (i)) {pri[k++]=i; }    }    intT; //cout<<pri[0]<<endl; //cout<<pri[1]<<endl;Cin>>T;  while(t--) {cin>>a>>b;      BFS (); if(d[b]==-1) {cout<<"Impossible"<<Endl; }      Else{cout<<d[b]<<Endl; }    }      return 0;}

POJ 3126 Prime Path (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.