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. 1254 Problem V

    问题 V: 光棍的yy 时间限制: 1 Sec  内存限制: 128 MB 提交: 42  解决: 22 [提交][状态][讨论版] 题目描述 yy经常遇见一个奇怪的事情,每当他看时间的时候总会看见1 ...

  2. day4笔记

    今日讲解内容:1,int数字:运算.1 ,2,3... # 数字类型:int #范围.用于运算, + - * / // %.... bit_lenth :十进制数字用二进制表示的最小位数 a=10 p ...

  3. 使用MyBatis_Generator工具jar包自动化生成Dto、Dao、Mapping 文件

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  4. 如何用Python输出一个斐波那契Fibonacci数列

    a,b = 0, 1 while b<100: print (b), a, b = b, a+b

  5. 我的Android进阶之旅------>ListView中android:cacheColorHint,android:listSelector属性作用 .

    ( 本文转载于:http://blog.csdn.net/stonecao/article/details/6216449) 自定义listview的时候,当你不使用android:cacheColo ...

  6. 关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related(转)

    转至:http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html application/x-www-form-urlencoded: 窗 ...

  7. Python基础-文件的基本操作

    测试文件fansik内容如下:This is line 1This is line 2This is line 3This is line 4This is line 5This is line 6 ...

  8. PyQt4设置窗口左上角的小图标

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  9. 剑指offer 面试30题

    面试30题: 题目:包含min函数的栈 题:定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.在该栈中,调用min.push.pop的时间复杂度都是O(1) 解题思路:1)如果每次 ...

  10. LeetCode:螺旋矩阵【54】

    LeetCode:螺旋矩阵[54] 题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], ...