poj3126 (prime Path) wide search + prime number determination

Source: Internet
Author: User

Description

The ministers of the Cabinet were quite upset by the message from the chief of Security stating, they would all has T o Change the Four-digit-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 had 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'll just has to paste four new digits over the four old ones on your office door.
-no, it ' s not so simple. Suppose that I change the first digit to an 8 and then the number would read 8033 which is not a prime!
-I see, being the Prime Minister you cannot stand have a non-prime number on your door even for a few seconds.
-correct! So I must invent a scheme for going from 1033 to 8179 by a path of the prime numbers where only one digit are 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 then the price of a digit is one pound.
-HMM, in this case I need a computer program to minimize the cost. You don ' t know some very cheap software gurus?
-in fact, I do. You see, there are this programming contest going on ... Help the Prime minister to find the cheapest prime path between any and given Four-digit primes! The first digit must be nonzero, of course. Here's 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 step 2 can is reused in the last Step–a new 1 must is purchased.

Input

One line with a positive number:the number of test cases (at most 100). Then for each test case, one line with the numbers separated by a blank. Both numbers is four-digit primes (without leading zeros).

Output

Either with a number stating the minimal cost or containing the word impossible.

Sample Input

31033 81791373 80171033 1033

Sample Output

670


Flood problem, 40 entrance of the BFS, after pruning far from the 40 entrance.


#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using    namespace Std;const int maxp = 10010;bool isprime[maxp];int A, b;void init () {Fill (IsPrime, ISPRIME+MAXP, true);                for (int i = 2; i < MAXP, ++i) if (Isprime[i]) for (int j = i*i; J < Maxp; J + = i) ISPRIME[J] = false;}    int BFS () {int VIS[MAXP];    memset (Vis,-1, sizeof (VIS));    Queue<int> Q;    Q.push (a);    Vis[a] = 0;        while (!q.empty ()) {int num = Q.front (); Q.poP ();        if (num = = b) return vis[num];                     for (int i = 0, i < 4; ++i) for (int j = 0; J < ++j) if (i! = 3 | | j) {                    int t = (int) (POW (ten, I) + 0.5);                    int c = num-((num/t)%) * t + J * t;                        if (Isprime[c] && vis[c] = =-1) {Q.push (c);                    VIS[C] = Vis[num] + 1; }}} return-1;}    int main () {init ();    int T;    Cin >> T;        while (t--) {scanf ("%d%d", &a, &b);        int ans = BFS ();        if (ans = =-1) printf ("impossible\n");    else printf ("%d\n", BFS ()); } return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

poj3126 (prime Path) wide search + prime number determination

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.