题目传送门

 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. 初识NuGet

    因为想查一查opencvsharp的东西,然后发觉这个包可以再NuGet上面可以直接下载.我也经常在很多地方都可以看到NuGet,所以我想写下来,记录下来. NuGet是一个免费的并且开源的包管理器在 ...

  2. jquery+html实现前端的上传图片预览

        就是这样的一个功能,点击加号,出现图片选择,然后选择好以后生成预览. input那么丑,UI看不惯,一定要改成加号 我就用了fa的图标,外部套一个bootstrap4中的class:borde ...

  3. React(1) --新建项目

    搭建React开发环境之前的准备工作 1.必须要安装node.js (注意:安装node.js稳定版本) 2.安装cnpm,用cnpm替代npm npm install -g cnpm --regis ...

  4. 初学Java 求最大公约数

    import java.util.Scanner; public class GreatesCommonDivisor { public static void main(String[] args) ...

  5. [CSS布局]简单的CSS三列布局

    前言 公司终于可以上外网了,近期在搞RN的东西,暂时脑子有点晕,等过段时间再来写点总结.倒是最近有个新学前端的同学经常会问一些基础知识,工作空闲写了小Demo给他看,全是很基础的知识,纯粹是顺便记录在 ...

  6. SpringBoot 快速支持国际化i18n

    学习目标 快速学会如何在工程中支持国际化语言. 快速查阅 专题阅读:<SpringBoot 布道系列> 源码下载:springboot-locale-i18n — Hey Man,Don' ...

  7. Sass Maps的函数-map-remove($map,$key)、keywords($args)

    map-remove($map,$key) map-remove($map,$key) 函数是用来删除当前 $map 中的某一个 $key,从而得到一个新的 map.其返回的值还是一个 map.他并不 ...

  8. AGC003[BCDEF]题解

    2018-12-28 有点累EF明天再写叭=v= 2018-12-29 update EF B - Simplified mahjong 可以注意到 一段连续的非0序列都可以凑出 就是显然%2=0的可 ...

  9. VUE中使用canvas做签名功能,兼容IE

    <template>         <div>           <div class="msgInput">             &l ...

  10. ceph-状态监测-脚本

    http://www.tang-lei.com/2018/06/05/ceph-%E7%8A%B6%E6%80%81%E7%9B%91%E6%B5%8B-%E8%84%9A%E6%9C%AC/ 为了能 ...