POJ 3126 Prime Path (Guang search)

Source: Internet
Author: User

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12974 Accepted: 7342

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

the main topic: to two primes A, B (is 4 digits), asked whether a can be transformed, to change the principle: only one digit of a at a time, and the converted number must be a prime number, such as 1033 can become 1733, but not become 1233 (because 1233 is not a prime), Also can not become 3733 (because from 1033 to 37,331 times the transformation of 2 digits), if possible, the output of the minimum number of transformations, otherwise output impossible.
wide Search, judging whether it is a prime number can be first hit the table.
#include <stdio.h> #include <queue> #include <string.h>using namespace std; #define HUR 100#define THO 1000#define TEN 10const int maxn=10000;bool prime[maxn+5];int vis[maxn],s,e;void prime_table () {int I,j;memset (prime,0, sizeof (prime)); for (i=2;i<maxn;i++) if (!prime[i]) for (j=i*i;j<maxn;j+=i) prime[j]=1;} int BFS () {int I;memset (vis,0,sizeof (VIS));vis[s]=1;queue<int> Que;que.push (s); while (!que.empty ()) {int t= Que.front (); Que.pop (); int d=t;d%=1000;for (i=1;i<10;i++) {int tt=d+i*tho;//transform thousand if (prime[tt]==0 && Vis[tt] ==0) {if (tt==e) return Vis[t];que.push (TT); vis[tt]=vis[t]+1;}} d=t%100+ (t/1000*1000); for (i=0;i<10;i++) {int tt=d+i*hur;//transform hundred if (prime[tt]==0 && vis[tt]==0) {if (tt==e) Return Vis[t];que.push (TT); vis[tt]=vis[t]+1;}} D=t%10+t/100*100;for (i=0;i<10;i++) {int tt=d+i*ten;//transform 10-bit if (prime[tt]==0 && vis[tt]==0) {if (tt==e) return Vis[t];que.push (TT); vis[tt]=vis[t]+1;}} D=t/10*10;for (i=0;i<10;i++) {int tt=d+i;//transform digit if (prime[tt]==0 && vis[tt]==0) {if (tt==e) return Vis[t];que.push (TT); vis[tt]=vis[t]+1;}}} return 0;} int main () {int t,res;prime_table (), scanf ("%d", &t), while (t--) {scanf ("%d%d", &s,&e), if (s==e) {printf ("0\ n "); continue;} Res=bfs (); if (res==0) printf ("impossible\n"); elseprintf ("%d\n", res);} return 0;}




POJ 3126 Prime Path (Guang search)

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.