Prime Path

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 26475 Accepted: 14555

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

Source

Northwestern Europe 2006

【题意】:给你两个素数,s和e,要求在最少次数从s转变到e,而且中间的数字也得是素数,并且变化前后相邻的两个数只有一位不同

【分析】:是一道在素数集合上的搜索题目,并且一定是四位数的素数。题目要求所经过的路径最短,自然是BFS。关键是BFS构造的问题,有一个很简单的方法,就是把BFS想成一棵树,每个节点表示一个状态,这个节点的子节点则表示由该状态可到达的状态,对于一个四位数,比如1033,它的千位可以从1-9取值,百位和十位可以从0-9取值,而个位只能取奇数位,因为个位为偶数的数肯定不是素数,然后就是让所有可以到达的状态进队列,并且给他们标记,防止下次再次进入,至于判断素数,可以在O(1)内完成,所以整个程序的效率是比较高的。

最后如果弹出的数就是想要到达的数,输出步数

Tips:一般这种要求步数的BFS都是需要使用结构体,每一个节点记录自己的步数

【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const int N = 1e4+10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[][3]={ {0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0} };
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct node
{
int p[4],step; //數組代表4個數位的取值
}st,ed; int vis[15000];
int t,n,s,e;
int a,b,c,d; bool prime( int x )
{
for(int i=2; i<=sqrt(x); i++)
if(x%i==0)
return false;
return true;
} int cal(int *a)
{
return a[0]*1000 + a[1]*100 + a[2]*10 + a[3];
} void bfs()
{
memset(vis,false,sizeof(vis));
queue<node>q;
while(!q.empty()) q.pop(); st.p[0]=s/1000; st.p[1]=s/100%10; st.p[2]=s/10%10; st.p[3]=s%10;
st.step=0;
q.push(st);
vis[cal(st.p)] = 1; while(!q.empty())
{
st = q.front();
q.pop();
if(cal(st.p)==e)
{
printf("%d\n",st.step);
return ;
}
for(int i=0;i<4;i++)//枚舉數位
{ for(int j=0; j<10; j++) //枚舉數位上值
{
ed=st;//
if(i==0 && j==0) continue; //首位不能为0
ed.p[i]=j; //给某位赋值
if(!vis[cal(ed.p)] && prime(cal(ed.p)))
{
vis[cal(ed.p)] = 1;
ed.step = st.step + 1;
q.push(ed);
}
} }
}
printf("Impossible\n");
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&e);
bfs();
}
}
/*
3
1033 8179
1373 8017
1033 1033 6
7
0
*/

POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】的更多相关文章

  1. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  2. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  3. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  4. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

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

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

  6. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

  7. POJ - 3126 - Prime Path(BFS)

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

  8. POJ 3126 Prime Path

    给定两个四位素数a  b,要求把a变换到b 变换的过程要保证  每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数  与  前一步得到的素数  只能有一个位不同,而且每步得到的素数都不能 ...

  9. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

随机推荐

  1. BZOJ4446 SCOI2015小凸玩密室(树形dp)

    设f[i][j]为由根进入遍历完i子树,最后一个到达的点是j时的最小代价,g[i][j]为由子树内任意一点开始遍历完i子树,最后一个到达的点是j时的最小代价,因为是一棵完全二叉树,状态数量是nlogn ...

  2. 为什么比IPVS的WRR要好?

    动机 五一临近,四月也接近尾声,五一节乃小长假的最后一天.今天是最后一天工作日,竟然感冒了,半夜里翻来覆去无法安睡,加上窗外大飞机屋里小飞机(也就是蚊子)的骚扰,实在是必须起来做点有意义的事了!    ...

  3. 【模拟赛·polyline】

    Input file: polyline.in Output file: polyline.out Time limit: 1s Memory limit: 128M 有若⼲个类似于下⾯的函数: 定义 ...

  4. 【BZOJ 2744 朋友圈】

    Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1570  Solved: 532[Submit][Status][Discuss] Descripti ...

  5. 【BZOJ 4832】 [Lydsy2017年4月月赛] 抵制克苏恩 期望概率dp

    打记录的题打多了,忘了用开维记录信息了......我们用f[i][j][l][k]表示已经完成了i次攻击,随从3血剩j个,2血剩l个,1血剩k个,这样我们求出每个状态的概率,从而求出他们对答案的贡献并 ...

  6. 一个JavaScript日期格式化扩展函数

    我们都知道在Java和PHP语言中,有专门用于格式化日期对象的类和函数,例如Java中的DateFormat等等,通过这些类和函数,我们可以方便的将一个日期对象按照格式的要求输出为字符串,例如对于同一 ...

  7. rest与restful

      知乎上面摘抄的,感觉不错,分享下:  https://www.zhihu.com/question/28557115 1. REST描述的是在网络中client和server的一种交互形式:RES ...

  8. C#中的弱引用(WeakReference)

    我们平常用的都是对象的强引用,如果有强引用存在,GC是不会回收对象的.我们能不能同时保持对对象的引用,而又可以让GC需要的时候回收这个对象呢?.NET中提供了WeakReference来实现.弱引用可 ...

  9. elementary os 5配置

    打开终端,执行以下步骤: 1.更新软件源 sudo apt update 2.安装add-apt-repository命令所在的软件包 sudo apt install software-proper ...

  10. [ Openstack ] Openstack-Mitaka 高可用之 环境初始化

    目录 Openstack-Mitaka 高可用之 概述    Openstack-Mitaka 高可用之 环境初始化    Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...