poj 3126 Prime Path

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

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <queue>
  5 using namespace std;
  6 int a,b;
  7 int flag[10000];
  8 struct node
  9 {
 10     int num,step;
 11 };
 12 bool prim[10000];
 13 void bfs()
 14 {
 15     node p,temp;
 16     int i;
 17     p.num=a;
 18     p.step=0;
 19     flag[a]=0;
 20     queue<node>q;
 21     while(!q.empty())
 22     {
 23         q.pop();
 24     }
 25     q.push(p);
 26     while(!q.empty())
 27     {
 28         p=q.front();
 29         q.pop();
 30         if(p.num==b)
 31         {
 32             printf("%d\n",p.step);
 33             return;
 34         }
 35         for(i=0;i<4;i++)
 36         {
 37             temp=p;
 38             temp.step=p.step+1;
 39             if(i==0)
 40             {
 41                 int ff=temp.num/1000;
 42                 int j;
 43                 for(j=1;j<=9;j++)
 44                 {
 45                     if(j!=ff)
 46                     {
 47                         int dd=temp.num%1000+j*1000;
 48                         if(!prim[dd] && temp.step<flag[dd])
 49                         {
 50                             temp.num=dd;
 51                             flag[dd]=temp.step;
 52                             q.push(temp);
 53                         }
 54                     }
 55                 }
 56 
 57             }
 58             if(i==1)
 59             {
 60                 int ff=(temp.num/100)%10;
 61                 int j;
 62                 for(j=0;j<=9;j++)
 63                 {
 64                     if(j!=ff)
 65                     {
 66                         int dd=temp.num%100+j*100+temp.num/1000*1000;
 67                         if(!prim[dd] && temp.step<flag[dd])
 68                         {
 69                             temp.num=dd;
 70                             flag[dd]=temp.step;
 71                             q.push(temp);
 72                         }
 73                     }
 74                 }
 75 
 76             }
 77             if(i==2)
 78             {
 79                 int ff=(temp.num/10)%10;
 80                 int j;
 81                 for(j=0;j<=9;j++)
 82                 {
 83                     if(j!=ff)
 84                     {
 85                         int dd=temp.num/100*100+j*10+temp.num%10;
 86                         if(!prim[dd] && temp.step<flag[dd])
 87                         {
 88                             temp.num=dd;
 89                             flag[dd]=temp.step;
 90                             q.push(temp);
 91                         }
 92                     }
 93                 }
 94 
 95             }
 96             if(i==3)
 97             {
 98                 int ff=temp.num%10;
 99                 int j;
100                 for(j=0;j<=9;j++)
101                 {
102                     if(j!=ff)
103                     {
104                         int dd=temp.num/10*10+j;
105                         if(!prim[dd] && temp.step<flag[dd])
106                         {
107                             temp.num=dd;
108                             flag[dd]=temp.step;
109                             q.push(temp);
110                         }
111                     }
112                 }
113             }
114         }
115     }
116 }
117 int main()
118 {
119     int t;
120     memset(prim,false,sizeof(prim));
121     prim[1]=true;
122     for(int i=2;i<=5000;i++)
123     {
124         for(int j=i+i;j<=10000;j+=i)
125         {
126             prim[j]=true;
127         }
128     }
129     scanf("%d",&t);
130     while(t--)
131     {
132         scanf("%d%d",&a,&b);
133         if(a==b)
134         {
135             printf("0\n");
136             continue;
137         }
138         memset(flag,0x7f,sizeof(flag));
139         bfs();
140     }
141     return 0;
142 }
View Code

此题大意:

给你一个素数a ,每次只能改变一位数字,并且改成下个数也必须是素数,题目要求从a变到b最少的步数。

然后就是简单的bfs,用flag数组装每一个素数的最少步数。

posted @ 2013-05-31 22:11  欧阳生朵  阅读(392)  评论(0编辑  收藏  举报