Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20237   Accepted: 11282

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

Source

方法很简单,但是我写代码还是写了好久,将思路转化成代码的速度还是太慢。
其中需要素数,又复习了一下素数筛法的方法。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; bool prime[];
int que[];
int step[];
int isprime[];
bool vis[];
int cou=; void getprime(){
int num=;
num=sqrt(num*1.0);
memset(prime,true,sizeof(prime));
prime[]=prime[]=false;
for(int i=;i<;i+=){
prime[i]=false;
}
for(int i=;i<num;i+=){
if(prime[i]){
for(int j=i*i;j<;j+=*i){
prime[j]=false;
}
}
}
for(int i=;i<;i++){
if(prime[i]){
isprime[cou++]=i;
}
}
} bool judge(int a,int b){
int sum=;
int t1[],t2[];
for(int i=;i<;i++){
t1[i]=a%;
a/=;
t2[i]=b%;
b/=;
}
for(int i=;i<;i++){
if(t1[i]==t2[i]){
sum++;
}
}
if(sum==){
return true;
}else{
return false;
}
} int bfs(int a,int b){
int start=;
int endd=;
memset(vis,true,sizeof(vis));
que[endd]=a;
step[endd++]=;
while(start<endd){
int now=que[start];
int nowStep=step[start++];
if(now==b){
return nowStep;
}
for(int j=;j<=cou;j++){
int i=isprime[j];
if(!vis[i]){
continue;
}
if(prime[i]&&judge(i,now)){
if(i==b){
return nowStep+;
}
que[endd]=i;
step[endd++]=nowStep+;
vis[i]=false;
}
}
}
return ;
} int main()
{
int n;
scanf("%d",&n);
int a,b;
getprime();
for(int i=;i<n;i++){
scanf("%d %d",&a,&b);
int ans=bfs(a,b);
printf("%d\n",ans);
}
return ;
}

poj 3126 Prime Path(搜索专题)的更多相关文章

  1. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  2. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  3. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  4. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  6. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

  7. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  8. (简单) POJ 3126 Prime Path,BFS。

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  9. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

随机推荐

  1. Python常用的软件包

    下面是Python开发常用的软件包. 名称 用途 安装命令 opengl   sudo pip3 install PyOpenGL  pyqtgraph GUI图形库 sudo pip3 instal ...

  2. sshd服务

    SSHD服务 介绍:SSH 协议:安全外壳协议.为 Secure Shell 的缩写.SSH 为建立在应用层和传输层基础上的安全协议. 作用 sshd服务使用SSH协议可以用来进行远程控制, 或在计算 ...

  3. Office Web Apps 2013 修改Excel在线查看文件大小限制

    前言 最近搭建了一个OWA 2013环境,帮客户实现在线查看Excel文档,不过,使用过程中出现了错误,文件大小超过10MB就无法预览了,查了好久,发现需要使用PowerShell命令进行修改. 1. ...

  4. u3d不显示阴影的处理方法

    正常情况下都会显示的,如果没有显示,尝试以下几种方案: 1)缩小模型看一看 2)旋转灯光试试,看是否有阴影 3)检查阴影设置 菜单栏Edit –> Project Settings –> ...

  5. cocos creator热更新教程

    1.下载HotUpdate热更新DEMO 2.在cocos creator中下载热更新插件,cocos creator版本要在1.7及以上版本 3.插件默认安装在C:\Users\Administra ...

  6. SharePoint每日小贴士Web部件

    SharePoint每日小贴士Web部件 项目描写叙述         此Web部件从指定SP自己定义列表或一个选定的 RSS源选择一个随机项目.并显示一张图片.标题和一个Tip.         适 ...

  7. Spark(四十四):使用Java调用spark-submit.sh(支持 --deploy-mode client和cluster两种方式)并获取applicationId

    之前也介绍过使用yarn api来submit spark任务,通过提交接口返回applicationId的用法,具体参考<Spark2.3(四十):如何使用java通过yarn api调度sp ...

  8. Delphi10.2 DPR文件

    通过选择[Project | View Source],可以看到DPR文件的基本面貌,操作如下: 默认的 Delphi 项目文件的内容如下: program Project1; {关键字 progra ...

  9. android ScrollView 控制行数

    利用ScrollView 来控制textView 显示的行数 <ScrollView android:layout_width="fill_parent" android:l ...

  10. SpringBoot2.x使用EasyPOI导入Excel浅谈

    SpringBoot2.x使用EasyPOI导入Excel浅谈 平时经常遇到客户要帮忙导入一些数据到数据库中,有些数据比较多有时候手动录入就会很耗时间,所以就自己写一个Excel导入的demo记录一下 ...