先上题目

Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9259   Accepted: 5274

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   题意比较简单,就是给你两个4位的素数,问你能不能经过有限的步骤将第一个素数转化为第二个素数,其中转化的要符合一下的要求:每一次只可以改变这个四位数的某一位,首位不能为0,每一次转化以后得到的数要还是素数。如果不可以转化得到目标素数,输出Impossible。
  做法也是比较简单,先筛出10000以内的素数出来,然后枚举当前的这个素数可以转化的其他素数,然后进行bfs,当遇到目标素数的时候就输出步数;如果转化不了就会把可以转化的素数都转化了一遍,最后循环就会结束。这里为了能够判断是否能转化,需要标记它转化过哪些素数,转化过的素数如果再出现,就不需要再搜索这个数了。 上代码:
 #include <stdio.h>
#include <string.h>
#include <queue>
#include <map>
#define LL long long
#define MAX (10000+10)
using namespace std; bool f[MAX],M[MAX]; typedef struct
{
int l;
int st;
}S;
queue<S> q; void dedeal()
{
LL i,j,n;
n=MAX;
for(i=;i<=n;i++)
{
if(!f[i])
{
for(j=i*i;j<=n;j+=i) f[j]=;
}
}
} int check(int x,int y)
{
int t,i;
S d;
d.l=x;
d.st=;
memset(M,,sizeof(M));
while(!q.empty()) q.pop();
q.push(d);
M[d.l]=;
while(!q.empty())
{
d=q.front();
q.pop();
if(d.l==y) return d.st;
d.st++;
x=d.l;
t=x/*;
for(i=;i<;i++)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
t=x/*+x%;
for(i=;i<;i+=)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
t=x/*+x%;
for(i=;i<;i+=)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
t=x%;
for(i=;i<;i+=)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
}
return -;
} int main()
{
int n,c,x,y;
//freopen("data.txt","r",stdin);
dedeal();
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&x,&y);
c=check(x,y);
if(c==-) printf("Impossible\n");
else printf("%d\n",c);
}
return ;
}

3126

最新文章

  1. 使用openssl生成RSA公私密钥
  2. 【转载】jQuery Validate验证框架 + CKEditor 无法验证问题的解决方法
  3. 【新产品发布】发布STM8S 核心板
  4. Android设置Activity启动和退出时的动画
  5. IEnumerable 和 IQueryable
  6. 你需要知道的九大排序算法【Python实现】之归并排序
  7. 使用 Node.js 做 Function Test
  8. [Jobdu] 题目1385:重建二叉树
  9. hibernate它 10.many2many单向
  10. Linux命令基础2-ls命令
  11. Nowcoder | [题解-N210]牛客OI月赛2-提高组
  12. 转载&gt;&gt;C# Invoke和BeginInvoke区别和使用场景
  13. c++第十六天
  14. Angular 4 依赖注入
  15. Monkey原理初步和改良优化--Android自动化测试学习历程
  16. 【Lua】面向对象编程(二)
  17. C#如何弹出输入框
  18. 常见的Java问题
  19. ES6 Promise对象then方法链式调用
  20. Java中的阻塞和非阻塞IO包各自的优劣思考(经典)

热门文章

  1. oc30--id
  2. Uboot中支持lcd和hdmi显示不同的logo图片【转】
  3. 利用递归分割(Split)字符串
  4. 5-7 第五天 微信 JS-SDK-简介
  5. etcd数据备份与恢复验证
  6. Mock实现模拟python的对象
  7. go函数初级
  8. C#发送电子邮件代码记录
  9. android AIDL示例代码(mark下)
  10. C#关闭退出线程的几种方法