【POJ - 3126】 Prime Path (BFS)

Prime Path

Here is the English text directly on the Chinese

Descriptions:

Give you two four-digit prime numbers a, b.
a can change the numbers on a bit becomes c, but only when the primes are also four c's can be such a change.
How many times have you calculated a minimum after the conversion to become a b.
For example: 1033--> 8179 
1033 
1733 
3733 
3739 
3779 
8779 
8179
happened converted six times.
Input

The first line of input integer T, denotes the number of samples. (T <= 100) 
for each sample four input two primes a, b. (Without leading zeros) 
the Output

For each sample, a minimum number of transitions of the output, if the output can not be converted into b "Impossible".

Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

Topic links :

https://vjudge.net/problem/POJ-3126

Two points, each step is a prime number, the smallest number of steps, for the first a prime table lists, for each and every search changes are what you can, write a personal search, accustomed priority queue

AC Code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0 (X) Memset (X, 0, the sizeof (X))
 the using  namespace STD;
 int T, n-, m;
 int isPrime [ 10005 ]; // prime table 
int VIS [ 10005 ]; // tag 
struct Node 
{ 
    int NUM, Money;
     BOOL  operator <( const Node C &) const // small number of steps to dequeue 
    {
         return Money> c.money; 
    } 
} now, Next; 
void eratos ( int X) // find prime number table 
{
    for(int i=0; i<=x; ++i)
        isprime[i]=true;
    isprime[0]=isprime[1]=false;
    for(int i=2; i<=x; ++i)
    {
        if(isprime[i])
        {
            int j=i+i;
            while(j<=x)
            {
                isprime[j]=false;
                j+=i;
            }
        }
    }
}
void bfs()
{
    priority_queue<node>q;
    now.num=n,now.money=0;
    q.push(now);
    vis[n]=1;
    int f=0;
//    cout<<now.num<<endl;
//    cout<<2<<endl;
    while(!q.empty())
    {
        char x[5];
//        cout<<1<<endl;
        now=q.top();
        q.pop();
        if(now.num==m)
        {
            f=1;
            cout<<now.money<<endl;
            return;
        }
        for(int i=0; i<4; ++i)
        {
            sprintf(x,"%d",now.num);
            for(int j=0; j<10; ++j)
            {
                if(i==0&&j==0)//千位不允许为0
                    continue;
                if(i==0)// four cases, respectively, for converting "a ten Baiqian" bits 
                    next.num = J * 1000 + (X [ . 1 ] - ' 0 ' ) * 100 + (X [ 2 ] - ' 0 ' ) * 10 + (X [ . 3 ] - ' 0 ' );
                 the else  IF (I == . 1 ) 
                    next.num = J * 100 + (X [ 0 ] - ' 0 ' ) * 1000 + (X [ 2 ] - ' 0 ' ) * 10+(x[3]-'0');
                else if(i==2)
                    next.num=j*10+(x[0]-'0')*1000+(x[1]-'0')*100+(x[3]-'0');
                else if(i==3)
                    next.num=j+(x[1]-'0') * 
    {100 + (X [ 2 ] - ' 0 ' ) * 10 + (X [ 0 ] - ' 0 ' ) * 1000 ;
                 IF (! IsPrime [next.num] && VIS [next.num]) // this number is prime and not been tagged 
                { 
                    VIS [next.num] = . 1 ; 
                    next.money = now.money + . 1 ; 
                    q.push (Next); 
                } 
            } 
        } 
    } 
    IF (F == 0 ) 
        COUT <<"Impossible"<<endl;
        return;
    }
}
int main()
{
    eratos(10005);//10005以内的素数表
    cin>>T;
    while(T--)
    {
        ME0(vis);
        cin>>n>>m;
        bfs();
    }
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11109179.html