Poj3126 prime path search BFS

Source: Internet
Author: User

Question:
Each time, only one digit can be changed to a new prime number,
How many times can be used to change to the second given prime number?
Analysis:
Since it is classified into BFs, we can use BFs to do it. We can judge a table with a prime number,
Speed up the determination, and then enumerate the number of each bit, the same as the previous BFs,
Calculate 0 to 9 each time. (Note: the value of a thousand bits cannot be 0. If the value of a single bits is an even number
You don't have to worry about it)

# Include <iostream>
# Include <cstring>
# Include <queue>
Using namespace STD;
Int m, n, ans;
Int A [6];
Bool isprime [10000], use [10000];
Void make_prime ()
{// Generate an array of prime numbers within 10000. If it is true, it is not a prime number.
Memset (isprime, false, sizeof (isprime ));
For (INT I = 3; I <= 9999; I + = 2)
If (! Isprime [I])
For (Int J = I + I; j <10000; j + = I)
Isprime [J] = true;
}
Void BFS ()
{
Int X;
Queue <int> P; // record the current number of times
Queue <int> q; // record the current four-digit number
P. Push (0 );
Q. Push (m );
While (! Q. Empty ())
{
Int T = Q. Front ();
Ans = P. Front ();
Q. Pop ();
P. Pop ();
If (t = N) // ends when the request is successful
Return;
For (INT I = 1; I <= 4; I ++) // records each of the four digits.
{
A [I] = T;
T/= 10;
}
For (Int J = 1; j <= 9; j ++)
{// The number of thousands is changing, and the number of thousands cannot be 0.
X = J * 1000 + A [3] * 100 + A [2] * 10 + A [1]; // current number
If (! Isprime [x] &! Use [x])
{// It is a prime number and is out of date.
Q. Push (X );
P. Push (ANS + 1 );
Use [x] = true; // indicates that you have tried it.
}
}
For (Int J = 0; j <= 9; j ++)
{// Hundreds of digits are changing
X = A [4] * 1000 + J * 100 + A [2] * 10 + A [1];
If (! Isprime [x] &! Use [x])
{
Q. Push (X );
P. Push (ANS + 1 );
Use [x] = true;
}
}
For (Int J = 0; j <= 9; j ++)
{// 10 is changing
X = A [4] * 1000 + A [3] * 100 + J * 10 + A [1];
If (! Isprime [x] &! Use [x])
{
Q. Push (X );
P. Push (ANS + 1 );
Use [x] = true;
}
}
For (Int J = 1; j <= 9; j + = 2)
{// The change of a single digit starts from 1 and increases by 2 at a time
X = A [4] * 1000 + A [3] * 100 + A [2] * 10 + J;
If (! Isprime [x] &! Use [x])
{
Q. Push (X );
P. Push (ANS + 1 );
Use [x] = true;
}
}
}
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Make_prime ();
Int T;
Cin> T;
While (t --)
{
Memset (use, false, sizeof (use ));
Ans = 0;
Cin> m> N;
BFS ();
Cout <ans <Endl;
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.