POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法)
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<cstring>
using namespace std;
int a,b;
struct node{
int num;
int step;
};
node que[10000];//默认初始化为0
int visit[10000];//默认初始化为0
int prime(int d){
if(d<=1) return 0;
for(int i=2;i<=sqrt(double(d));i++)
if(d%i==0) return 0;
return 1;
}
void bfs(){
int i;
int p1,p2;
p1=0;
que[p1].num=a;
que[p1].step=0;
visit[a]=1;
p2=1;
if(a==b){//考虑 a , b 相等的时候
printf("%d\n",0);
return ;
}
while(p1<p2){
node x=que[p1];
p1++; int unit=x.num%10;//取出个位
int decade=(x.num/10)%10;//取出十位
for(i=1;i<=9;i=i+2){ //枚举个位,保证为奇数(偶数必定不是素数)
int y=(x.num/10)*10+i;
if(y==b) { //如果找到了,直接退出
printf("%d\n",x.step+1);
return ;
}
if(visit[y]==0 &&prime(y)) {
visit[y]=true;
que[p2].num=y;
que[p2].step=x.step+1;
p2++;
}
}
for(i=0;i<=9;i++){ //枚举十位
int y=(x.num/100)*100+i*10+unit;
if(y==b) { //如果找到了,直接退出
printf("%d\n",x.step+1);
return ;
}
if(visit[y]==0 &&prime(y)) {
visit[y]=true;
que[p2].num=y;
que[p2].step=x.step+1;
p2++;
}
}
for(i=0;i<=9;i++){ //枚举百位
int y=(x.num/1000)*1000+i*100+decade*10+unit;
if(y==b) { //如果找到了,直接退出
printf("%d\n",x.step+1);
return ;
}
if(visit[y]==0 &&prime(y)){
visit[y]=true;
que[p2].num=y;
que[p2].step=x.step+1;
p2++;
}
}
for(i=1;i<=9;i++) {//枚举千位,保证千位不为0
int y=i*1000+x.num%1000;
if(y==b) { //如果找到了,直接退出
printf("%d\n",x.step+1);
return ;
}
if(visit[y]==0 &&prime(y)) {
visit[y]=true;
que[p2].num=y;
que[p2].step=x.step+1;
p2++;
}
}
}
printf("Impossible\n");
return;
}
int main(){
int n;
scanf("%d",&n);
while(n--) {
memset(visit,0,sizeof(visit));//不要忘记初始化
scanf("%d%d",&a,&b);
bfs();
}
return 0;
}
下面代码优化了一下判断素数的代码
int prime(int d){
if(d==2) return 1;//特殊情况,偶数2是素数
if( d<=1 || d%2==0 ) return 0;//排除d<=1时以及d为偶数时
for(int i=3;i<=sqrt(double(d));i=i+2)
if(d%i==0) return 0;
return 1;
}
POJ 3126 Prime Path(BFS算法)的更多相关文章
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- POJ 3126 Prime Path (BFS+剪枝)
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- Android使用LinearViewLayout展示数据
如果要滚动,使用ScrollView来包裹这个LinearViewLayout. ListView控件,自己带有滚动效果的. BaseAdapter LayoutInflater 其他两种绑定方式 A ...
- 【Mac系统】之fiddler下载和安装
使用教程参考:http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html#request 一.首先,在Mac下安装fiddler时, ...
- lnmp 环境require(): open_basedir restriction in effect 错误
最近配置开发用的lnmp环境,环境配置完成后,爆500错误,查看nginx错误日志 open_basedir 将 PHP 所能打开的文件限制在指定的目录树,包括文件本身 错误日志显示,访问脚本不在 o ...
- 使用strace,lstrace,truss来跟踪程序的运行过程
使用truss.strace或ltrace诊断软件问题 2008-07-05 16:25 使用truss.strace或ltrace诊断软件问题 进程无法启动,软件运行速度突然变慢, ...
- 正负小数点后两位浮点数--jquery
背景:项目中需要做个对两位小数点的正负浮点数的处理, 要求:非数字或者.字符自动清除,并对.12自动修补.前的0 原理:在输入框中加入两个事件,keyup与blur,keyup处理字符串中非要求的字符 ...
- CSU 1663: Tree(树链剖分)
1663: Tree Time Limit: 5 Sec Memory Limit: 128 MB Submit: 26 Solved: 11 [Submit][id=1663"> ...
- 趣味编程:C#中Specification模式的实现(参考答案 - 下)
一篇文章中我们利用C#语言的特性实现了一种轻量级的Specification模式,它的关键在于抛弃了具体的Specification类型,而是使用一个委托对象代替唯一关键的IsSatisfiedBy方 ...
- 安装Hadoop 1.1.2 (二 安装配置SSH)
1 查找SSH yum search ssh 2 如果没有安装, yum install openssh.x86_64 4 直接运行 ssh-keygen -t dsa -P '' -f /roo ...
- vptr
#include <stdio.h> class Point3d { public: virtual ~Point3d(){} public: static Point3d origin; ...
- 【基础版限时免费】致敬WebForms,ASP.NET Core也能这么玩!
ASP.NET WebForms ASP.NET WebForms 随着微软 2000 年的 .Net Framework 一起发布,至今也将近 20 年的时间.相信很多人和我一样,对 WebForm ...