题目地址: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

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0
分析:从一个四位素数a 变到另一个四位素数b,求最少的变换次数。bfs求最少的次数!(变换的要求请读题目)
一开始在记录一个数是否被访问过的时候,用了一种比较的方式,即:比较当前要生成的数和cur是否相同,这种
判断是否该数字是否访问过的方法是不对的,因为这样判断还是会导致一个数可能会被多次加入队列,最后TLE。
故,改为vis[]标记判断是否访问过,这样每个数顶多会被加入队列一次。AC!
代码:
 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define N 100000+100 using namespace std; struct node
{
int a, b, c, d; //a不能等于0
int path;
}; int f[];
bool vis[]; void sushu()
{
int i, dd=sqrt(+0.5);
memset(f, , sizeof(f));
i=; f[]=f[]=; //not
while(i<=dd){
for(int j=i+i; j<=; j+=i)
f[j]=; // not
i++;
while(f[i]==) i++;
}
} int main()
{
sushu(); //筛素数
// printf("%d %d", f[1001], f[1003] ); int tg; scanf("%d", &tg);
int dd, ff;
int i, j; while(tg--){
scanf("%d %d", &dd, &ff);
if(dd==ff){
printf("0\n"); continue;
}
node s, e;
s.a=dd/; s.b=dd/%; s.c=dd/%; s.d=dd%; s.path=;
e.a=ff/; e.b=ff/%; e.c=ff/%; e.d=ff%; queue<node>q; bool flag=false;
q.push(s); node cur;
int ans=;
memset(vis, false, sizeof(vis));
vis[dd]=true; while(!q.empty()){
cur=q.front(); q.pop(); for(i=; i<=; i+=){//枚举个位 偶数排除
int temp=cur.a*+cur.b*+cur.c*+i;
if(!vis[temp] && f[temp]==){
node cc=cur; cc.d=i; cc.path=cur.path+;
vis[temp]=true;
if(temp==ff){
flag=true; ans=cc.path; break;
}
q.push(cc);
}
}
if(flag) break; for(i=; i<=; i++){ //枚举个位 int temp=cur.a*+cur.b*+i*+cur.d;
if(!vis[temp] &&f[temp]==){
node cc=cur; cc.c=i; cc.path=cur.path+;
vis[temp]=true;
if(temp==ff){
flag=true; ans=cc.path; break;
}
q.push(cc);
}
}
if(flag) break; for(i=; i<=; i++){//枚举百位
int temp=cur.a*+i*+cur.c*+cur.d;
if(!vis[temp] &&f[temp]==){
node cc=cur; cc.b=i; cc.path=cur.path+;
vis[temp]=true;
if(temp==ff){
flag=true; ans=cc.path; break;
}
q.push(cc);
}
}
if(flag) break; for(i=; i<=; i++){ //枚举千位 千位不能为0
int temp=i*+cur.b*+cur.c*+cur.d;
if(!vis[temp] &&f[temp]==){
node cc=cur; cc.a=i; cc.path=cur.path+;
vis[temp]=true;
if(temp==ff){
flag=true; ans=cc.path; break;
}
q.push(cc);
}
}
if(flag) break;
}
if(flag) printf("%d\n", ans );
else printf("Impossible\n");
}
return ;
}
												

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. JQuery小结(转)

    一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...

  2. ES 31 - 从0开始搭建Elasticsearch生产集群

    目录 1 配置环境 1.1 服务器IP映射 1.2 配置各节点的ssh免密通信 1.3 安装JDK并配置环境变量 2 部署单节点服务 3 部署集群服务 4 启动集群中的所有节点 4.2 启动各个节点中 ...

  3. MySQL_常用SQL语句

    1.按小时统计的语句 select concat(date_format(gmt_create, "%Y-%m-%d %k:00~"), hour(gmt_create)+1, & ...

  4. Visual Assist X安装路径

    C:\Users\系统用户名\AppData\Local\Microsoft\VisualStudio\VS版本号\Extensions\VAX插件目录\

  5. BZOJ 2176 Strange string 最小表示法

    题目大意:给定一个串S,求最小表示法 n<=1000W,实在不敢写后缀自己主动机,就去学了最小表示法= = 记得用unsigned char不然WA= = 数据真是逗- - #include & ...

  6. Dockerfile安装KOD可道云

    [root@docker01 base2]# cat Dockerfile FROM centos:6.8 RUN yum install openssh-server -y RUN /etc/ini ...

  7. java中什么是bridge method(桥接方法)

    java中什么是bridge method(桥接方法) https://blog.csdn.net/z69183787/article/details/81115524

  8. C语言进行站点开发之cgi

     安装Apach 配置ApacheRuntime 以下的过程中一直点击next 配置CGI,放开配置:AddHandler cgi-script .cgi watermark/2/text/aHR ...

  9. PHP-Manual的学习----【语言参考】----【类型】-----【Boolean类型】

    2017年7月20日15:41:26Boolean 布尔类型 1.这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE. 其实就是真假的问题.2.语法 要指定一个布尔值,使用 ...

  10. GUN C中的错误报告

    在C语言中,很多库函数在调用失败时都会返回特定的值.比如返回-1,空指针,EOF等.但是这些值仅仅表示的调用失败,并未给出详细的错误信息.如果想查看详细的错误内容,就要去查看errno的错误代码,er ...