POJ-3126 Prime Path
来源:互联网 发布:小学一年级数学软件 编辑:程序博客网 时间:2024/06/14 10:10
Prime Path
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20932 Accepted: 11648
Description
![](http://poj.org/images/3126_1.jpg)
— 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
题意:给一个四位数,要求变换成另外一个四位数,每次只能改变一个数字,且改变后的数字必须为素数,问从给定四位数变换到目标数字最少需要几次变化。如果无法到达则输出Impossible。
思路:BFS,从目标数字开始依次改变个位数十位数百位数和千位数,其中个位数一定不是偶数且千位数不能为零,每改变一次则判断是否是素数,再判断是否等于目标数,如果等于目标数则输出结果,否则加入队列等待下一次BFS。素数可以采用函数判断或者先对1000到10000间的数字进行素数打表(然而我最开始就是用的函数判断法,一直WA,没有素数判断算法,改为素数打表一下就AC了比较迷),这里的数字比较小,所以只用普通的素数判断算法就可以了,不用素数筛也不会TLE。
#include <iostream>#include <cmath>#include <cstring>#include <queue>#define mst(b,ss) memset(ss,b,sizeof(ss))#define maxn 10005using namespace std;bool visit[maxn];int step[maxn];int prime[maxn];int n,m;queue <int> q;bool isprime(int n){ for(int i=2;i<=sqrt(n);i++) { if (n%i==0) return false; } return true;}void init(){ for (int i=1000;i<10000;i++) if(isprime(i)) prime[i]=1;}void bfs(int n){ step[n]=0; visit[n]=true; q.push(n); while(!q.empty()) { int X=q.front(); q.pop(); if(X==m) { cout << step[m] << endl; return ; } for(int i=1;i<=9;i+=2) { int temp=X/10*10+i; if(!visit[temp]&&prime[temp]) { visit[temp]=true; step[temp]=step[X]+1; q.push(temp); } } for(int i=0;i<=9;i++) { int temp=X/100*100+i*10+X%10; if(!visit[temp]&&prime[temp]) { visit[temp]=true; step[temp]=step[X]+1; q.push(temp); } } for(int i=0;i<=9;i++) { int temp=X/1000*1000+i*100+X%100; if(!visit[temp]&&prime[temp]) { visit[temp]=true; step[temp]=step[X]+1; q.push(temp); } } for(int i=1;i<=9;i++) { int temp=i*1000+X%1000; if(!visit[temp]&&prime[temp]) { visit[temp]=true; step[temp]=step[X]+1; q.push(temp); } } } cout << "Impossible" << endl;}int main(){ int t; cin >> t; init(); while(t--) { cin >> n >> m; memset(visit,false,sizeof(visit)); memset(step,0,sizeof(step)); while(!q.empty()) q.pop(); bfs(n); } 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
- 很好看的后台管理界面
- 搭建Struts2 环境
- 朴素贝叶斯分类原理及Python实现简单文本分类
- 1642 Set
- 二叉树的层次遍历
- POJ-3126 Prime Path
- USACO——Paired Up
- LINUX字符设备驱动纠正
- MySQL数据库操作
- C. Vladik and Memorable Trip
- 图像处理基础知识系列之二:核概率密度估计简介
- 网站怎么赚钱? 靠流量赚钱吗? 广告吗? 建什么网站赚钱
- scala隐式转换
- 在Nanopi-NEO上直接编译带Cedrus-avcodec的ffmpeg