POJ3126 素数之路
题目内容
Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6432 Accepted: 3673
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
Source
Northwestern Europe 2006
题目很长,大概意思是给你a、b两个四位素数,每一步可以将a的一个数字变成其他数字(0-9),但要求每次变换得到的结果都是素数且a始终为四位数。编程求解最少的变换次数。
依旧DFS,看准a始终为四位数就没什么难度。
我的基本方法:每次都把a拆成四个一位数,对这四个数逐个加\减1-9同时保证其<10或>=0。
View Code
1 #include<cstdio>
2 #include<cstring>
3 #include<queue>
4 #define MAXN 10001
5 using namespace std;
6 bool prime[MAXN],used[MAXN];
7 long step[MAXN];
8 long N,beg,tot;
9 void Primemaker()
10 {
11 long i,j;
12 memset(prime,true,sizeof(prime));
13 for (i=2;i<=100;i++)
14 {
15 if (prime[i])
16 {
17 for (j=i*i;j<MAXN;j+=i)
18 prime[j]=false;
19 }
20 }
21 prime[0]=prime[1]=false;
22 }
23 void change(long n[],long x)
24 {
25 n[0]=x/1000;
26 x%=1000;
27 n[1]=x/100;
28 x%=100;
29 n[2]=x/10;
30 x%=10;
31 n[3]=x;
32 }
33 long back(long n[])
34 {
35 return n[0]*1000+n[1]*100+n[2]*10+n[3];
36 }
37 long bfs()
38 {
39 queue<long>q;
40 if (beg==tot)
41 return 0;
42 step[beg]=0;
43 long oper,t;
44 q.push(beg);
45 used[beg]=true;
46 while(!q.empty())
47 {
48 oper=q.front();
49 q.pop();
50 if(oper==tot)
51 return step[oper];
52 long de[4];
53 change(de,oper); //分散存储oper
54 for (long i=1;i!=10;i++) //加
55 {
56 for(long j=0;j!=4;j++)
57 {
58 if(de[j]+i<10)
59 {
60 de[j]+=i;
61 t=back(de);
62 if(!used[t]&&prime[t]&&t>1000)
63 {
64 used[t]=true;
65 step[t]=step[oper]+1;
66 if(t==tot)
67 return step[t];
68 q.push(t);
69 }
70 de[j]-=i;
71 }
72 }
73 }
74 for(long i=-1;i!=-10;i--) //减
75 {
76 for(long j=0;j!=4;j++)
77 {
78 if(de[j]+i>=0)
79 {
80 de[j]+=i;
81 t=back(de);
82 if(!used[t]&&prime[t]&&t>1000)
83 {
84 used[t]=true;
85 step[t]=step[oper]+1;
86 if(t==tot)
87 return step[t];
88 q.push(t);
89 }
90 de[j]-=i;
91 }
92 }
93 }
94 }
95 return -1;
96 }
97 void init()
98 {
99 memset(used,false,sizeof(used));
100 }
101 int main(void)
102 {
103 Primemaker();
104 scanf("%ld",&N);
105 for(long i=0;i!=N;++i)
106 {
107 scanf("%ld%ld",&beg,&tot);
108 init();
109 long ans=bfs();
110 if (ans<0)
111 printf("Impossible\n");
112 else
113 printf("%ld\n",ans);
114 }
115 return 0;
116 }
应用了STL的queue,G++编译,240K 16MS
posted on 2011-10-30 18:50 SilVeRyELF 阅读(260) 评论(0) 收藏 举报

浙公网安备 33010602011771号