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. C# 获取字符串字节长度

    一.C# 获取字符串字节长度 1.在C# 语言中使用string 字符串Unicode 编码 2.在C#语言中常用汉字 占 3个字节 方式1:使用默认编码类获取字节长度 Console.WriteLi ...

  2. Pandas 使用笔记

    创建空的数据框: import pandas as pd df = pd.DataFrame(columns = ["ebayno", "p_sku", &qu ...

  3. mac 下apache服务的根目录

    根据文章的介绍 http://jingyan.baidu.com/article/67508eb434539f9cca1ce4da.html apache服务的根目录是在 /Library/WebSe ...

  4. [Vuex] Use Namespaces in Vuex Stores using TypeScript

    Even by using modules, they still share the same namespace. So you couldn’t have the same mutation n ...

  5. URL参数编码

    简单明了区分escape.encodeURI和encodeURIComponent 一.前言讲这3个方法区别的文章太多了,但是大部分写的都很绕.本文试图从实践角度去讲这3个方法. 二.escape和它 ...

  6. 时间mysql

    查询一天/今天: select * from table where to_days(column_time) = to_days(now()) select * from table where d ...

  7. 【转】33 个 2017 年必须了解的 iOS 开源库

    1.IGListKit,作者是Instagram Engineering Instagram 程序员做的,IGListKit 是数据驱动的 UICollectionView 框架,为了构建快速和可扩展 ...

  8. Windows Server 2008 IIS安装FTP及端口配置

    添加角色IIS,选择上FTP服务 打开IIS,右击网站,添加FTP站点 允许访问的指定用户,必须是Windows系统真实存在的用户,为了安全起见,此用户只赋予user组即可,不能赋予远程桌面权限 如果 ...

  9. 云服务jdk 升级为 OpenJDK11

    由于oracle是个碧池,大家都懂,今年来,jdk版本更新的越来越频繁,所以目前商业版本需要收费了 每个企业都要考虑这样的问题,所以嘛,新项目试水肯定是要的,用openJDK11吧, https:// ...

  10. [转]用JAVA在读取EXCEL文件时如何判断列隐藏

    原文地址:https://www.cnblogs.com/OwenWu/archive/2012/01/03/2310620.html org.apache.poi.hssf.usermodel.HS ...