Poj 3126 -- prime path

Source: Internet
Author: User
Prime path
Time limit:1000 ms   Memory limit:65536 K
Total submissions:11288   Accepted:6398

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
Question: give two prime numbers and ask how many steps are required to convert the first one to the second one. Only one digit can be converted at a time. And the first digit cannot be zero.

Idea: A wide search can enumerate 9 or 10 cases for each digit. Question.

1/* ============================================== ====================================== 2 * Author: kevin 3 * filename: primepath. CPP 4 * creat time: * description: 6 ================================================ ===================================== */7 # include <iostream> 8 # include <algorithm> 9 # include <cstdio> 10 # include <cstring> 11 # include <queue> 12 # include <cmath> 13 # define CLR (, b) me Mset (a, B, sizeof (A) 14 # define M 15000 15 using namespace STD; 16 int isprime [M + 5], vis [m], CNT [m]; 17 int A, B; 18 void makeprime () 19 {20 CLR (isprime, 0); 21 isprime [0] = isprime [1] = 1; 22 For (INT I = 2; I <m; I ++) {23 if (! Isprime [I]) {24 for (Int J = I + I; j <m; j + = I) {25 isprime [J] = 1; 26} 27} 28} 29} 30 void BFS (INT s) 31 {32 CLR (VIS, 0); 33 CLR (CNT, 0 ); 34 vis [s] = 1; 35 queue <int> que; 36 que. push (s); 37 while (! Que. empty () {38 int T = que. front (); 39 que. pop (); 40 if (t = B) {41 printf ("% d \ n", CNT [T]); 42 break; 43} 44/* ------------------ handle 1st bits ---------------- */45 int No1 = T/1000; 46 int temp = T-No1 * 1000; 47 for (Int J = 1; j <= 9; j ++) {48 if (j = No1) continue; 49 int change_num = J * 1000 + temp; 50 if (! Isprime [change_num] &! Vis [change_num]) {51 que. push (change_num); 52 vis [change_num] = 1; 53 CNT [change_num] = CNT [T] + 1; 54} 55} 56/* --------------------- end ---------------------- */57/* -------------------- process 2nd-bit ----------------- */58 int NO2 = T/100% 10; 59 int S2 = T % 100; 60 for (Int J = 0; j <= 9; j ++) {61 If (j = NO2) continue; 62 int change_num = No1 * 1000 + J * 100 + S2; 63 If (! Isprime [change_num] &! Vis [change_num]) {64 que. push (change_num); 65 vis [change_num] = 1; 66 CNT [change_num] = CNT [T] + 1; 67} 68} 69/* --------------------- end ---------------------- */70/* -------------------- process 3rd bits ------------------- */71 int NO3 = T/10% 10; 72 S2 = T/100; 73 int S1 = T % 10; 74 for (Int J = 0; j <= 9; j ++) {75 if (j = NO3) continue; 76 int change_num = S2 * 100 + J * 10 + S1; 77 If (! Isprime [change_num] &! Vis [change_num]) {78 que. push (change_num); 79 vis [change_num] = 1; 80 CNT [change_num] = CNT [T] + 1; 81} 82} 83/* --------------------- end --------------------- */84/* ------------------- handle 4th-bit ------------------- */85 int No4 = T % 10; 86 int S3 = t-no4; 87 for (Int J = 0; j <= 9; j ++) {88 If (j = No4) continue; 89 int change_num = S3 + J; 90 if (! Isprime [change_num] &! Vis [change_num]) {91 que. push (change_num); 92 vis [change_num] = 1; 93 CNT [change_num] = CNT [T] + 1; 94} 95} 96/* -------------------- end ---------------------- */97} 98} 99 int main (INT argc, char * argv []) 100 {101 makeprime (); 102 int T; 103 scanf ("% d", & T); 104 while (t --) {105 scanf ("% d", & A, & B ); 106 BFS (a); 107} 108 return 0; 109}
View code

 



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.