天天看点

POJ_3126_Prime Path(数论+BFS)

Prime Path

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9509 Accepted: 5450

Description

POJ_3126_Prime Path(数论+BFS)

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

3
1033 8179
1373 8017
1033 1033      

Sample Output

6
7
0      

Source

Northwestern Europe 2006

题型:数论+搜索

题意:

       输入两个四位数素数a、b,现在对a进行变换,每次只能变换一位,并且每次变换之后也是素数。求将a变到b最少需要多少步。

       比如1033->1733->3733->3739->3779->8779->8179,即从1033 → 8179至少需要6步;

分析:

        果断BFS,搜索出每一位上应该是什么。先筛出10000以内的素数以便用于判断。然后从给定的数开始,逐个改变然后从给定的数开始,逐个改变该数的不同位数上的数字,如果是素数,则将这个数入队,同时步数加1,如果在队列非空时能找到目标素数,则输出此时的步数,退出搜索,否则输出Impossible。该数的不同位数上的数字,如果是素数,则将这个数入队,同时步数加1,如果在队列非空时能找到目标素数,则输出此时的步数,退出搜索,否则输出Impossible。

代码:

#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int p[12345];
int a,b,flag;
int pos[12345],mark[12345];
int step;
void isprime(){
    memset(p,1,sizeof(p));
    p[0]=0;
    p[1]=0;
    for(int i=2;i*i<=12340+1;i++){
        if(!p[i]) continue;
        for(int j=(i<<1);j<=12340;j+=i)
            p[j]=0;
    }
}

void bfs(){
    memset(pos,0,sizeof(pos));
    memset(mark,0,sizeof(mark));
    queue<int> q;
    while(!q.empty()) q.pop();
    q.push(a);
    mark[a]=1;
    while(!q.empty()){
        if(q.front()==b){
            flag=1;
            printf("%d\n",pos[b]);
            break;
        }
        int tmp=q.front();
        q.pop();
        for(int i=0;i<=9;i++){
            if(p[tmp/10*10+i]&&!mark[tmp/10*10+i]){   //个位
                mark[tmp/10*10+i]=1;
                q.push(tmp/10*10+i);
                pos[tmp/10*10+i]=pos[tmp]+1;
            }
            if(p[tmp/100*100+i*10+tmp%10]&&!mark[tmp/100*100+i*10+tmp%10]){   //十位
                mark[tmp/100*100+i*10+tmp%10]=1;
                q.push(tmp/100*100+i*10+tmp%10);
                pos[tmp/100*100+i*10+tmp%10]=pos[tmp]+1;
            }
            if(p[tmp/1000*1000+i*100+tmp%100]&&!mark[tmp/1000*1000+i*100+tmp%100]){  //百位
                mark[tmp/1000*1000+i*100+tmp%100]=1;
                q.push(tmp/1000*1000+i*100+tmp%100);
                pos[tmp/1000*1000+i*100+tmp%100]=pos[tmp]+1;
            }
            if(p[i*1000+tmp%1000]&&!mark[i*1000+tmp%1000]&& tmp%1000+i*1000>=1000){  //千位,注意不能是0
                mark[i*1000+tmp%1000]=1;
                q.push(i*1000+tmp%1000);
                pos[i*1000+tmp%1000]=pos[tmp]+1;
            }
        }
    }
}

int main(){
    isprime();
    int n;
    while(~scanf("%d",&n)){
        while(n--){
            flag=0;
            scanf("%d%d",&a,&b);
            bfs();
            if(!flag) printf("Impossible\n");
        }
    }
    return 0;
}