Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9519   Accepted: 5458

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
题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素数; 思路:将四位数以内的素数筛选出来,bfs时,枚举四位数的每一位上的每一个数;
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; struct node
{
int num;
int step;
};
queue<struct node>que;
int n,m,flag;
bool p[],vis[]; //素数筛;
void prime_search()
{
memset(p,,sizeof(p));
for(int i = ; i <= ; i+=)
p[i] = ;
for(int i = ; i <= ; i++)
{
if(p[i])
{
for(int j = i+i; j <= ; j += i)
p[j] = ;
}
}
} int bfs()
{
while(!que.empty())
que.pop();
que.push((struct node){n,});
vis[n] = ;
int res,tmp,r,t,s;
while(!que.empty())
{
struct node u = que.front();
que.pop();
if(u.num == m)
return u.step; //枚举个位数
r = u.num%;
for(int k = -; k <= ; k++)
{
t = r+k;
if(t >= && t <= )
{
res = (u.num/)*+t;
if(p[res] && !vis[res])
{
que.push((struct node){res,u.step+});
vis[res] = ;
}
}
} //枚举十位数
tmp = u.num/;
r = tmp%;
s = tmp/;
for(int k = -; k <= ; k++)
{
t = r+k;
if(t >= && t <= )
{
res = (s*+t)*+u.num%;
if(p[res] && !vis[res])
{
que.push((struct node){res,u.step+});
vis[res] = ;
}
}
} //枚举百位数
int tmp = u.num/;
r = tmp%;
s = tmp/;
for(int k = -; k <= ; k++)
{
t = r+k;
if(t >= && t <= )
{
res = (s*+t)*+u.num%;
if(p[res] && !vis[res])
{
que.push((struct node){res,u.step+});
vis[res] = ;
}
}
} //枚举千位数
r = u.num/;
for(int k = -; k <= ; k++)
{
t = r+k;
if(t > && t <= )
{
res = t*+u.num%;
if(p[res] && !vis[res])
{
que.push((struct node){res,u.step+});
vis[res] = ;
}
}
}
}
}
int main()
{
int test;
prime_search();
scanf("%d",&test);
while(test--)
{
memset(vis,,sizeof(vis));
scanf("%d %d",&n,&m);
int ans = bfs();
printf("%d\n",ans);
}
return ;
}

Prime Path(素数筛选+bfs)的更多相关文章

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

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

  2. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  3. poj3126 Prime Path 广搜bfs

    题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  4. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

  5. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  6. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  7. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

  8. HDOJ(HDU) 2136 Largest prime factor(素数筛选)

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  9. Prime Path[POJ3126] [SPFA/BFS]

    描述 孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了.zydsg要做些改变,他想去和素数小姐姐约会. 所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这样 ...

随机推荐

  1. n进制转为十进制

    主程序代码 - #include <stdio.h> #include <string.h> main() { long t1; int i, n, t, t3; ]; pri ...

  2. 为什么选择我--ReactJS

    在页面中如何大面积操作DOM的话,性能肯定是一个很大的问题,然而聪明的ReactJS实现了Virtual DOM技术,这是他的亮点之一.将组件的DOM结构映射到这个Virtual DOM对象上,并且R ...

  3. DataTable和List集合互转

    /// <summary> /// 将集合转换成DataTable /// </summary> /// <param name="list"> ...

  4. Windows7添加SSD硬盘后重启卡住正在启动

    楼主办公电脑,原来只配置了一块机械硬盘,用着总很不顺心,于是说服领导给加了块SSD固态硬盘. 操作如下: 1.在PE下分区格式化新固态硬盘,将原来机械硬盘的C盘GHOST备份后还原到新固态硬盘: 2. ...

  5. 禁用windows 10自动更新

    按Win键+R键调出运行,输入“gpedit.msc”点击“确定”,调出“本地组策略编辑器”.顺序依次展开计算机配置,管理模板 ,windows组件 ,windows更新 点击右边“配置自动更新”,选 ...

  6. Deep Learning 学习随记(四)自学习和非监督特征学习

    接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...

  7. C# div、css

    目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...

  8. C++ Reference 的“三位一体”诠释

    C++ 是介于汇编语言与高级语言之间的一种“全能”语言.它的能力是其他任何基于VMA(冯-诺曼架构)计算机的高级程序设计语言无法望其项背的,而性能也只有C语言可与之伯仲. 然而长期以来,喜欢C++和憎 ...

  9. windows编程中 一些前缀区分 IDR和IDD

    IDC_:控件的ID命名前缀(Control) IDM_:菜单的ID命名前缀(Menu) IDD_:对话框的ID命名前缀(Dialog) IDR_:资源的ID命名前缀(Resource) IDS_:字 ...

  10. phpcms(3) V9 常用函数 及 代码整理(转)

    转自http://www.cnblogs.com/Braveliu/p/5103918.html 常用函数 及 常用代码 总结如下 <;?php //转换字符串或者数组的编码 str_chars ...