POJ3126
来源:互联网 发布:手机cad看图软件 编辑:程序博客网 时间:2024/05/16 11:11
A - Prime Path
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
题目大意:给出两个质数a,b。从a变化到b每次只能对一位数字进行操作,且操作后的数字依然是质数,变化到b的最小操作次数。
//poj 3126#include "iostream"#include "cstdio"#include "queue"using namespace std;int ncase;int prime_1,prime_2;struct ehh{ int nownumber,step;}m;bool a[10000];queue<ehh> q;bool is_prime(int n) //判断是否为质数{ int result = 0; int i; if (n <= 1) { return result; } if(2 == n) { result = 1; return result; } for (i = 2; i < n ;i++) { if (0 == n % i) { result = 0; break; } else if (i + 1 == n) { result = 1; } else { continue; } } return result;}int ge,shi,bai,qian,store;bool flag=false;void bfs(){ while(!q.empty()) { m=q.front(); if(m.nownumber==prime_2) { cout<<m.step<<endl; flag=true; break; } q.pop(); ehh temp; temp=m; int n; for(int j=1;j<=4;j++) //只改变一位数字时的所有可能 { for(int i=0;i<=9;i++) { if(j==1) { n=m.nownumber/10; ge=m.nownumber%10; n=n*10; n=n+i; if(a[n]==false) { a[n]=true; if(is_prime(n)) { temp.nownumber=n; temp.step=m.step+1; q.push(temp); } } } if(j==2) { n=m.nownumber/100; ge=m.nownumber%10; n=n*100+ge; n=n+i*10; if(a[n]==false) { a[n]=true; if(is_prime(n)) { temp.nownumber=n; temp.step=m.step+1; q.push(temp); } } } if(j==3) { n=m.nownumber/1000; ge=m.nownumber%10; shi=m.nownumber%100/10; n=n*1000+ge+shi*10; n=n+i*100; if(a[n]==false) { a[n]=true; if(is_prime(n)) { temp.nownumber=n; temp.step=m.step+1; q.push(temp); } } } if(j==4) { ge=m.nownumber%10; shi=m.nownumber%100/10; bai=m.nownumber%1000/100; if(i!=0) //首位不为0 { n=i*1000+ge+shi*10+bai*100; if(a[n]==false) { a[n]=true; if(is_prime(n)) { temp.nownumber=n; temp.step=m.step+1; q.push(temp); } } } } } } }}int main(){ while(scanf("%d",&ncase)!=EOF) { for(int i=1;i<=ncase;i++) { while(!q.empty()) q.pop(); flag=false; cin>>prime_1>>prime_2; memset(a,false,sizeof(a)); m.nownumber=prime_1; m.step=0; a[prime_1]==true; q.push(m); bfs(); if(flag==false) cout<<"Impossible"<<endl; } }}
- poj3126
- POJ3126
- poj3126
- poj3126
- poj3126
- POJ3126
- poj3126
- poj3126
- poj3126
- poj3126
- poj3126
- poj3126
- poj3126
- POJ3126
- Poj3126
- poj3126
- poj3126
- POJ3126
- box-sizing
- DateTime.Now.ToString()总结
- 南邮 OJ 1225 石子合并问题
- Java 嵌套类(Nested Class):静态嵌套类,内部类,局部类,匿名类
- POJ 1236 Network of Schools
- POJ3126
- 在C#中新手易犯的典型缺陷
- How to find indexPath for tapped button in tableView Using Seque
- AIX系统修改系统时间
- android主题切换
- Button、Shape、selector和layer-list
- Eclipse下Tomcat配置
- 南邮 OJ 1226 数字三角形问题
- Viewpager轮播图