POj 3126 Prime Path
来源:互联网 发布:linux权限数字表示 编辑:程序博客网 时间:2024/05/21 04:41
来源:http://poj.org/problem?id=3126
Prime Path
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11384 Accepted: 6453
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
题目大意: 从素数四位数a变换到四位数b,每次只能改变一位数字,且每次改变后的数也必须为素数~~ 四位数中第一位不能为0,求最少变换次数,如果无法变换,则输出"Impossible".
题解:明显的广搜~~ 每次改变一位数,用hash判重解决之。
PS:这题出现了一个很蛋疼的错误,昨天样例都可以过, 但是提交WA。今天重写一遍-----发现把++exdir 写成exidir++囧..........
AC代码:
#include<iostream>#include<cmath>#include<cstring>using namespace std;const int Max=2500000,Maxh=10005;bool visit[Maxh]={0},Isprime[Maxh]={0};int star,dest;bool prime(int k){ for(int i=2;i<=(int)sqrt((float)k);i++) if(k%i==0) return false; return true;}struct Node{ int dig[4],num,step;}map[Max]={0},temp;void calc(Node &x){x.dig[0]=x.num/1000;x.dig[1]=(x.num/100)%10;x.dig[2]=(x.num/10)%10;x.dig[3]=x.num%10;}void calcsum(Node &x){ x.num=0; for(int i=0;i<4;i++) x.num+=x.dig[i]*(int)pow((float)10,3-i);}int main(){ int t; cin>>t; for(int i=1000;i<10000;i++) if(prime(i)) Isprime[i]=1; while(t--){ memset(visit,0,sizeof(visit)); int flag=1; cin>>star>>dest; if(star==dest){ cout<<0<<endl; continue; } map[0].num=star; calc(map[0]); map[0].step=0; int nodedir=0,exdir=0; while(nodedir<=exdir&&exdir<Max&&flag){ for(int i=0;i<4&&flag;i++){ for(int k=0;k<10;k++){ if(i||k){ if(i==3&&k%2==0) continue; temp=map[nodedir]; temp.dig[i]=k; calcsum(temp); temp.step++; if(temp.num==dest){ cout<<temp.step<<endl; flag=0; break; } if(Isprime[temp.num]&&!visit[temp.num]){ visit[temp.num]=1; map[++exdir]=temp; } } } } nodedir++; } if(flag) cout<<"Impossible"<<endl; }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
- VS 附加到进程调试技巧
- 使用achartengine 实现简单静态、动态折线图
- 数据库外键
- c++ stack,queue,vector用法
- 源文件与命令行执行九九乘法表
- POj 3126 Prime Path
- c++ 中的cast
- listview与adapter用法
- 【leetcode系列】3Sum
- JS关闭当前页面的办法
- UVA 540 Team Queue(入列和出列)
- SVN Checkout代码库 不包括源文件夹根目录
- 在美国当个程序员,没你想的那么难
- 《操作系统概念第六版》阅读笔记一 I/O子系统结构