2016暑期训练4C-PrimePath(POJ3126)
来源:互联网 发布:视频编辑最好软件 编辑:程序博客网 时间:2024/06/05 19:50
C - Prime Path
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %lluDescription
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
这道题直接用bfs暴力搜索,最好打个素数表以防超时。在给这个四位素数的每一位换元素的时候出了点问题,反正就是思路不对,WA了多次。本来觉得很简单的,没想到是个坑。还是那句话,对于菜鸟来说,其实是没有水题和难题之分的,因为他们能把所有的水题都做成难题啊
水水的代码如下:
#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <cstdio>using namespace std;struct node{ int n; int cot;};node start,endp;node q[100000];int prime[10010];int vis[10010];void asprime(){ prime[0]=prime[1]=0; for(int i=2;i<10000;i++) { if(prime[i]==-1) { prime[i]=1; for(int j=2*i;j<10000;j+=i) prime[j]=0; } }}void bfs(){ int front=0,rear=0; int flag=0; q[rear++]=start; vis[start.n]=1; node t,tmp; while(front<rear) { t=q[front++]; //cout<<t.n<<endl; if(t.n==endp.n){flag=1;break;} for(int i=0;i<4;i++) { for(int j=0;j<10;j++) { if(i==0) tmp.n=t.n/10*10+j; else if(i==1) tmp.n=t.n/100*100+j*10+t.n%10; else if(i==2) tmp.n=t.n/1000*1000+j*100+t.n%100; else if(j==0) continue; else {tmp.n=j*1000+t.n%1000;} if(!vis[tmp.n]&&prime[tmp.n]) { tmp.cot=t.cot+1; q[rear++]=tmp; vis[tmp.n]=1; } } } } if(flag) cout<<t.cot<<endl; else cout<<"Impossible"<<endl;}int main(){ //freopen("in.txt","r",stdin); int T; cin>>T; while(T--) { memset(prime,-1,sizeof(prime)); memset(vis,0,sizeof(vis)); asprime(); cin>>start.n>>endp.n; start.cot=0; bfs(); }}
0 0
- 2016暑期训练4C-PrimePath(POJ3126)
- 暑期训练赛(6)C
- 2016 北邮暑期训练3-C题(CodeForces 699BOne Bomb)白痴题
- CSU-ACM2017暑期训练6-bfs C
- 暑期训练
- {小结}2016暑期Aug~Sep训练小结
- CSU-ACM2017暑期训练4-dfs D
- CSU-ACM2017暑期训练4-dfs E
- CSU-ACM2017暑期训练4-dfs F
- CSU-ACM2017暑期训练4-dfs G
- 暑期训练赛(6)解题报告
- 暑期训练赛(6)A题
- 暑期训练赛(6)B
- 暑期训练赛(6)D
- 暑期训练赛(6)E
- 暑期集训day1训练(图论1)
- 暑期集训day3训练(数据结构)
- 2013 - ECJTU 暑期训练赛第四场-problem-C
- iOS 获取系统相册
- python 数据类型转换
- 电力设备巡检管理系统
- 行走、想象、发现、情感和美
- unity 特殊文件夹
- 2016暑期训练4C-PrimePath(POJ3126)
- 魔影病毒学习总结
- [网络]无线AP启用WIFI模式设置步骤
- "Sum of Two Integers" and "Sort Colors"
- Unity 客户端框架(一):有限状态机
- rw_我的阿勒泰1
- Spring学习之旅(1)------单元测试
- Mac 访问Google 配置
- 话说NOIP一些会考到的算法