poj3126:http://poj.org/problem?id=3126

题意:给你两个数n,k,两个数都是四位数的素数。现在让你改变n的一位数,让n变成另外一个素数。然后把这个素数在改变其中的以为又变成一个新的素数,问你最少有这样变换几步,才能使得使他变成k。
题解:求最短的问题,很自然的就想到了BFS,此外这一题还要处理10000以内的素数,可以采用素数筛法。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int prime[];//标记该数是不是素数
struct Node{
int x;
int step;
};
int counts[];
int n,k;
void DealPrime(){//素筛
memset(prime,,sizeof(prime));
for(int i=;i<=;i++){
if(prime[i])
for(int j=*i;j<=;j+=i)
prime[j]=;
}
}
int BFS(int x){
queue<Node>Q;
for(int i=;i<=;i++)
counts[i]=;
Node tt;
tt.x=x;
tt.step=;
Q.push(tt);
counts[x]=;
while(!Q.empty()){
Node temp=Q.front();
Q.pop();
int xx=temp.x;
int a=xx%;//取出个位
int b=xx/%;//十位
int c=xx/%;//百位
int d=xx/;//千位
int step=temp.step;
for(int i=;i<=;i++){//个位的变换
int xxx=d*+c*+b*+i;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//十位的变换
int xxx=d*+c*+i*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//百位的变换
int xxx=d*+i*+b*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//千位的变换,注意千位不能为0
int xxx=i*+c*+b*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
}
return counts[k];
}
int main(){
int cas;
scanf("%d",&cas);
DealPrime();
while(cas--){
scanf("%d%d",&n,&k);
int ans=BFS(n);
if(ans==)
printf("Impossible\n");
else
printf("%d\n",ans);
} }

Prime Path的更多相关文章

  1. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  2. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  3. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  4. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

  5. Prime Path(poj 3126)

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

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

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

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

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

  8. Prime Path(POJ 3126 BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Descr ...

  9. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  10. Prime Path (poj 3126 bfs)

    Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Ac ...

随机推荐

  1. python装饰实现线程同步

    import threading def tryfinally(finallyf):   u"returns a decorator that adds try/finally behavi ...

  2. vi模式

    保存命令 按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi:w file 将修改另外保存到file中,不退出vi:w! 强制保存,不推出vi:wq 保存文件并退出vi:wq! 强制保存文件 ...

  3. linux上传下载软件

    如何实现windows和linux之间的文件传输 (原文地址:http://hi.baidu.com/ying5420/item/439dee93f0f7fd1a934f41e2) 如果想从windo ...

  4. xslt语法之---基础语法

    1. XSLT常用元素: 1.1 <xsl:template>:创建模板     Match属性的作用是使模板和XML元素相关联 <xsl:template match=" ...

  5. tomcat work 目录

    用tomcat作web服务器的时候,部署的程序在webApps下,这些程序都是编译后的程序(发布到tomcat的项目里含的类,会被编译成.class后才发布过来,源文件没有发布过来,但这里的jsp没有 ...

  6. SQL查询显示行号、随机查询、取指定行数据

    转自:walkingp 1.显示行号 如果数据没有删除的情况下主键与行号是一致的,但在删除某些数据,行号就与主键不一致了,这时需要查询行号就需要用新的方法,在SQL Server2005之前,需要使用 ...

  7. python字符串跟整型互转

    print ("整型:",int(50))a=int(50)print("整型:",a)numStr = "50";print (" ...

  8. siege安装和使用

    siege(支持http.https).多url. 下载地址:http://www.joedog.org/index/siege-home Cent os系统: 1)./configure 2)mak ...

  9. nodejs开发环境sublime配置

    前端时间使用webstorm搭建一个node.js的学习环境,感觉非常强大.不过由于其加载的速度,每次让都让我抓狂.后来我找到了一个sublime.虽说3.0以上是收费的,2.0暂时免费.官方的不对s ...

  10. winows8.1或winows7 64bit 安装Itunes 64bit 11.1.3 无法打开一直停止工作的解决办法

    winows8.1或winows7 64bit 安装Itunes 64bit 11.1.3 无法打开一直停止工作的解决办法 系统环境变量里的Path追加 ;C:\program files (x86) ...