<题目链接>

题目大意:

给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四位数。

解题分析:

先打一张素数表,然后进行BFS搜索,对于每次搜索的当前数,枚举某一位与它不同的所有数,判断它是否符合条件,如果符合,就将它加入队列,继续搜索。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<queue>
  5. #include<math.h>
  6. #define min 0x3f3f3f3f
  7.  
  8. using namespace std;
  9.  
  10. int arr[];
  11. int vis[];
  12. int n,m;
  13.  
  14. void prim() //素数打表
  15. {
  16. int i,j;
  17. arr[]=arr[]=;
  18. for(i=;i<=;i++)
  19. {
  20. if(!arr[i])
  21. {
  22. for(j=i*;j<=;j+=i)
  23. arr[j]=;
  24. }
  25. }
  26. }
  27.  
  28. int bfs(int first,int last)
  29. {
  30. queue <int>q;
  31. int v,i,j,temp,vtemp,count[],N[];
  32. memset(vis,,sizeof(vis));
  33. memset(count,,sizeof(count));
  34. q.push(first);
  35. vis[first]=;
  36. while(!q.empty())
  37. {
  38. v=q.front();
  39. q.pop();
  40. N[]=v/;
  41. N[]=v%/;
  42. N[]=v%/;
  43. N[]=v%; //N[]为v的各位数
  44.  
  45. for(j=;j<;j++)
  46. {
  47. temp=N[j];
  48. for(i=;i<;i++)
  49. if(i!=temp) //枚举某一位与当前数不同的数
  50. {
  51. N[j]=i;
  52. vtemp=N[]*+N[]*+N[]*+N[];
  53. if(!vis[vtemp] && !arr[vtemp] && vtemp>) //如果枚举的这个数没有枚举过,并且是一个四位数素数
  54. {
  55. vis[vtemp]=; //已经走过的就标记
  56. count[vtemp]=count[v]+; //步数加一
  57. q.push(vtemp);
  58. }
  59. if(vtemp==last) return count[vtemp]; //找到了最终的数
  60. }
  61. N[j]=temp; //将原来的数还原,防止对下一步造成影响
  62. }
  63. if(v==last) return count[v]; //找到了最终的数
  64. }
  65. return -;
  66. }
  67.  
  68. int main()
  69. {
  70. int t,k;
  71. cin>>t;
  72. prim();
  73. while(t--)
  74. {
  75. cin>>n>>m;
  76. k=bfs(n,m);
  77. if(k!=-)
  78. cout<<k<<endl;
  79. else
  80. cout<<"Impossible"<<endl;
  81. }
  82. return ;
  83. }

下面是另一份代码,与上面枚举当前四位数的每一位数不同的是,这里用的是链式前向星储存某一个四位数能够转化成的所有下一个四位数。但是不知道为什么这份代码WA了。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. const int maxn=+;
  10.  
  11. int isprime[maxn+];
  12. int prime[maxn];
  13. int top;
  14. int n,m,ans;
  15.  
  16. void getprime(){
  17. top=;
  18. memset(isprime,,sizeof(isprime));
  19. for(int i=;i<=sqrt(maxn*1.0);i++){
  20. if(!isprime[i]){
  21. for(int j=i*i;j<=maxn;j+=i){
  22. isprime[j]=;
  23. }
  24. }
  25. }
  26. for(int i=;i<=;i++){ //打表找出四位数的素数
  27. if(!isprime[i]){
  28. prime[++top]=i;
  29. }
  30. }
  31. }
  32.  
  33. const int N=;
  34. struct EDGE{ //用链式前向星记录与当前点只有一位不同的素数,即记录当前数下一步能够转化成的数
  35. int to;
  36. int next;
  37. }edge[N*N]; //因为我打了一下表,发现top的值接近1100,所以这里这样设数组的上界
  38. int head[];
  39. int cnt;
  40.  
  41. struct node{
  42. int loc;
  43. int step;
  44. node(int a=,int b=):loc(a),step(b){}
  45. };
  46. int vis[];
  47.  
  48. bool juge(int a,int b){ //判断这两个数是否只有某一位数不同
  49. int fp=;
  50. while(a){
  51. int a1=a%;
  52. int a2=b%;
  53. a/=;
  54. b/=;
  55. if(a1==a2)fp++;
  56. }
  57. if(fp==)return true;
  58. return false;
  59. }
  60.  
  61. void init(){
  62. memset(head,-,sizeof(head));
  63. cnt=;
  64. }
  65.  
  66. void add(int u,int v){
  67. edge[cnt].to=v;
  68. edge[cnt].next=head[u];
  69. head[u]=cnt++;
  70. }
  71.  
  72. bool bfs(){
  73. memset(vis,,sizeof(vis));
  74. queue<node>q;
  75. q.push(node(n,));
  76. while(!q.empty()){
  77. node now=q.front();
  78. q.pop();
  79. if(now.loc==m){ //找到了最终要找的数
  80. ans=now.step;
  81. return true;
  82. }
  83. if(vis[now.loc])continue;
  84. vis[now.loc]=;
  85. for(int i=head[now.loc];i!=-;i=edge[i].next){ //枚举下一步所有满足条件的数(已经预处理过了)
  86. int v=edge[i].to;
  87. if(vis[v])continue;
  88. q.push(node(v,now.step+));
  89. }
  90. }
  91. return false;
  92. }
  93.  
  94. int main(){
  95. getprime();
  96. int t;scanf("%d",&t);
  97. while(t--){
  98. init();
  99. int a,b;
  100. scanf("%d %d",&a,&b);
  101. for(int i=;i<=top;i++){
  102. if(prime[i]==a)n=i;
  103. if(prime[i]==b)m=i;
  104. }
  105. for(int i=n;i<=m;i++){
  106. for(int j=n;j<=m;j++){
  107. if(juge(prime[i],prime[j])){ //预处理,找到当前数下一步能够转化的所有数
  108. add(i,j);
  109. }
  110. }
  111. }
  112. if(bfs()){
  113. printf("%d\n",ans);
  114. }
  115. else printf("Impossible\n");
  116. }
  117. return ;
  118. }

2018-08-30

POJ 3126 Prime Path【BFS】的更多相关文章

  1. poj 3126 Prime Path 【bfs】

    题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...

  2. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

  3. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  4. (简单) POJ 3126 Prime Path,BFS。

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

  5. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

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

  6. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  7. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  8. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  9. 双向广搜 POJ 3126 Prime Path

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

随机推荐

  1. ORA-00257 archiver error. 错误的处理方法

    archive log 日志已满 方法/步骤 1 SecureCRT登录服务器,切换用户oracle,连接oracle [root@userbeta~]# su - oracle [oracle@us ...

  2. web前端识别文字转语音

    const msg = new SpeechSynthesisUtterance("hello world"); window.speechSynthesis.speak(msg) ...

  3. java多线程快速入门(十八)

    Lock锁是JDK1.5之后推出的并发包里面的关键字(注意捕获异常,释放锁) Lock与synchronized的区别 Lock锁可以人为的释放锁(相当于汽车中的手动挡) synchronized当线 ...

  4. LoadRunner JAVA Vuser接口测试

    注:JDK只支持1.6 1.创建工程Test2.写个经典的HelloWorld类.3.Runas--->Java Application运行下4.将工程下的整个com包拷贝到loadrunner ...

  5. VisualSVN Server如何安装和使用

    首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http://subversion.apache.org/packages.html 这是二进制文件 ...

  6. 论文阅读笔记六:FCN:Fully Convolutional Networks for Semantic Segmentation(CVPR2015)

    今天来看一看一个比较经典的语义分割网络,那就是FCN,全称如题,原英文论文网址:https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn ...

  7. 下载中间件--随机IP代理以及随机User_Agent

    下载中间件随机IP代理以及随机User_Agent 1.在settings.py中设置开启代理功能 # 设置下载中间件 DOWNLOADER_MIDDLEWARES = { # 随机的 User-Ag ...

  8. haproxy admin_stats端口启动错误解决

    /var/log/message里的错误消息大概如下: Feb 13 09:32:50 cluster-node2 haproxy-systemd-wrapper: [ALERT] 043/09325 ...

  9. 使用JDBC连接ElasticSearch6.3(ElasticSearch SQL JDBC)

    使用JDBC连接ElasticSearch6.3(ElasticSearch SQL JDBC) https://blog.csdn.net/scgaliguodong123_/article/det ...

  10. 去除ArrayList集合中的重复自定义对象元素

    要求去除ArrayList集合中重复的Student的对象(什么叫重复,所有属性值都相同叫做重复). 思路: 1.创建一个新集合 2.遍历旧集合中的每一个元素,去新集合中找这个元素,如果这个元素不存在 ...