描述

孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了。zydsg要做些改变,他想去和素数小姐姐约会。
所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这样,如果两个路口间只差1个数字,则有一条路连通两个路口。(例如1033和1073间有一条路连接)

现在,你知道了zydsg的位置和素数小姐姐的家,问最少zydsg要走多少条路才能见到素数小姐姐。例如:如果zydsg在1033,素数小姐姐的家在8179,最少要走6条街,走法为:

1033

1733

3733

3739

3779

8779

8179

Input
输入数据有多组,首先是一个数字n,代表之后有n组数据。
其次,在每一组输入中,都包含两个数字a和b,代表zydsg的位置和素数小姐姐家的位置。
其中,a和b都是四位数,而且不含前导0。
Output
每组输入输出一行,表示zydsg最少需要走多少条路。若不存在合法的路径,则输出单词“Impossible”。Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

分析

首先我们要筛素数,接下来

方法一:

两个“相邻的”素数连边,每次从开头向终点跑SPFA

方法二:

按个十百千位向四周扩散,BFS

代码(SPFA)

 #include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register int
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<29)
#define maxn 10005
#define lim 10002
#define maxm 1500005
#define add(x,y) e[++ct]=(E){y,head[x]},head[x]=ct
using namespace std;
int n,m,cnt,ct;
int isp[maxn],p[maxn],head[maxn],dis[maxn],vis[maxn],id[maxn];
struct E{
int v,next;
}e[maxm];
inline int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} inline int judge(int x,int y)
{
int sum=;
while(x) {if(x%!=y%) sum++;x/=,y/=;}
return sum==;
} void pre()
{
rep(i,,lim)
{
if(!isp[i]) p[++cnt]=i,id[i]=cnt;
for(RG j=;j<=cnt&&p[j]*i<=lim;j++)
{
isp[i*p[j]]=;
if(!(i%p[j])) break;
}
}
rep(i,,cnt) rep(j,i+,cnt) if(judge(p[i],p[j])) add(i,j),add(j,i);
} int SPFA(int S,int T)
{
memset(dis,,sizeof(dis));dis[S]=;
queue<int> que;que.push(S);
RG u,v;
while(!que.empty())
{
u=que.front(),que.pop(),vis[u]=;
for(RG i=head[u];i;i=e[i].next)
{
v=e[i].v;
if(dis[v]>dis[u]+){
dis[v]=dis[u]+;
if(!vis[v]) vis[v]=,que.push(v);
}
}
}
return dis[T];
} int main()
{
int Tim=read();
pre();
while(Tim--)
{
scanf("%d%d",&n,&m);
printf("%d\n",SPFA(id[n],id[m]));
}
return ;
}

Prime Path[POJ3126] [SPFA/BFS]的更多相关文章

  1. poj3126 Prime Path 广搜bfs

    题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  2. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  3. Prime Path(素数筛选+bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9519   Accepted: 5458 Description The m ...

  4. Prime Path POJ-3126

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

  5. POJ 3126:Prime Path(素数+BFS)

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

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

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

  7. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

  8. POJ 3126 Prime Path (素数+BFS)

    题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...

  9. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

随机推荐

  1. Java接口自动化测试之TestNG测试报告ExtentReports的应用(三)

    pom.xml导入包 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  2. ubuntu 手动更新源 以及使用sudo update与upgrade的作用及区别

    一.今天更新一下我的ubuntu系统,用了几个源发现不怎么好用 上网查了一下发现有说阿里云的源挺好用 然后我试了一下 下载速度还挺快,下面分享一下怎么手动添加源列表 1.最好先做一下备份 sudo c ...

  3. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  4. Pycharm常用操作方法

    1.调整字体大小 2.选择python编译器

  5. python之requests urllib3 连接池

    0.目录 1.参考 2. pool_connections 默认值为10,一个站点主机host对应一个pool (4)分析 host A>>host B>>host A pag ...

  6. [转]利用ssh传输文件

    利用ssh传输文件 http://www.cnblogs.com/jiangyao/archive/2011/01/26/1945570.html 在linux下一般用scp这个命令来通过ssh传输文 ...

  7. ReactNative调试技术-真机调试

    在我开始用ReactNative开始开发APP时,为了能够获取程序运行中的信息,就需要搭建调试环境. 手机调试方式有两类,一类是模拟器方式,另一类是真机模式. 我测试了一下相应的模拟器: 如果用谷歌管 ...

  8. Python_collections_namedtuple可命名元组

    namedtuple:用来构建带字段名的元组 import collections # 创建类,两种创建方法 MytupleClass = collections.namedtuple('Mytupl ...

  9. 伪分布式hbase从0.94.11版本升级stable的1.4.9版本

    Hbase从0.94.11升级到stable的1.4.9版本: 升级思路: hadoop1.1.2    hbase 0.94.11                             ↓ had ...

  10. jQuery Validate自定义错误信息,自定义方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...