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. 进程已经被attach debug,如何解除其debug权限?

    今天碰到一个问题,详情: 进程A创建了进程B,并且进程A在创建进程B的时候指定了debug权限: 我的进程C启动了一个Hook,然后系统将我的X dll载入到进程B中: 此时,用visual stud ...

  2. go http的三种实现---3

    package main //效率最高的一个方法 import ( "fmt" "io" "log" "net/http" ...

  3. Ubuntu 14.04 Vim编辑文件的一般操作

    vim编辑文件的一般操作 1. vim #在命令行中输入vim,进入vim编辑器 2. i #按一下i键,下端显示 --INSERT-- #插入命令,在vim中可能任意字符都有作用 3. Esc #退 ...

  4. php中变量引用&不可与global共同使用

    问题来源,新公司debug. 程序中代码大致如下 class Ci { private static $instance = NULL; public $name = 'hello'; public ...

  5. The server encountered an internal error that prevented it from fulfilling this request.(JsonMappingException: Conflicting getter definitions)

    在测试一个方法,dubug查看查询结果已经出来了,结果页面上是The server encountered an internal error that prevented it from fulfi ...

  6. convex hull trick CF344.E

    类似于斜率优化的东西,果真CF的E以后才会考点算法啊. 感觉这种优化应该很常见,但这题直线只有第一象限的,但是插入,和查找操作是不变的,按极角排序后就可以直接用这个模板了. #include < ...

  7. javaScript Number对象

    Number 对象 Number 对象是原始数值的包装对象. 创建 Number 对象的语法: var myNum=new Number(value); var myNum=Number(value) ...

  8. Nuxt使用element-ui

    废话不多说,在Nuxt中引入Nuxt其实很简单,分下面几步 一.安装element-ui依赖 打开nuxt项目以后,在Terminal中通过 npm i element-ui -S 即可安装eleme ...

  9. REST --- Representational State Transfer --- 表现层状态转化

    引用:阮一峰的网络日志 如果一个架构符合REST原则,就称它为RESTful架构. 要理解RESTful架构,最好的方法就是去理解Representational State Transfer这个词组 ...

  10. 鸟哥的Linux私房菜-第一部分-第2章Linux如何学习

    第2章 Linux如何学习 Linux可以干什么 企业级:网络服务器.金融数据库.大型企业网管环境.高性能计算.集群 个人:桌面计算机.手机.PDA(掌上电脑,这个电脑的意义十分广泛,在不同的场景下有 ...