【POJ - 3126】Prime Path(bfs)
Prime Path
原文是English 这里直接上中文了
Descriptions:
给你两个四位的素数a,b。
a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变。
请你计算a最少经过多少次上述变换才能变成b。
例如:1033 -> 8179
1033
1733
3733
3739
3779
8779
8179
最少变换了6次。
Input
第一行输入整数T,表示样例数。 (T <= 100)
每个样例输入两个四位的素数a,b。(没有前导零)
Output
对于每个样例,输出最少变换次数,如果无法变换成b则输出"Impossible"。
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
题目链接:
https://vjudge.net/problem/POJ-3126
两个点,每一步都是素数,步数最小,针对这个,先列出一个素数表,对于每一位的变化都搜一下即可,个人写搜索,习惯用优先队列
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
int T,n,m;
int isprime[];//素数表
int vis[];//标记
struct node
{
int num,money;
bool operator <(const node &c) const//步数小的先出队
{
return money>c.money;
}
} now,next;
void eratos(int x)//求素数表
{
for(int i=; i<=x; ++i)
isprime[i]=true;
isprime[]=isprime[]=false;
for(int i=; i<=x; ++i)
{
if(isprime[i])
{
int j=i+i;
while(j<=x)
{
isprime[j]=false;
j+=i;
}
}
}
}
void bfs()
{
priority_queue<node>q;
now.num=n,now.money=;
q.push(now);
vis[n]=;
int f=;
// cout<<now.num<<endl;
// cout<<2<<endl;
while(!q.empty())
{
char x[];
// cout<<1<<endl;
now=q.top();
q.pop();
if(now.num==m)
{
f=;
cout<<now.money<<endl;
return;
}
for(int i=; i<; ++i)
{
sprintf(x,"%d",now.num);
for(int j=; j<; ++j)
{
if(i==&&j==)//千位不允许为0
continue;
if(i==)//四种情况,分别针对"个十百千"位的变换
next.num=j*+(x[]-'')*+(x[]-'')*+(x[]-'');
else if(i==)
next.num=j*+(x[]-'')*+(x[]-'')*+(x[]-'');
else if(i==)
next.num=j*+(x[]-'')*+(x[]-'')*+(x[]-'');
else if(i==)
next.num=j+(x[]-'')*+(x[]-'')*+(x[]-'')*;
if(isprime[next.num]&&!vis[next.num])//这个数是素数且没被标记过
{
vis[next.num]=;
next.money=now.money+;
q.push(next);
}
}
}
}
if(f==)
{
cout<<"Impossible"<<endl;
return;
}
}
int main()
{
eratos();//10005以内的素数表
cin>>T;
while(T--)
{
ME0(vis);
cin>>n>>m;
bfs();
}
}
【POJ - 3126】Prime Path(bfs)的更多相关文章
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- 【POJ - 3669】Meteor Shower(bfs)
-->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126:Prime Path(素数+BFS)
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- poj3216 Prime Path(BFS)
题目传送门 Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...
- 【POJ 1273】Drainage Ditches(网络流)
一直不明白为什么我的耗时几百毫秒,明明差不多的程序啊,我改来改去还是几百毫秒....一个小时后:明白了,原来把最大值0x3f(77)取0x3f3f3f3f就把时间缩短为16ms了.可是为什么原来那样没 ...
- BZOJ 2296【POJ Challenge】随机种子(构造)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2296 [题目大意] 给出一个数x,求一个10的16次以内的数使得其被x整除并且数字包含 ...
随机推荐
- fullpage.js最后一屏不满一屏时,滚动方式
这两天公司网页改版用到fullpage.js这个滚屏插件,页面内容整屏的滚动,不成问题,各种设置在网上也都有文档.而我遇到的问题就是,页面内容不满屏的时候,和上面的内容放一块就太挤,单独放一屏就太空, ...
- CSS引入外部字体方法,附可用demo
有时候我们做的页面需要用到一些更好看的字体又不想用图片代替,图片会影响加载速度则使用外部字体来显示但是直接通过font-family又不一定全部都行这就需要我们在css中进行定义并且引入字体文件路径然 ...
- 数据管理必看!Kendo UI for jQuery过滤器状态保持
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- Java程序中使用 Jsoup 爬虫( 简单示例 )
一.maven项目里pom添加jsoup依赖 <dependency> <groupId>org.jsoup</groupId> <artifactId> ...
- STM32F103C8T6最小板搞定CMSIS-DAP和SWO功能
转载:http://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=616081&extra=page%3D&am ...
- git回退错误的提交
提交代码导致冲突,执行merge后,冲掉其他人的提交.需要reset,并新建分支进行恢复 解决方法: 1.找到最后一次提交到master分支的版本号,即[merge前的版本号] 2.会退到某个版本号 ...
- yii框架学习(安装)
安装yii: 在本地安装前, 要确保PHP配置了环境变量, 通过cmd输入PHP -v 即可检测. 能看到PHP版本号, 则OK. PHP不是内部命令,则需要添加PHP环境变量. 使用compos ...
- jmeter 5.0,http请求登录返回的cookie在头部处理方法
http登录之后返回的cookie在响应的头部,再次请求虽然加了cookie管理器,但是下一个请求调用响应是登陆失效,这里讲一下我的解决方法 1:在登录之后添加正则表达式,提取cookie 2:提取之 ...
- BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 概率与期望+高斯消元
这个还挺友好的,自己相对轻松能想出来~令 $f[i]$ 表示起点到点 $i$ 的期望次数,则 $ans[i]=f[i]\times \frac{p}{q}$ #include <cmath> ...
- LibreOffice/Calc:在表格中始终显示某列/某行
本文适用于LibreOffice Calc 5.1.6.2 + Ubuntu 16.04,熊猫帮帮主@cnblogs 2018/3/5 编写表格时,我们常常使用表格最左侧的一列和最上方的一行作为序号列 ...