【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整除并且数字包含 ...
随机推荐
- 我遇到的Echarts 最大值不显示&平均值不正常
用Echarts做图表的时候,遇到最大值不显示和平均值不正常的问题,如图: 找了半天,原来是X轴6个坐标有7个数据,只是最后那个数据没有显示出来,而且还是最大的,无语. 加上第七个坐标之后,就显示正常 ...
- Java-五种线程池,四种拒绝策略,三种阻塞队列(转)
Java-五种线程池,四种拒绝策略,三种阻塞队列 三种阻塞队列: BlockingQueue<Runnable> workQueue = null; workQueue = n ...
- Codeforces 884E E. Binary Matrix
题 OvO http://codeforces.com/contest/884/problem/E 884e 解 考虑并查集,每个点向上方和左方的点合并,答案即为1的总数减去需要合并的次数 由于只有1 ...
- 部署安装python3.7
1:安装依赖包 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-deve ...
- C#静态变量 总结
在C#程序中,没有全局变量的概念,这意味着所有的成员变量只有该类的实例才能操作这些数据,这起到了“信息隐藏”的作用.但有些时候,这样做却不是个明智的选择. 假设我们要定义一个图书类,要求该类能保存图书 ...
- [HNOI2004][bzoj1211] 树的计数(prufer序列)
1211: [HNOI2004]树的计数 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3432 Solved: 1295[Submit][Stat ...
- 灰度图像--频域滤波 傅里叶变换之离散傅里叶变换(DFT)
学习DIP第23天 转载请标明本文出处:http://blog.csdn.net/tonyshengtan,欢迎大家转载,发现博客被某些论坛转载后,图像无法正常显示,无法正常表达本人观点,对此表示很不 ...
- Java当中的集合框架Map
简书作者:达叔小生 Java当中的集合框架Map 01 Map提供了三个集合视图: 键集 值集 键-值 映射集 public String getWeek(int num){ if(num<0 ...
- pymysql pymysql.err.OperationalError 1045 Access denied最简单解决办法
我使用的是python3.6+pymysql+mysql8.0 在cmd命令行直接输入mysql回车出现:ERROR 1045 (28000): Access denied for user 'ODB ...
- XOR Guessing
E. XOR Guessing 第一次做这种交互题,刚开始还看不懂,现在已经差不多可以理解了,清空缓存区用cout<<endl;即可,需要注意的是,如果用fflush(stdout)来 ...