Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14539   Accepted: 8196

Description

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. 
— It is a matter of security to change such things every now and then, to keep the enemy in the dark. 
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know! 
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door. 
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime! 
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds. 
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened. 
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound. 
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you? 
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.

1033
1733
3733
3739
3779
8779
8179

The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

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
题意: 输入多组数据,每组数据包括两个四位素数1033 8179,每次只改变四位数中的一位并且改变后的数也为素数,从1033到8179有6部。没有路径则输出Imbossiblei。
做法: 这是一个40端口的bfs不过剪枝之后就没有40入口了,入口数远小于40

无论是判定素数还是搜索素数,首先排除偶数,这样就剪掉一半枝叶了

判断素数用根号法判断,

如果一个数X不能被 [2,√X] 内的所有素数整除,那么它就是素数

可以判断的复杂度降到logn

注意:千位的变换要保证千位不为0

其实素数也是用来辅助搜索剪枝的#include <iostream>#include <stdio.h>

#include <queue>
#include <string.h>
using namespace std;
const int MAX=;
int dis[MAX],str[];
bool vis[MAX];
bool just(int n)
{
for(int i=; i*i<=n; i++)
{
if(n%i==)
return ;
}
return ;
}
int bfs(int star,int ends)
{
int y;
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
queue<int> q;
q.push(star);
vis[star]=;
if(star==ends)
return ;
while(!q.empty())
{
int x=q.front();
q.pop();
str[]=x/;
str[]=x/%;
str[]=x/%;
str[]=x%;
for(int i=; i<; i++)
{
int h=str[i];//注意这里记住这个str[i];
if(i==)
for(int j=; j<; j++)
{
str[i]=j;
y=str[]*+str[]*+str[]*+str[];
if(!vis[y]&&just(y))
{
q.push(y);
vis[y]=;
dis[y]=dis[x]+;
if(y==ends)
return dis[y];
}
}
else
for(int j=; j<; j++)
{
str[i]=j;
y=str[]*+str[]*+str[]*+str[];
if(!vis[y]&&just(y))
{
q.push(y);
vis[y]=;
dis[y]=dis[x]+;
if(y==ends)
return dis[y];
}
}
str[i]=h;//在这里返回去;
}
}
return -;
}
int main()
{
int t,star,ends;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%d%d",&star,&ends);
int s=bfs(star,ends);
if(s!=-)
printf("%d\n",s);
else
printf("Impossible\n");
}
return ;
}

poj 3126 Bfs的更多相关文章

  1. Prime Path(POJ 3126 BFS)

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

  2. Prime Path (poj 3126 bfs)

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

  3. POJ - 3126 bfs + 素数筛法 [kuangbin带你飞]专题一

    题意:给定两个四位素数作为终点和起点,每次可以改变起点数的某一位,且改变后的数仍然是素数,问是否可能变换成终点数字? 思路:bfs搜索,每次改变四位数中的某一位.素数打表方便判断新生成的数是否是素数. ...

  4. POJ - 3126 - Prime Path(BFS)

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

  5. BFS POJ 3126 Prime Path

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

  6. Prime Path(POJ - 3126)【BFS+筛素数】

    Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...

  7. 双向广搜 POJ 3126 Prime Path

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

  8. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  9. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

随机推荐

  1. linux命令之面试题1

    1.请解释下列10个shell命令的用途 top:是linux下常用的性能分析工具,能够实时的显示系统中各个进程的资源占用情况,类似于windows的资源管理器,查看系统的cpu,内存,运行时间,交互 ...

  2. CentOS7环境下在/离线安装GCC与GCC-C++

    前几天在准备CentOS7下的编译环境,在线安装GCC和GCC-C++非常简单,只要机器是联网的在Terminal窗口中按顺序分别输入 yum install gcc yum install gcc- ...

  3. JPA概述以及它和Hibernate之间的关系

    http://www.cnblogs.com/Kevin-ZhangCG/p/8996491.html 一.JPA概述以及它和Hibernate之间的关系 1.1.Hibernate 概述 JPA J ...

  4. poj1840

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15133   Accepted: 7426 Description ...

  5. EasyPlayer.js网页全终端播放器安装使用文档

    EasyPlayer.js 集 rtmp, hls, flv, websocket 于一身的网页直播/点播播放器, 使用简单, 功能强大 属性(Property) video-url 视频流地址 St ...

  6. SharePoint服务器端对象模型 之 访问文件和文件夹(Part 1)

    本节中所阐述的内容,主要适用于SharePoint文档库中的文件和文件夹,以及列表中的文件夹.系统中的其他文件(如_layouts中的文件.配置文件.程序文件等)不在本章节的讨论范围之内.   (一) ...

  7. 记录--java获取网络资源(图片、音频等)保存本地

    注:本人开始运行下面报 java.io.FileNotFoundException ,纠结很久后清理tomcat后运行成功 //获取wav文件地址 String vRecordUrl=(request ...

  8. <2013 08 20> -----澳大利亚博士研究生申请-----

    1.澳大利亚昆士兰大学博士的申请一年中什么时间都可以,但奖学金的评选每年只有四轮.和美国不同的是,在提交申请材料之前,个人必须联系好愿意接收你的导师,这个可以自己套磁联系,也可以和那边学院的小秘联系, ...

  9. python系列十五:Python3 错误和异常

    #!/usr/bin/python #-*-coding:gbk-*- #Python3 错误和异常'''Python 语法错误或者称之为解析错语法分析器指出了出错的一行,并且在最先找到的错误的位置标 ...

  10. ThinkPHP官网瀑布流实现分享

    很多人都想做瀑布流的效果,这里告诉大家官网使用的方法. 首先要下载瀑布流的插件jquery.masonry.min.js 地址:http://masonry.desandro.com/index.ht ...