题目地址: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. RAII手法封装相互排斥锁

    CriticalSectionWrapper是一个接口类 class CriticalSectionWrapper { public: // Factory method, constructor d ...

  2. 解决myeclipse10.x的Servers产生的at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(Unknown Source)错

    错误: java.lang.NullPointerException at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(U ...

  3. SO_KEEPALIVE

    if tcp_keepalive: # 启用KeepAlive选项 sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # TCP_K ...

  4. nginx 查看接口请求时间 每个请求图片的时间或者文件的

    根据nginx的access_log查看接口请求时间 muyuren 发表于 1年前 阅读 2300 收藏 0 推荐 0 评论 0 推荐 收藏 首先修改修改生成日志的格式,在nginx配置文件的htt ...

  5. Java Web开发笔记

    问题: 读取资源文件问题 servletContext.getRealPath() servletContext.getResouce().getPath() setvletContext.getRe ...

  6. JSP的优势

    以下列出了使用JSP带来的其他好处: 与ASP相比:JSP有两大优势.首先,动态部分用Java编写,而不是VB或其他MS专用语言,所以更加强大与易用.第二点就是JSP易于移植到非MS平台上. 与纯 S ...

  7. Unity合并选中物体的Mesh

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; pu ...

  8. linux卸载一个源码包安装的软件的流程

    完全卸载memcached的方法(CentOS) 我的大内存vps(centos系统)曾经安装过memcached,想给论坛提速,实际上不但没有明显效果,反倒耗费内存,看着碍眼,于是想卸载,于是网上各 ...

  9. github常见错误

    如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...

  10. List和Set排序的实现

    List.Set.Map的区别 List和Set继承了Collection接口. List以特定索引来存取元素,可以有重复元素.Set不能存放重复元素(用对象的equals()方法来区分元素是否重复) ...