poj 3126 Prime Path problem solution

Reference blog https://www.xuebuyuan.com/2962927.html

27 No. 28 climbing out of the hand = = not good with computers, so cuckoo for two days ( by no means a lazy do not want to write ), put on make up less subject today.

 

poj 3126 subject to the effect from a four-digit prime number to another prime number, one through a change (a change from plus 1), in the middle can only go prime number, ask the shortest distance?

 

In fact, it is clear that the idea of ​​using BFS to do, just give us added qualification (only prime number), but there are four of every 10 kinds of way to go. I wanted to start solving the problem when this is the case every number there are 40 road can go = =, emm feel big, but like any good for this optimization method does not, can only bite the bullet to write if there is anything else to write good way to welcome in the comments.

In addition to this problem is a good example to learn prime number sieve

Attached https://blog.csdn.net/qq_41117236/article/details/81152055 , which is when I learned to see prime number sieve blog, very detailed classification, the code I was using爱拉托斯尼特sieve ( after all, good written understanding = =) complexity is O (N * lnlnN)

Under the two algorithms are a bunch of numbers in batch testing is a good summary of not prime time, the first criterion is the wording of a single number is not a prime number, to be a template to use it.

 

ac Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<set> 
#include<queue>

using namespace std;

int num,aim;

struct node
{
    int num,step;
}node_num,node_aim;                //step是所需距离 

const int maxn=1e5;
bool number[maxn+5];
void isprime(int N)
{
    int i,j;
    memset(number,true, The sizeof (Number));
     for (I = 2 ; I <= sqrt (N * 1.0 ); I ++ ) 
    { 
        IF (Number [I] == to true ) 
        { 
            for (J = I * I; J <= N; J + = I) 
                { 
                number [J] = to false ; 
                 
                } 
            // secondary screening: i is a prime number, the next starting point is i * i, the back of the all i * i + 2 * n * i screened out 
        } 
    
    } 
    
} 

int BFS () 
{ 
    Queue <Node> Q; 
    q.push (node_num); 
    SET < int > S;
    s.insert (node_num.num); 

    the while (! q.empty ()) 
    { 
        Node Shu = q.front (); q.pop (); 

        for ( int I = 0 ; I < . 4 ; I ++)               / / a total of 4 times every 9 conversion should be carried out (because the changes are so 0) 
        {
             int P = ( int ) POW ( 10.0 , I);            
             int digt = (shu.num / P)% 10 ;
             int the TEMP = shu.num - digt * P;          // these three steps is to allow us to operate one who goes 0 
            for ( int J = 0 ; J <10 ; J ++ ) 
            { 
                IF (I == . 3 && J == 0 ) Continue ;    // end to end is not 0 
                int In Flag TEMP = P * + J;
                 IF (Number [In Flag] == to true && s.find ( in Flag) s.end == ())     // set.find () function if the function is not found return traverse set.end in the set, in order to achieve a weight check function 
                { 
                    Node N; 
                    N.num = in Flag ; 
                    N.step = shu.step + . 1 ;
                     IF (In Flag == AIM)   return N.step;
                    q.push(N);
                    s.insert(flag);
                }
            }
        }
    }
    return 0;

}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>num>>aim;
        isprime(9999);
        node_num.num = num;
        node_num.step = 0;
        cout<<bfs()<<endl;
    }
    return 0;
}

In addition to this problem because the smaller range, you can put all the prime numbers within a range hit the table, but that would still have to determine whether each prime number is only a difference of one point of view can not go (BFS qualification), this would mean that every step to go the more the road, the rookie also do not quite understand that it will reduce the complexity, welcome comments teach me or correct me on this code.

(In fact, is the title on the 27th of - -, day5)

Guess you like

Origin www.cnblogs.com/hanny007/p/11608854.html