先上题目

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

POJ - 3126 - Prime的更多相关文章

  1. 双向广搜 POJ 3126 Prime Path

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

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

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

  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)

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

  6. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  7. POJ 3126 Prime Path 素数筛,bfs

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

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

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

  9. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  10. POJ 3126 Prime Path 广度优先搜索 难度:0

    http://poj.org/problem?id=3126 搜索的时候注意 1:首位不能有0 2:可以暂时有没有出现在目标数中的数字 #include <cstdio> #include ...

随机推荐

  1. Modern Qt Development: The Top 10 Tools You Should Be Using

    Published Friday October 12th, 2018Leave a comment Posted in Biz Circuit & Dev Loop, C++, QtCrea ...

  2. python统计ES存储空间占用的代码

    import os from os.path import join, getsize def get_dir_size(dir, suffix_filter=None): size = 0L if ...

  3. 音频格式opus

    人耳能听到自然界的声音是20HZ-20KHZ,一般高保真音质采样率只有达到最高采样率的2倍以上即可,平时电话采样率8KHZ,CD音质的采样率44.1KHZ. IBM 的Watson的音频转文字接口支持 ...

  4. IntelliJ IDEA :解决idea导入项目爆红

    转:https://my.oschina.net/LevelCoder/blog/1802158 我们在导入一个新的项目到idea的时候,项目明明没有报错,但是会出现出了父包属于正常颜色外,其子包都会 ...

  5. 第3课 把文件存入Git文档库

    3-1  排除不需要加入文档库的文件 Git追踪文件的方式.Git会将文件和文件夹分成以下三类: 1.   被追踪的(tracked): 2.   忽略的(ignored): 3.   不被追踪的(u ...

  6. gulp安装成功但是无法使用

    gulp安装正常,但是查看gulp -v和使用gulp的时候报错, 原因:缺少环境变量或环境变量错误. 查找环境变量的方法:在dos下输入npm config get prefix就会显示一个地址,这 ...

  7. VScode常用插件(持续更新)

  8. Oracle 12.2.0.1 RAC for rhel 7.X 数据库安装(节点1执行root.sh失败)

    说明: 最开始是用的rehat7.2安装12.2.0.1,后面安装GI节点一执行root.sh脚本失败,排查原因,最开始以为是操作系统的问题,换成rehat7.6,同样的出现问题,经过一番折腾,后面通 ...

  9. 5.30获取openid和createTime--mybatis自动生成接口和映射【这里需要自定义】

    自定义sql获取数据:        dao:            前提是反向成了代码:                A : 接口PhoneModelMapper extends IBaseMap ...

  10. WEB笔记-让HTML5向下兼容的策略

    //给新标签增加块级元素声明 article,aside,dialog,figure,fotter,header,legend,nav,section{display:block} //添加css兹瓷 ...