POJ 3126 Prime Path (BFS) __ Search

Source: Internet
Author: User
Description

The ministers of the Cabinet were quite upset by the "Chief of security stating" they would all have t o change the Four-digit room numbers on their offices.

The-it is a matter of the such things every now and then, to keep the enemy.

-but look, I have chosen me 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 have to paste four new digits over the four old ones on your office door.

-no, it ' s not this simple. Suppose I change the "I" to "a" 8, then the number would read 8033 which be not a prime!

-I-being the prime Minister you cannot stand has a non-prime number on your door even for a few.

-correct! So I must invent a scheme for going from 1033 to 8179 through a path of prime numbers where only one digit is 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 this price of a digit is one pound.

-HMM, in which case I need a computer program to minimize the cost. You don ' t know some very cheap software gurus.

-in fact, I do. You are there are this programming contest going ... Help the prime minister the cheapest prime path between any two given four-digit primes! The digit must be nonzero, of course. Here are 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 the "in" Step 2 can is reused in the last Step–a new 1 must to be purchased.


Input

One line with a positive number:the number of test cases (at most 100). Then for each test case, the one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading).


Output

One line for each case, either with a number stating the minimal or containing the word impossible.


Sample Input

3
1033 8179
1373 8017
1033 1033


Sample Output

6
7
0


the

Here you go. N,m is a prime number, the number of steps to change from N to M, which stipulates that each step can only change a China bit (thousands cannot be 0), and the number of variations is also prime.


train of Thought

First, make a list of primes, play all prime numbers (or 1000-10000) within 10000, and then BFS.

For n change every bit of the Chichong, after the change is detected as prime, if, join the queue, and mark the access, and then the number of queues in the same operation, until a change to produce m, the output of the least number of transformations.

If it is not possible to reach M, it will exit after all prime numbers are traversed between [1000-9999].


AC Code

#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <
Math.h> #include <iostream> using namespace std;
#include <vector> #include <queue> #define MAX 10000 int Prime[max];    BOOL Isvis[max];
    Numbers that have been accessed do not need to rejoin the queue void IsPrime ()//prime number dozen {prime[0]=prime[1]=0;
    Prime[2]=1;
    for (int i=3; i<max; i++) prime[i]=i%2==0?0:1;
    int t= (int) sqrt (max*1.0); for (int i=3; i<=t; i++) if (prime[i) for (int j=i*i; j<max; j+=2*i)//optimizing PRIME[J]
= 0;
    } struct point {int data;   int time;
        Current to step (int data,int time) {this->data=data;
    this->time=time;
}
};
    int solve (point A,int b) {memset (isvis,false,sizeof (Isvis));
    queue<point>sk;
    Sk.push (a);
        while (!sk.empty ()) {point P=sk.front ();
        Sk.pop ();
        if (p.data==b) return p.time;
        Isvis[p.data]=true; for (int i=10; i<=10000;
                I*=10)//from low to high enumeration {for (int j= (i==10000)? 1:0; j<=9; j + +)//MAX bit cannot be 0 {    int num=p.data/i*i+i/10*j+p.data% (I/10);
            Calculates the current number if (!isvis[num]&&prime[num]) Sk.push (Point (num,p.time+1));
}} return-1;
    int main () {isprime ();
    int n;
        while (~SCANF ("%d", &n)) {int a,b;
            for (int i=0; i<n; i++) {scanf ("%d%d", &a,&b);
            int Ans=solve (Point (a,0), b); printf (ans==-1?)
        impossible\n ":"%d\n ", ans);
} return 0; }

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.