Poj 3126-prime path simple search BFS

Source: Internet
Author: User

Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3126

 

Solution report:

 

Or BFs, search for breadth first

For a number ABCD, the number adjacent to it is obtained by changing the number of one of the digits.

If one of the digits is changed, if the number is a prime number and has not been accessed before, it is placed in the queue for the next search.

In this way, search until we find the number that we finally need to change and exit.

The output result should be the shortest path from S to goal.

 

# Include <iostream> <br/> # include <queue> <br/> using namespace STD; </P> <p> int isprime [10000]; <br/> int color [10000]; <br/> int d [10000]; </P> <p> void findprime () // create a prime number table <br/>{< br/> for (INT I = 2; I <10000; I ++) <br/> isprime [I] = 1; <br/> for (INT I = 2; I <5000; I ++) <br/> {<br/> for (Int J = 2; j <= I; j ++) <br/> {<br/> int temp = I * j; <br/> If (temp> = 10000) <br/> break; <br/> else <br/> isprime [temp] = 0; <br/>}</ P> <p> int BFS (int s, int goal) <br/> {<br/> for (INT I = 0; I <10000; I ++) <br/>{< br/> color [I] = 0; <br/> d [I] = 0; <br/>}< br/> If (S = goal) <br/> return 0; <br/> color [s] = 1; <br/> d [s] = 0; <br/> queue <int> q; <br/> q. push (s); <br/> while (! Q. empty () <br/>{< br/> int u = Q. front (); <br/> q. pop (); <br/> int N [4]; <br/> N [0] = u/1000; // Number of thousands <br/> N [1] = (u-n [0] * 1000)/100; // hundreds of digits <br/> N [2] = (u-n [0] * 1000-n [1] * 100)/10; // ten digits <br/> N [3] = u-1000 * n [0]-N [1] * 100-n [2] * 10; // single digit <br/> int K = 1; <br/> for (Int J = 3; j> = 0; j --) <br/>{< br/> If (J! = 3) <br/> K = 10 * k; <br/> for (INT I =-N [J]; I <10-n [J]; I ++) <br/> {<br/> int temp = u + I * K; <br/> If (isprime [temp] & Color [temp] = 0 & temp> = 1000) <br/>{< br/> color [temp] = 1; <br/> d [temp] = d [u] + 1; <br/> If (temp = goal) <br/> return d [temp]; <br/> q. push (temp); <br/>}< br/> color [u] = 2; <br/>}< br/> return 0; <br/>}</P> <p> int main () <br/>{< br/> findprime (); <br/> int casenum; <br/> CIN> casenum; <br/> while (casenum --) <br/>{< br/> int S, goal; <br/> CIN> S> goal; <br/> cout <BFS (S, goal) <Endl; <br/>}< br/>}

 

 

Appendix:

 

Prime path
Time limit:1000 ms   Memory limit:65536 K
Total submissions:4497   Accepted:2631

Description

The Ministers of the Cabinet were quite upset by the message from the Chief of Security stating that they wowould 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! Also 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

31033 81791373 80171033 1033

Sample output

670

 

 

 

 

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.