Prime Path--(bfs)

来源:互联网 发布:谁人知我心可 编辑:程序博客网 时间:2024/05/16 14:01

Problem Description
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would 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! So 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.
 

Input
One 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).
 

Output
One 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

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;int a,b,cnt;bool prime[10000];           //素数判断bool use[10][10][10][10];    //是否用过struct node {               //将一个4位数化为4个个位数,a是千位,b是百位......,s是步数    int a,b,c,d,s;};void bfs(){    queue<node> Q;    node t1,t2;    t1.s=0;    t1.a=a/1000;    t1.b=(a/100)%10;    t1.c=(a/10)%10;    t1.d=a%10;    Q.push(t1);    use[t1.a][t1.b][t1.c][t1.d]=true;    while(!Q.empty())    {        t1=Q.front();        Q.pop();        if((t1.a*1000+t1.b*100+t1.c*10+t1.d)==b)        {            cnt=t1.s;            return;        }        else        {            for(int i=1;i<=9;i++)  //试用千位,千位不能为0,其余位上可以为0            {                t2.a=i; t2.b=t1.b; t2.c=t1.c; t2.d=t1.d;                if(use[t2.a][t2.b][t2.c][t2.d]==false)                {                    if(prime[t2.a*1000+t2.b*100+t2.c*10+t2.d]==true)                    {                        use[t2.a][t2.b][t2.c][t2.d]=true;                        t2.s=t1.s+1;                        Q.push(t2);                    }                }            }            for(int i=0;i<=9;i++)            {                t2.a=t1.a; t2.b=i; t2.c=t1.c; t2.d=t1.d;                if(use[t2.a][t2.b][t2.c][t2.d]==false)                {                    if(prime[t2.a*1000+t2.b*100+t2.c*10+t2.d]==true)                    {                        use[t2.a][t2.b][t2.c][t2.d]=true;                        t2.s=t1.s+1;                        Q.push(t2);                    }                }            }            for(int i=0;i<=9;i++)            {                t2.a=t1.a; t2.b=t1.b; t2.c=i; t2.d=t1.d;                if(use[t2.a][t2.b][t2.c][t2.d]==false)                {                    if(prime[t2.a*1000+t2.b*100+t2.c*10+t2.d]==true)                    {                        use[t2.a][t2.b][t2.c][t2.d]=true;                        t2.s=t1.s+1;                        Q.push(t2);                    }                }            }            for(int i=0;i<=9;i++)            {                t2.a=t1.a; t2.b=t1.b; t2.c=t1.c; t2.d=i;                if(use[t2.a][t2.b][t2.c][t2.d]==false)                {                    if(prime[t2.a*1000+t2.b*100+t2.c*10+t2.d]==true)                    {                        use[t2.a][t2.b][t2.c][t2.d]=true;                        t2.s=t1.s+1;                        Q.push(t2);                    }                }            }        }    }}int main(){    int t,i,j;    memset(prime,true,sizeof(prime)); //进行筛素数,一次性处理完素数判断问题    prime[0]=prime[1]=false;    for(i=2;i<10000;i++)    {        if(prime[i])        {            for(j=2*i;j<10000;j+=i)                prime[j]=false;        }    }    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&a,&b);        if(a==b) {printf("0\n"); continue;}        memset(use,false,sizeof(use));        cnt=-1;        bfs();        printf("%d\n",cnt);    }    return 0;}