POJ 3126 Prime Path
来源:互联网 发布:图片转换矢量图软件 编辑:程序博客网 时间:2024/06/10 04:20
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.
Input Output Sample Input Sample Output
— 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
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).
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
31033 81791373 80171033 1033
670
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>using namespace std;int m,n;struct node{ int x,s;};queue<struct node>q;int vis[9999];int Judge(int n){ int i,j; if(n==0||n==1) return 0; else if(n==2||n==3) return 1; else { for(i=2;i<=(int)sqrt(n);i++) { if(n%i==0) return 0; } return 1; }}void BFS(){ int i; while(!q.empty()) { struct node t; t=q.front(); q.pop(); if(t.x==m) { printf("%d\n",t.s); return ; } for(i=1;i<=9;i+=2) { int x=t.x/10*10+i; if(!vis[x]&&Judge(x)&&x!=t.x) { vis[x]=1; struct node f; f.x=x; f.s=t.s+1; q.push(f); } } for(i=0;i<=9;i++) { int x=t.x/100*100+i*10+t.x%10; if(!vis[x]&&Judge(x)&&x!=t.x) { vis[x]=1; struct node f; f.x=x; f.s=t.s+1; q.push(f); } } for(i=0;i<=9;i++) { int x=t.x/1000*1000+i*100+t.x%100; if(x!=t.x&&Judge(x)&&!vis[x]) { vis[x]=1; struct node f; f.x=x; f.s=t.s+1; q.push(f); } } for(i=1;i<=9;i++) { int x=i*1000+t.x%1000; if(x!=t.x&&Judge(x)&&!vis[x]) { vis[x]=1; struct node f; f.x=x; f.s=t.s+1; q.push(f); } } } printf("Impossible\n"); return ;}int main(){ int t; scanf("%d",&t); while(t--) { memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); scanf("%d%d",&n,&m); if(n==m) printf("0\n"); else { vis[n]=1; struct node t; t.x=n; t.s=0; q.push(t); BFS(); } } return 0;}
0 0
- POJ 3126 Prime Path
- POJ 3126 Prime Path
- poj 3126Prime Path
- POJ -3126-Prime Path
- POJ 3126 Prime Path
- poj 3126 Prime Path
- POJ 3126 - Prime Path
- POJ 3126 Prime Path
- poj 3126 prime path
- POJ 3126 Prime Path
- Prime Path poj 3126
- POJ 3126 Prime Path
- poj 3126 Prime Path
- POJ 3126 Prime Path
- poj 3126 Prime path
- poj - 3126 - Prime Path
- POJ 3126 Prime Path
- POJ 3126 Prime Path
- 一张图明白box-sizing属性
- JVM系列三:JVM参数设置、分析
- Python的并列和或者条件
- Netty学习-handler 的基类选择!
- Struts优缺点
- POJ 3126 Prime Path
- virtualbox 中原来系统的磁盘文件vdi复制及移动
- 微信的智能心跳方案
- VR实训项目规划(3D场景篇)
- 交流心得1
- java string类型转换boolean类型
- 计算机网络之Cisco Packet Tracer模拟器使用
- FZU 2129 子序列个数(计数DP)
- linux查看某个端口(port)是否被占用