POJ-3126 Prime Path

来源:互联网 发布:小学一年级数学软件 编辑:程序博客网 时间:2024/06/14 10:10
Prime Path
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20932 Accepted: 11648

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

Source

Northwestern Europe 2006
题意:给一个四位数,要求变换成另外一个四位数,每次只能改变一个数字,且改变后的数字必须为素数,问从给定四位数变换到目标数字最少需要几次变化。如果无法到达则输出Impossible。
思路:BFS,从目标数字开始依次改变个位数十位数百位数和千位数,其中个位数一定不是偶数且千位数不能为零,每改变一次则判断是否是素数,再判断是否等于目标数,如果等于目标数则输出结果,否则加入队列等待下一次BFS。素数可以采用函数判断或者先对1000到10000间的数字进行素数打表(然而我最开始就是用的函数判断法,一直WA,没有素数判断算法,改为素数打表一下就AC了比较迷),这里的数字比较小,所以只用普通的素数判断算法就可以了,不用素数筛也不会TLE。
#include <iostream>#include <cmath>#include <cstring>#include <queue>#define mst(b,ss) memset(ss,b,sizeof(ss))#define maxn 10005using namespace std;bool visit[maxn];int step[maxn];int prime[maxn];int n,m;queue <int> q;bool isprime(int n){    for(int i=2;i<=sqrt(n);i++)    {        if (n%i==0)            return false;    }    return true;}void init(){    for (int i=1000;i<10000;i++)        if(isprime(i))            prime[i]=1;}void bfs(int n){    step[n]=0;    visit[n]=true;    q.push(n);    while(!q.empty())    {        int X=q.front();        q.pop();        if(X==m)        {            cout << step[m] << endl;            return ;        }        for(int i=1;i<=9;i+=2)        {            int temp=X/10*10+i;            if(!visit[temp]&&prime[temp])            {                visit[temp]=true;                step[temp]=step[X]+1;                q.push(temp);            }        }        for(int i=0;i<=9;i++)        {            int temp=X/100*100+i*10+X%10;            if(!visit[temp]&&prime[temp])            {                visit[temp]=true;                step[temp]=step[X]+1;                q.push(temp);            }        }        for(int i=0;i<=9;i++)        {            int temp=X/1000*1000+i*100+X%100;            if(!visit[temp]&&prime[temp])            {                visit[temp]=true;                step[temp]=step[X]+1;                q.push(temp);            }        }        for(int i=1;i<=9;i++)        {            int temp=i*1000+X%1000;            if(!visit[temp]&&prime[temp])            {                visit[temp]=true;                step[temp]=step[X]+1;                q.push(temp);            }        }    }    cout << "Impossible" << endl;}int main(){    int t;    cin >> t;    init();    while(t--)    {        cin >> n >> m;        memset(visit,false,sizeof(visit));        memset(step,0,sizeof(step));        while(!q.empty())   q.pop();        bfs(n);    }    return 0;}



原创粉丝点击