题目传送门

 Prime Path

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
题意:给你四位数字a,b;每一不只能改变一位数字,且新的数字只能是素数,
要你输出最小步数 题解:bfs,每次向下遍历40个方向
代码:
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
struct niu
{
int prime,step;
niu(){}
niu(int pr,int st)
{
prime=pr,step=st;
}
};
int a,b;
int ans=INF;
bool check(int a)
{
for(int i=;i*i<=a;i++)
if(a%i==)return false;
return a!=;
}
queue<niu>q;
bool vis[];
int bfs()
{
memset(vis,false,sizeof(vis));
while(q.size())q.pop();
q.push(niu(a,));
vis[a]=true;
while(q.size())
{
niu tmp=q.front();q.pop();//cout<<tmp.prime<<tmp.step<<endl;
if(tmp.prime==b)
{
ans=min(ans,tmp.step);
}
int cnt=tmp.prime%;
int cur=(tmp.prime/)%;
for(int i=;i<=;i++)
{
int nx=(tmp.prime/)*+i;
if(check(nx)&&!vis[nx])
{
vis[nx]=true;
q.push(niu(nx,tmp.step+));
}
int ny=(tmp.prime/)*+i*+cnt;
if(check(ny)&&!vis[ny])
{
vis[ny]=true;
q.push(niu(ny,tmp.step+));
}
int nz=(tmp.prime/)*+i*+cur*+cnt;
if(check(nz)&&!vis[nz])
{
vis[nz]=true;
q.push(niu(nz,tmp.step+));
}
if(i==)continue;
int nn=(tmp.prime%)+i*;//cout<<nn<<endl;
if(check(nn)&&!vis[nn])
{
vis[nn]=true;
q.push(niu(nn,tmp.step+));
}
}
}
return ans==INF?-:ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
ans=INF;//注意这里的ans要初始化
cin>>a>>b;
if(bfs()==-)
cout<<"Impossible"<<endl;
else cout<<bfs()<<endl;
}
return ;


poj3216 Prime Path(BFS)的更多相关文章

  1. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  3. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  4. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  5. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  7. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  8. POJ - 3126 - Prime Path(BFS)

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

  9. POJ-3126-Prime Path(BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27852   Accepted: 15204 Desc ...

随机推荐

  1. 在css里如何控制表单中文本的value内的文字的位置,比方说让它向右移动2px

    方法1:比较简单的方法是将文本放到一个容器中(div,span,a等等)然后给容器设置样式,通过控制容器的位置来达到控制字体位置.(margin-left:10px; margin-top:15px; ...

  2. MySQL--11 备份的原因

    目录 一.备份的原因 二.备份的类型 三.备份的方式 四.备份策略 五.备份工具 六.企业故障恢复案例 1.模拟环境 2.模拟恢复数据过程: 一.备份的原因 运维工作的核心简单概括就两件事: 1)第一 ...

  3. Class.forName的作用

    在java语言中,任何类只有被装载到JVM上才能运行.Class.forName()方法的作用就是把类加载到JVM中,它会返回一个与带有给定字符串明的类或者接口相关联的Class对象,并且JVM会加载 ...

  4. grep正则 以.o结尾的文件

    ls -l | grep *.o 查不出任何东西 . 代表一定有一个任意字符 * 重复零个到无穷多个前一个字符(所以需要前面有字符) 所以应该是 ls -l | grep '.*\.o' .*表示零个 ...

  5. canvas 绘制二次贝塞尔曲线

    代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  6. AGC002[BCDEF]题解

    F是计数于是就做(kan ti jie)了= = B - Box and Ball 模拟一下 每个盒子开一个d表示有的球数 可能存在红球的打个标记 传递一下就行了 #include<cstdio ...

  7. linux根据进程名获取PID

    经常需要Kill多个进程,这些进程包含共同的关键字,可以用一条命令Kill掉它们. ps aux | grep "common" |grep -v grep| cut -c 9-1 ...

  8. java获取当月日期 和 周末

    /** * java获取 当月所有的日期集合 * @return */public static List<Date> getDayListOfMonth() { List list = ...

  9. CSS text-indent 属性

    text-indent 属性首行文本缩进,有点像padding-left的效果.

  10. C++ 概率算法 利用蒙特卡罗算法计算圆周率

    概率算法大致可分为4种形式: 数值概率算法: 蒙特卡罗算法: 拉斯维加斯算法: 舍伍德算法: 计算蒙特卡罗概率的算法实现: #include "stdio.h" #include ...