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. go语言调用cmd

    package main import ( "fmt" "os/exec" ) func main() { //删除C:\Users\Administrator ...

  2. sz与rz

    yum安装root 账号登陆后执行以下命令:yum install -y lrzsz使用说明sz命令发送文件到本地:# sz filenamerz命令本地上传文件到服务器:# rz https://w ...

  3. <转载> Jquery的性能优化-实用!

    我一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些.找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来 ========================= ...

  4. POJ 2479 Maximum sum(双向DP)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Des ...

  5. timus1965(不错的贪心)

    题意是:给你一个1-n的排列,要你把这个排列分成两个序列,且这个两个序列都满足单调性. 题解: 1.首先假设找出的两个序列都是单调递增的(都是单调递减的同理) 那么很容易可以想到,将新加入的数放入到某 ...

  6. 解决asp.net中HTML中talbe的行高被内容撑的变高的问题

    将asp.net页面中的如下语句: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  7. js在页面输出信息的几种方式alert,confirm,prompt,document.write

  8. 0x03 MySQl 库操作

    一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...

  9. ubuntu17.04 配置go环境变量

    把官网下载好的tar解压后,go文件夹放到 /usr/local 目录下 在当前用户的 .bashrc 文件末尾添加 这句话 export PATH=$PATH:/usr/local/go/bin 执 ...

  10. 剑指offer 面试65题

    题目65题:不用加减乘除做加法. 解法一:Python特性 # -*- coding:utf-8 -*- class Solution: def Add(self, num1, num2): # wr ...