Prime Path[POJ3126] [SPFA/BFS]
描述
孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了。zydsg要做些改变,他想去和素数小姐姐约会。
所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这样,如果两个路口间只差1个数字,则有一条路连通两个路口。(例如1033和1073间有一条路连接)
现在,你知道了zydsg的位置和素数小姐姐的家,问最少zydsg要走多少条路才能见到素数小姐姐。例如:如果zydsg在1033,素数小姐姐的家在8179,最少要走6条街,走法为:
1033
1733
3733
3739
3779
8779
8179
其次,在每一组输入中,都包含两个数字a和b,代表zydsg的位置和素数小姐姐家的位置。
其中,a和b都是四位数,而且不含前导0。
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]的更多相关文章
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- Prime Path(素数筛选+bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9519 Accepted: 5458 Description The m ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ 3126:Prime Path(素数+BFS)
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- POJ 3126 - Prime Path - [线性筛+BFS]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- POJ 3126 Prime Path (素数+BFS)
题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
随机推荐
- OpenSSL + Windows 下载安装
Download Win32 OpenSSLhttps://slproweb.com/products/Win32OpenSSL.htmlhttps://wiki.openssl.org/index. ...
- CSS常见Bugs及解决方案列表
以下实例默认运行环境都为Standard mode 如何在IE6及更早浏览器中定义小高度的容器? 方法: #test{overflow:hidden;height:1px;font-size:0;li ...
- es6 箭头函数【箭头表达式】
箭头函数,通过 => 语法实现的函数简写形式,C#/JAVA8/CoffeeScript 中都有类似语法.与函数不同,箭头函数与其执行下文环境共享同一个 this.如果一个箭头函数出现在一个函数 ...
- .net core 中的 DependencyInjection - IOC
概要:因为不知道写啥,所以随便找个东西乱说几句,嗯,就这样,就是这个目的. 1.IOC是啥呢? IOC - Inversion of Control,即控制反转的意思,这里要搞明白的就是,它是一种思想 ...
- Understanding about numerical stability, convergence and consistency
In a computer simulation of the real world, physical quantities, which usually have continuous distr ...
- Factorial Trailing Zeroes Add to List
https://leetcode.com/problems/factorial-trailing-zeroes/#/description 想到了要找2x5:也想到了只要找5,剩下的2 管够.也想到了 ...
- BZOJ4556 [Tjoi2016&Heoi2016]字符串 SA ST表 二分答案 主席树
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4556.html 题目传送门 - BZOJ4556 题意 给定一个长度为 $n$ 的字符串 $s$ . ...
- sql 把一个用逗号分隔的多个数据字符串变成一个表的一列
USE [tms]GO/****** Object: UserDefinedFunction [dbo].[StrToTable] Script Date: 2017/4/26 9:06:20 *** ...
- Practice| 类型转换| 逻辑运算
类型转换 class Pratice1{ public static void main(String[] args){ int a = 3; //float f = 4.5;//TypeChange ...
- 深入理解Python中赋值、深拷贝(deepcopy)、浅拷贝(copy)
赋值 python跟java中的变量本质是不一样的,Python的变量实质上是一个指针(int型或str型),而java的变量是一个可操作的存储空间. a = 123b = a print(id(a) ...