poj 3126 -- Prime Path

Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
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 would 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! So 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

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

题意:给出两个素数,问至少需要多少步将第一个转换成第二个。每次只能转换一位。并且第一位不能是零。

思路:广搜枚举每一位,每一位都9或10种情况。水题。

  1 /*======================================================================
  2  *           Author :   kevin
  3  *         Filename :   PrimePath.cpp
  4  *       Creat time :   2014-08-03 16:07
  5  *      Description :
  6 ========================================================================*/
  7 #include <iostream>
  8 #include <algorithm>
  9 #include <cstdio>
 10 #include <cstring>
 11 #include <queue>
 12 #include <cmath>
 13 #define clr(a,b) memset(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         /*------------------处理第1位------------------*/
 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         /*------------------处理第2位-------------------*/
 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         /*------------------处理第3位-------------------*/
 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         /*-----------------处理第4位-------------------*/
 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%d",&a,&b);
106         BFS(a);
107     }
108     return 0;
109 }
View Code

 



posted @ 2014-08-03 16:57  ZeroCode_1337  阅读(170)  评论(0编辑  收藏  举报