<题目链接>

题目大意:

给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四位数。

解题分析:

先打一张素数表,然后进行BFS搜索,对于每次搜索的当前数,枚举某一位与它不同的所有数,判断它是否符合条件,如果符合,就将它加入队列,继续搜索。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<math.h>
#define min 0x3f3f3f3f using namespace std; int arr[];
int vis[];
int n,m; void prim() //素数打表
{
int i,j;
arr[]=arr[]=;
for(i=;i<=;i++)
{
if(!arr[i])
{
for(j=i*;j<=;j+=i)
arr[j]=;
}
}
} int bfs(int first,int last)
{
queue <int>q;
int v,i,j,temp,vtemp,count[],N[];
memset(vis,,sizeof(vis));
memset(count,,sizeof(count));
q.push(first);
vis[first]=;
while(!q.empty())
{
v=q.front();
q.pop();
N[]=v/;
N[]=v%/;
N[]=v%/;
N[]=v%; //N[]为v的各位数 for(j=;j<;j++)
{
temp=N[j];
for(i=;i<;i++)
if(i!=temp) //枚举某一位与当前数不同的数
{
N[j]=i;
vtemp=N[]*+N[]*+N[]*+N[];
if(!vis[vtemp] && !arr[vtemp] && vtemp>) //如果枚举的这个数没有枚举过,并且是一个四位数素数
{
vis[vtemp]=; //已经走过的就标记
count[vtemp]=count[v]+; //步数加一
q.push(vtemp);
}
if(vtemp==last) return count[vtemp]; //找到了最终的数
}
N[j]=temp; //将原来的数还原,防止对下一步造成影响
}
if(v==last) return count[v]; //找到了最终的数
}
return -;
} int main()
{
int t,k;
cin>>t;
prim();
while(t--)
{
cin>>n>>m;
k=bfs(n,m);
if(k!=-)
cout<<k<<endl;
else
cout<<"Impossible"<<endl;
}
return ;
}

下面是另一份代码,与上面枚举当前四位数的每一位数不同的是,这里用的是链式前向星储存某一个四位数能够转化成的所有下一个四位数。但是不知道为什么这份代码WA了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int maxn=+; int isprime[maxn+];
int prime[maxn];
int top;
int n,m,ans; void getprime(){
top=;
memset(isprime,,sizeof(isprime));
for(int i=;i<=sqrt(maxn*1.0);i++){
if(!isprime[i]){
for(int j=i*i;j<=maxn;j+=i){
isprime[j]=;
}
}
}
for(int i=;i<=;i++){ //打表找出四位数的素数
if(!isprime[i]){
prime[++top]=i;
}
}
} const int N=;
struct EDGE{ //用链式前向星记录与当前点只有一位不同的素数,即记录当前数下一步能够转化成的数
int to;
int next;
}edge[N*N]; //因为我打了一下表,发现top的值接近1100,所以这里这样设数组的上界
int head[];
int cnt; struct node{
int loc;
int step;
node(int a=,int b=):loc(a),step(b){}
};
int vis[]; bool juge(int a,int b){ //判断这两个数是否只有某一位数不同
int fp=;
while(a){
int a1=a%;
int a2=b%;
a/=;
b/=;
if(a1==a2)fp++;
}
if(fp==)return true;
return false;
} void init(){
memset(head,-,sizeof(head));
cnt=;
} void add(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} bool bfs(){
memset(vis,,sizeof(vis));
queue<node>q;
q.push(node(n,));
while(!q.empty()){
node now=q.front();
q.pop();
if(now.loc==m){ //找到了最终要找的数
ans=now.step;
return true;
}
if(vis[now.loc])continue;
vis[now.loc]=;
for(int i=head[now.loc];i!=-;i=edge[i].next){ //枚举下一步所有满足条件的数(已经预处理过了)
int v=edge[i].to;
if(vis[v])continue;
q.push(node(v,now.step+));
}
}
return false;
} int main(){
getprime();
int t;scanf("%d",&t);
while(t--){
init();
int a,b;
scanf("%d %d",&a,&b);
for(int i=;i<=top;i++){
if(prime[i]==a)n=i;
if(prime[i]==b)m=i;
}
for(int i=n;i<=m;i++){
for(int j=n;j<=m;j++){
if(juge(prime[i],prime[j])){ //预处理,找到当前数下一步能够转化的所有数
add(i,j);
}
}
}
if(bfs()){
printf("%d\n",ans);
}
else printf("Impossible\n");
}
return ;
}

2018-08-30

POJ 3126 Prime Path【BFS】的更多相关文章

  1. poj 3126 Prime Path 【bfs】

    题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...

  2. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

  3. POJ - 3126 - Prime Path(BFS)

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

  4. (简单) POJ 3126 Prime Path,BFS。

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

  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)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  7. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  8. BFS POJ 3126 Prime Path

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

  9. 双向广搜 POJ 3126 Prime Path

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

随机推荐

  1. java-pdf转word

    注:原文来至 < java-pdf转word   > 一: java Pdf 文字 转 Word 废话不说,直接上图 很简单的用法:1.new个PDFBox对象2.调用pdfToDoc() ...

  2. Confluence 6 找到你的支持识别代码(SEN)

    你可以在下面 3 个地方找到你的 SEN 代码: 在 Confluence 中,进入  > 基本配置(General Configuration) > 许可证详细(License Deta ...

  3. python使用 HTMLTestRunner.py生成测试报告

    HTMLTestRunner.py python 2版本 下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html 使用时,先建立一个”PyDe ...

  4. if __name__ == __'main'__: 判断讲解

    """王思聪作为消费者 要吃热狗生产者 负责做热狗问题:王思聪不清楚对方会生产多少热狗 """from multiprocessing im ...

  5. Java基本语法(一)

    一.标识符 (1)标识符就是在编写程序时给类,变量,方法等起的名字 (2)标识符的命名规则:标识符由字母,数字,下划线和$组成;第一个字符不能是数字;不能与关键字重名 二.关键字 定义:也称保留字,是 ...

  6. Git使用一:git客户端安装与创建用户

    1.下载并安装Git和图形客户端TortoiseGit Git官网:https://gitforwindows.org/ TortoiseGit官网: https://tortoisegit.org/ ...

  7. AI-URL注册器

    官方文档地址:https://www.django-rest-framework.org/tutorial/quickstart/#serializers #url生成器生成四个url,就可以访问关于 ...

  8. C++ Primer 笔记——命名空间

    1.我们既可以用 using 声明整个空间,也可以声明部分名字. using namespace std; using std::cout; 2.头文件不应包含 using 声明,因为头文件会拷贝到所 ...

  9. spring cloud Eureka常见问题总结

    Spring Cloud中,Eureka常见问题总结. 指定Eureka的Environment 1 eureka.environment: 指定环境 参考文档:https://github.com/ ...

  10. Java+selenium之WebDriver模拟鼠标键盘操作(六)

    org.openqa.selenium.interactions.Actions类,主要定义了一些模拟用户的鼠标mouse,键盘keyboard操作.对于这些操作,使用 perform()方法进行执行 ...