题目地址:http://poj.org/problem?id=3126

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

  1. 3
  2. 1033 8179
  3. 1373 8017
  4. 1033 1033

Sample Output

  1. 6
  2. 7
  3. 0
    分析:从一个四位素数a 变到另一个四位素数b,求最少的变换次数。bfs求最少的次数!(变换的要求请读题目)
    一开始在记录一个数是否被访问过的时候,用了一种比较的方式,即:比较当前要生成的数和cur是否相同,这种
    判断是否该数字是否访问过的方法是不对的,因为这样判断还是会导致一个数可能会被多次加入队列,最后TLE
    故,改为vis[]标记判断是否访问过,这样每个数顶多会被加入队列一次。AC
    代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6. #include <cmath>
  7. #include <iostream>
  8. #include <string>
  9. #include <queue>
  10. #include <stack>
  11. #include <vector>
  12. #include <algorithm>
  13. #define N 100000+100
  14.  
  15. using namespace std;
  16.  
  17. struct node
  18. {
  19. int a, b, c, d; //a不能等于0
  20. int path;
  21. };
  22.  
  23. int f[];
  24. bool vis[];
  25.  
  26. void sushu()
  27. {
  28. int i, dd=sqrt(+0.5);
  29. memset(f, , sizeof(f));
  30. i=; f[]=f[]=; //not
  31. while(i<=dd){
  32. for(int j=i+i; j<=; j+=i)
  33. f[j]=; // not
  34. i++;
  35. while(f[i]==) i++;
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. sushu(); //筛素数
  42. // printf("%d %d", f[1001], f[1003] );
  43.  
  44. int tg; scanf("%d", &tg);
  45. int dd, ff;
  46. int i, j;
  47.  
  48. while(tg--){
  49. scanf("%d %d", &dd, &ff);
  50. if(dd==ff){
  51. printf("0\n"); continue;
  52. }
  53. node s, e;
  54. s.a=dd/; s.b=dd/%; s.c=dd/%; s.d=dd%; s.path=;
  55. e.a=ff/; e.b=ff/%; e.c=ff/%; e.d=ff%;
  56.  
  57. queue<node>q; bool flag=false;
  58. q.push(s); node cur;
  59. int ans=;
  60. memset(vis, false, sizeof(vis));
  61. vis[dd]=true;
  62.  
  63. while(!q.empty()){
  64. cur=q.front(); q.pop();
  65.  
  66. for(i=; i<=; i+=){//枚举个位 偶数排除
  67. int temp=cur.a*+cur.b*+cur.c*+i;
  68. if(!vis[temp] && f[temp]==){
  69. node cc=cur; cc.d=i; cc.path=cur.path+;
  70. vis[temp]=true;
  71. if(temp==ff){
  72. flag=true; ans=cc.path; break;
  73. }
  74. q.push(cc);
  75. }
  76. }
  77. if(flag) break;
  78.  
  79. for(i=; i<=; i++){ //枚举个位
  80.  
  81. int temp=cur.a*+cur.b*+i*+cur.d;
  82. if(!vis[temp] &&f[temp]==){
  83. node cc=cur; cc.c=i; cc.path=cur.path+;
  84. vis[temp]=true;
  85. if(temp==ff){
  86. flag=true; ans=cc.path; break;
  87. }
  88. q.push(cc);
  89. }
  90. }
  91. if(flag) break;
  92.  
  93. for(i=; i<=; i++){//枚举百位
  94. int temp=cur.a*+i*+cur.c*+cur.d;
  95. if(!vis[temp] &&f[temp]==){
  96. node cc=cur; cc.b=i; cc.path=cur.path+;
  97. vis[temp]=true;
  98. if(temp==ff){
  99. flag=true; ans=cc.path; break;
  100. }
  101. q.push(cc);
  102. }
  103. }
  104. if(flag) break;
  105.  
  106. for(i=; i<=; i++){ //枚举千位 千位不能为0
  107. int temp=i*+cur.b*+cur.c*+cur.d;
  108. if(!vis[temp] &&f[temp]==){
  109. node cc=cur; cc.a=i; cc.path=cur.path+;
  110. vis[temp]=true;
  111. if(temp==ff){
  112. flag=true; ans=cc.path; break;
  113. }
  114. q.push(cc);
  115. }
  116. }
  117. if(flag) break;
  118. }
  119. if(flag) printf("%d\n", ans );
  120. else printf("Impossible\n");
  121. }
  122. return ;
  123. }
  1.  

poj 3126 Prime Path 【bfs】的更多相关文章

  1. POJ 3126 Prime Path【BFS】

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

  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. IBM Security App Scan 资料整理

    转自:http://blog.csdn.net/u013147600/article/details/50002089 这是学习和使用IBM AppScan过程中总结整理的一些资料.   扫描系统操作 ...

  2. Rabbitmq消息队列(一) centos下安装rabbitmq

    1.简介 AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的 ...

  3. How to Use HTML5 FUll Screen API(怎样使用HTML5全屏接口)

    原文链接:http://www.sitepoint.com/use-html5-full-screen-api/ 假设你不太喜欢变化太快的东西,那么web开发可能不适合你. 我曾在2012年末有写过F ...

  4. stage3D基础三------什么是AGAL(转)

    原文链接 http://www.adobe.com/cn/devnet/flashplayer/articles/hello-triangle.html 在本文中,你将研究一个能够正常运行的基于Sta ...

  5. IIS8应用池重启脚本

    重启 IIS8 应用程序池的批处理 批处理很简单:c:\windows\system32\inetsrv\AppCmd.exe stop apppool /apppool.name:"ASP ...

  6. git 修改远程仓库地址

    以前的老项目需要修改git路径,为了保留之前的上传记录和分支等可以通过以下方法解决这个问题 sourceTree项目远程仓库,直接修改origin路径,然后提交一个commit即可将项目上传到新的gi ...

  7. args *args **kwargs区别

    python 函数中的参数类型有两种,分别为 位置参数和关键字参数: 一 .位置参数(该类参数位置固定不变) args:     表示默认位置参数,该参数是具象的,有多少个参数就传递多少参数,且参数位 ...

  8. 【JMeter4.0学习(八)】之断言

    目录 响应断言 一.响应断言 1.添加线程组 2.添加HTTP请求默认值 3.添加HTTP请求1 4.先运行“HTTP请求1”,查看结果树的“取样器结果.请求.响应数据” ①取样器结果 ②请求 ③响应 ...

  9. vue实践---vue结合 promise 封装原生ajax

    有时候不想使用axios这样的外部依赖,想自己封装ajax,这里有两种方法 方法一,在单个页面内使用 封装的代码如下: beforeCreate () { this.$http = (() => ...

  10. opengl绘制图片

    #include <GL/glew.h>#include <glut.h>#include "FreeImage.h"#include <stdio. ...