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;
}
發佈了196 篇原創文章 · 獲贊 316 · 訪問量 200萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章