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

题意:
给定两个四位素数a b,要求把a变换到b。要求每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数与前一步得到的素数只能有一个位不同。求从a到b最少需要的变换次数。无法变换则输出Impossible

题解:
首先呢,我们会发现这道题是用bfs解决的。当有满足的条件的则放入队列,由于每步走过后不能重复走过(其实是重复了也没价值啊,难道要转圈圈吗~),所以我们还需要设一个标记数组。每一步都需要判素~其实打表也可以~随你~

#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

bool vis[10000];

struct node{
    int x, step;
};

bool pan(int num )
{
        //两个较小数另外处理
        if(num ==2|| num==3 )
                return 1 ;
        //不在6的倍数两侧的一定不是质数
        if(num %6!= 1&&num %6!= 5)
                return 0 ;
        int tmp =sqrt(num);
        //6的倍数两侧的也可能不是质数
        for(int i= 5;i <=tmp; i+=6 )
        if(num %i== 0||num %(i+ 2)==0 )
                return 0 ;
        //排除所有,剩余的是质数
        return 1 ;
}

int bfs(int x, int y){
    queue<node> q;
    int i, j;
    q.push(node{x, 0});
    vis[x] = 1;
    node a, ne;
    while(!q.empty()){
        a = q.front();
        q.pop();
        if(a.x == y){
            //cout << a.step << endl;
            return a.step;
        }
            int c, temp, l;
        for(i = 0; i < 4; i++){
            for(j = 0; j < 10; j++){
                temp = a.x;
                if(i == 0){
                    c = temp/10*10+j;
                    if(!vis[c] && pan(c)){
                        ne.x = c; ne.step = a.step +1;
  //cout << i << ' ' << j << ' ' << "ne.x " << ne.x << "ne.step " << ne.step << endl;
                        vis[c] = 1;
                        q.push(ne);
                    }
                }
                if(i == 1){
                    l = temp%10;
                    c = temp/100*100 + l + j*10;
                    if(!vis[c] && pan(c)){
                        ne.x = c; ne.step = a.step + 1;
                        vis[c] = 1;
                            //    cout << i << ' ' << j << ' ' << "ne.x " << ne.x << "ne.step " << ne.step << endl;
                        q.push(ne);
                    }
                }
                if(i == 2){
                    l = temp%100;
                    c = temp/1000*1000 + l + j*100;
                        if(!vis[c] && pan(c)){
                            ne.x = c; ne.step =a.step +1;
                            vis[c] = 1;
 // cout << i << ' ' << j << ' ' << "ne.x " << ne.x << "ne.step " << ne.step << endl;;
                            q.push(ne);
                    }
                }
                if(i == 3){
                    l = temp%1000;
                    if(j!=0){
                        c = j*1000 + l;
                        if(!vis[c] && pan(c)){
                            ne.x = c; ne.step = a.step + 1;
               // cout << i << ' ' << j << ' ' << "ne.x " << ne.x << "ne.step " << ne.step << endl;
                            vis[c] = 1;
                            q.push(ne);
                        }
                    }
                }
            }
        }
    }
    return -1;
}

int main(){
    int t, n, m, s;
    scanf("%d", &t);
    while(t--){
        memset(vis, 0, sizeof(vis));
        scanf("%d%d", &n, &m);
        s = bfs(n, m);
        if(s != -1){
            printf("%d\n", s);
        }
        else{
            printf("Impossible\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ling_wang/article/details/81366465