先上题目

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. C++著名程序库的比较

    转载出处:http://www.acejoy.com/ace/thread-3777-1-1.html 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost3. ...

  2. CodeForces - 735D Taxes (哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  3. Linux下,安装配置Weblogic

    环境说明 系统 -- Linux RHEL5 32bit 环境 -- 局域网中在192.168.0.140(windows)通过xshell连接服务器 软件 -- 1.JDK:1.5.0_15  2. ...

  4. DCloud-MUI:代码块

    ylbtech-DCloud-MUI:代码块 1.返回顶部 1. 怎么用? html      此底色代表最小触发字符      此底色代表非必要完整触发字符 *需HBuilder7.1+,或者下载m ...

  5. NSURLSession 和 NSURLConnection 的比较

    一.NSURLConnection 1.iOS2.0出现,iOS9.0后废弃的网络请求发送方式 2.可以在初始化时确定发送同步还是异步的请求,并且可以选择执行队列. +(void)sendAsynch ...

  6. 91. ExtJS获取父子、兄弟容器元素方法

    转自:https://blog.csdn.net/u014745818/article/details/44957341 1 1.当前对象的父对象(上级对象) this.ownerCt: 2.当前对象 ...

  7. PCB MS SQL 排序应用(row_number rank dense_rank NTILE PARTITION)

    一.排序前,准备数据 --表变量 ),流程数 int) insert into @table union all union all union all union all --查看一下 select ...

  8. 3Ddungeon-------三维搜索-----偷个懒 把 亡命逃窜 的代码修改了一下 拿来用了

    题 很简单  就是给一个   三维的迷宫然后 开你起始地点 S 问你能不能到达 出口 E 能的话 需要多长时间 ? #include<stdio.h> #include<string ...

  9. java实现读取yaml文件,并获取值

    首先在项目src目录下新建一个test.yaml的文件. 代码如下: spring: application: name: cruncher datasource: driverClassName: ...

  10. C#利用ICSharpCode将远程文件打包并下载

    应用于ASP.NET MVC中 方法主体代码: public void GetFilesByOrder(string Order_ID, string IntNumber) { MemoryStrea ...