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. 开源流媒体服务器SRS学习笔记(3) - HTTPCallback实现安全认证

    按上回继续,安全论证是绝大多数应用的基本要求,如果任何人都能无限制的发布/播放视频,显然不适合.SRS中可以通过HTTPCallback机制来实现,参考下面的配置: ... vhost __defau ...

  2. [asp.net core]The requested page cannot be accessed because the related configuration data for the page is invalid.

    bug HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the rela ...

  3. CSS魔法堂:那个被我们忽略的outline

    前言  在CSS魔法堂:改变单选框颜色就这么吹毛求疵!中我们要模拟原生单选框通过Tab键获得焦点的效果,这里涉及到一个常常被忽略的属性--outline,由于之前对其印象确实有些模糊,于是本文打算对其 ...

  4. mysql百分比显示

    select doll_name, type, value concat( left(get /(get+ fall)*100, 5), '%') as 抓取概率 from doll_conf

  5. PHPStrom激活方法

    直接用浏览器打开 http://idea.lanyus.com/ 点击页面中的“获得注册码”, 然后在注册时切换至Activation Code选项,输入获得的注册码一长串字符串 如果提示红字体信息, ...

  6. vue: 代码小记

    1.事件派发:子控件->父控件 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  7. 【PHP】解析PHP中的函数

    目录结构: contents structure [-] 可变参数的函数 变量函数 回调函数 自定义函数库 闭包(Closure)函数的使用 在这篇文章中,笔者将会讲解如何使用PHP中的函数,PHP是 ...

  8. SVN:This client is too old to work with working copy…解决的方法

    解决svn:This client is too old问题 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXlmbXlmbXlmbXlm/font/5a ...

  9. 认知:关于Android 调试的坑

    要注意充电线和数据线的区别! 要注意充电线和数据线的区别! 要注意充电线和数据线的区别! 可以通过访问 :chrome://inspect/#devices  查看设备是否正常. 通常电脑也会有提示, ...

  10. SNF软件开发机器人产品白皮书

    软件开发机器人 产品白皮书 使用说明书 模块名称:软件开发机器人 模块编号:12 项目负责人:王金斗 所属部门:技术中心 文档编制: 编制日期:2018-02-02 文档审核:王金斗 审核日期: 文档 ...