首页 > 代码库 > POJ 3126 math(BFS)
POJ 3126 math(BFS)
Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21581 | Accepted: 11986 |
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.
— 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.
1033The 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.
1733
3733
3739
3779
8779
8179
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
31033 81791373 80171033 1033
Sample Output
670
Source
Northwestern Europe 2006
思路:对每位数字替换,直到成为目标数字。
代码:
1 #include "cstdio" 2 #include "iostream" 3 #include "algorithm" 4 #include "string" 5 #include "cstring" 6 #include "queue" 7 #include "cmath" 8 #include "vector" 9 #include "map"10 #include "stdlib.h"11 #include "set"12 #define mj13 #define db double14 #define ll long long15 using namespace std;16 const int N=1e4+5;17 const int mod=1e9+7;18 const ll inf=1e16+10;19 bool pri[N];20 bool u[N],v[N];21 int c[N];//统计步数22 void init(){ 23 int i,j;24 for(i=1000;i<=N;i++){25 for(j=2;j<i;j++)26 if(i%j==0){27 pri[i]=0;28 break;29 }30 if(j==i) pri[i]=1;31 }32 }33 int bfs(int s,int e){34 queue<int >q;35 memset(v,0, sizeof(v));36 memset(c,0, sizeof(c));37 int tmp,a[4],ans=s;38 q.push(s);39 v[s]=1;40 while(q.size()){41 int k=q.front();42 q.pop();43 a[0]=k/1000,a[1]=k/100%10,a[2]=k/10%10,a[3]=k%10;44 for(int i=0;i<4;i++){45 tmp=a[i];46 for(int j=0;j<10;j++){47 if(tmp!=j){48 a[i]=j;49 ans=a[0]*1000+a[1]*100+a[2]*10+a[3];50 if(pri[ans]&&!v[ans]){//为素数且未使用过51 c[ans]=c[k]+1;52 v[ans]=1;53 q.push(ans);54 }55 if(ans==e) {56 printf("%d\n",c[ans]);57 return 0;58 }59 }60 a[i]=tmp;61 }62 }63 if(k==e) {64 printf("%d\n",c[k]);65 return 0;66 }67 }68 printf("Impossible\n");69 return 0;70 }71 int main()72 {73 int n;74 int x,y;75 scanf("%d",&n);76 init();77 for(int i=0;i<n;i++){78 scanf("%d%d",&x,&y);79 bfs(x,y);80 }81 return 0;82 }
POJ 3126 math(BFS)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。