Prime path (poj 3126 BFS)

Source: Internet
Author: User
Language:DefaultPrime path
Time limit:1000 ms   Memory limit:65536 K
Total submissions:11703   Accepted:6640

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

Source

Northwestern Europe 2006

Question:Given two prime numbers (four digits), the second prime number can be obtained after several conversions of the first number. Conversion Method: it refers to the number of a certain digit in the transformed number (the first digit cannot be zero, and the other transformed numbers are 0 ~~ 9), the transformed number is also a prime number.

Train of Thought: BFs, search for the shortest path, it is easy to think of the breadth-first search; because the breadth-first search, the first search for the number of steps is the minimum number of steps. In addition, the prime number table is used to improve the efficiency of judgment.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # pragma comment (linker, "/Stack: 102400000,102400000") # define maxn 10005 # define maxn 2005 # define mod 1000000009 # define INF 0x3f3f3f # define PI ACOs (-1.0) # define EPS 1e-6typedef long ll; using namespace STD; bool is Prime [maxn]; bool visit [maxn]; int m, n, a, B, c, d; struct node {int P [4]; // use an array to store the int step;}; void prime () // prime screening method {for (INT I = 2; I <maxn; I ++) {if (I % 2) isprime [I] = true; else isprime [I] = false;} int M = SQRT (10010.0); For (INT I = 3; I <m; I ++) {If (isprime [I]) {for (Int J = I + I; j <maxn; j + = I) isprime [J] = false ;}}int BFS () {node St, now; memset (visit, false, sizeof (visit); queue <node> q; while (! Q. empty () Q. pop (); visit [m] = true; ST. P [0] = Mb/1000; ST. P [1] = (M/100) % 10; ST. P [2] = (M/10) % 10; ST. P [3] = m % 10; // printf ("% d \ n", St. A [0], St. A [1], St. A [2], St. A [3]); ST. step = 0; q. push (ST); While (! Q. empty () {ST = Q. front (); q. pop (); If (St. P [0] = A & St. P [1] = B & St. P [2] = C & St. P [3] = d) {return St. step;} For (INT I = 0; I <= 3; I ++) {for (Int J = 0; j <10; j ++) {If (St. P [I] = J) continue; if (I = 0 & J = 0) continue; now. P [0] = ST. P [0]; now. P [1] = ST. P [1]; now. P [2] = ST. P [2]; now. P [3] = ST. P [3]; now. P [I] = J; int x = now. P [0] * 1000 + now. P [1] * 100 + now. P [2] * 10 + now. P [3]; If (isprime [x] &! Visit [x]) {visit [x] = true; now. step = ST. step + 1; q. push (now) ;}}}return-1 ;}int main () {Prime (); int CAS; scanf ("% d", & CAS ); while (CAS --) {scanf ("% d", & M, & N); A = N/1000; B = (N/100) % 10; C = (N/10) % 10; D = n % 10; // printf ("% d \ n", a, B, c, d); int ans = BFS (); If (ANS =-1) printf ("impossible \ n"); else printf ("% d \ n ", ans);} return 0;}/* 31033 81791373 80171033 1033 */



Prime path (poj 3126 BFS)

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.