1003 of ccnu bfs exercises on a simple BFS HDU

Source: Internet
Author: User
Prime Path Time Limit: 2000/1000 ms (Java/Other) Memory Limit: 131072/65536 K (Java/Other) Total Submission (s): 7 Accepted Submission (s ): 5 Problem DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of Security stating that they wowould all have to change the four-digit
Room 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 have 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 will just have to paste four new digits over the four old ones on your office door.
-No, it's not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
-I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
-Correct! Also I must invent a scheme for going from 1033 to 8179 by 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 that the price of a digit is one pound.
-Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
-In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is 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 in step 2 can not be reused in the last step-a new 1 must be purchased.

 

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

OutputOne line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

31033 81791373 80171033 1033
 

Sample Output

670
 

SourcePKU

The question is to give you two four-digit numbers, A and B. Then, each time a can change the number of A's digits in ten or ten digits, and find the minimum number of times that B needs, of course, each time you change the number, the number must be a prime number, if you cannot convert A to B under the condition that each transformation is a prime number, output impossible; otherwise, obtain the minimum number of times, therefore, the simple idea of this question is to search for the new number after each change in the number of digits, whether it is a prime number and whether it is equal to the value of the target B. If it is a prime number, the number of times is + 1, until the expected result is finally obtained, the number of accumulated times is counted, and then how to optimize the analysis. First, it refers to the prime number, therefore, we should first use a bool function to filter out the prime numbers that meet the conditions. during the screening process, remember to change the complexity of the cycle that determines the prime number to the root number n to avoid increasing the computer's computing workload, after filtering the prime number, you can use BFS for search statistics. The queue is still used. Of course, you must pay attention to the value of each bit in detail processing. The single digit must be an odd number, a thousand bits have to start from 1. There are no special requirements for ten hundred bits to change. No Please remember to separate the ten digits from one digit first! Well, I used STL and array simulation to make it. The following code is attached.

#include<iostream>#include<cstring>#include<queue>using namespace std;int a,b;bool check(int n){    if(n==1)        return false;    if(n==2)        return true ;    int i;         for(i=2;i*i<=n;i++)         {   if(n%i==0)             return false;         }       return true;}int num[100005];    int vis[100005];void bfs(){    queue<int> q;    q.push(a);    num[a]=0;    vis[a]=1;   while(!q.empty())   {       int target=q.front();       q.pop();       if(target==b)       {           cout<<num[b]<<endl;           return ;       }       int k,l,i;       k=target%10;       l=(target/10)%10;       for(i=1;i<=9;i+=2)       {           int y=(target/10)*10+i;           if(y!=target&&!vis[y]&&check(y))           {               vis[y]=1;               q.push(y);               num[y]=num[target]+1;           }       }       for(i=0;i<=9;i++)       {           int y=(target/100)*100+i*10+k;           if(y!=target&&!vis[y]&&check(y))           {               vis[y]=1;               q.push(y);               num[y]=num[target]+1;           }       }       for(i=0;i<=9;i++)       {           int y=(target/1000)*1000+100*i+k+l*10;           if(y!=target&&!vis[y]&&check(y))           {               vis[y]=1;               q.push(y);               num[y]=num[target]+1;           }       }       for(i=1;i<=9;i++)       {           int y=target%1000+i*1000;            if(y!=target&&!vis[y]&&check(y))           {               vis[y]=1;               q.push(y);               num[y]=num[target]+1;           }       }   }   cout<<"Impossible"<<endl;}int main(){    int t;    cin>>t;    while(t--)    {        cin>>a>>b;        memset(vis,0,sizeof(vis));        memset(num,0,sizeof(num));        bfs();    }return 0;}

#include<iostream>#include<cstring>using namespace std;bool check(int n){    if(n==1)        return false;    if(n==2)        return true;    int i;    for(i=2;i*i<=n;i++)    {        if(n%i==0)            return false ;    }     return true ;}int a,b;int q[100005];int vis[100005];int num[100005];void   bfs(){    int front=0,rear=0;    q[front]=a;    rear++;    num[a]=0;    vis[a]=1;    while(front<rear)    {        int target=q[front++];        if(target==b)        {            cout<<num[b]<<endl;            return ;        }        int k,l,i;        k=target%10;        l=(target/10)%10;        for(i=1;i<=9;i+=2)        {   int y=(target/10)*10+i;            if(y!=target&&!vis[y]&&check(y))            {                q[rear++]=y;                vis[y]=1;                num[y]=num[target]+1;            }        }        for(i=0;i<=9;i++)        {            int y=(target/100)*100+i*10+k;            if(y!=target&&!vis[y]&&check(y))            {                q[rear++]=y;                vis[y]=1;                num[y]=num[target]+1;            }        }        for(i=0;i<=9;i++)        {            int y=(target/1000)*1000+i*100+l*10+k;            if(y!=target&&!vis[y]&&check(y))            {                q[rear++]=y;                vis[y]=1;                num[y]=num[target]+1;            }        }        for(i=1;i<=9;i++)        {            int y=target%1000+i*1000;            if(y!=target&&!vis[y]&&check(y))            {                q[rear++]=y;                vis[y]=1;                num[y]=num[target]+1;            }        }    }    cout<<"Impossible"<<endl;}int main(){    int t;    cin>>t;    while(t--)    {        cin>>a>>b;        memset(vis,0,sizeof(vis));        memset(num,0,sizeof(num));        bfs();    }    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.