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. nginx访问css js 图片等静态资源,报404或无法定向访问到

    配置完nginx,把php的项目放上去后,发现css,js和图片全部访问不到,一直重定向到根目录执行index.php,郁闷的在网上查了半天,原来不同后缀名的文件访问时都要在nginx.conf中声明 ...

  2. HDU3062-Party(2-SAT)

    pid=3062">题目链接 思路:2-SAT的模版题 代码: #include <iostream> #include <cstdio> #include & ...

  3. PowerDesigner之设置(1)

    这里用到的数据库为SQL2000. PowerDesigner 16. 原始格式: (个人认为的缺点) 创建表前先将原来的表删除(创建表逻辑,假如存在则drop.假如原来有数据,用drop就有问题.新 ...

  4. 【BZOJ2044】三维导弹拦截 DP+(有上下界的)网络流

    [BZOJ2044]三维导弹拦截 Description 一场战争正在A国与B国之间如火如荼的展开. B国凭借其强大的经济实力开发出了无数的远程攻击导弹,B国的领导人希望,通过这些导弹直接毁灭A国的指 ...

  5. 【BZOJ4199】[Noi2015]品酒大会 后缀数组+并查集

    [BZOJ4199][Noi2015]品酒大会 题面:http://www.lydsy.com/JudgeOnline/wttl/thread.php?tid=2144 题解:听说能用SAM?SA默默 ...

  6. maven 打包报错

    [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------ ...

  7. 服务器http请求https服务时报错解决方案

    问题一. 问题二. java.security.KeyException 解决: Java.security.KeyException的解决 程序调用环信的接口时,出现此异常. 环境:centos , ...

  8. 小程序html 显示 图片处理

    let arr = [] for (const v of r.data.data ){ // v.content = v.content.replace(/<img/g ,' <image ...

  9. Vue.js中this.$nextTick()的使用

    this.$nextTick()将回调延迟到下次 DOM 更新循环之后执行.在修改数据之后立即使用它,然后等待 DOM 更新.它跟全局方法 Vue.nextTick 一样,不同的是回调的 this 自 ...

  10. Python3.6全栈开发实例[025]

    25.文件a1.txt内容(升级题)name:apple price:10 amount:3 year:2012name:tesla price:100000 amount:1 year:2013通过 ...