<题目链接>

题目大意:

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

解题分析:

先打一张素数表,然后进行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. Swift中使用oc代码桥接设置

    1 将oc的代码拖入项目中 2 新建一个头文件 在头文件中导入你想用的oc头文件  import "****.h" 3 在设置build Setting 中搜索bird 找到 Ob ...

  2. Linux下vsftpd的安装,Java上传文件实现。

    首先我们需要查看是否已经安装vsftpd,输入命令 :vsftpd  -v.如果出现以下信息,那么就说明已经安装vsftpd 如果没有安装,那么输入命令   : yum  install vsftpd ...

  3. LeetCode(100):相同的树

    Easy! 题目描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 ...

  4. 对一个元素 同时添加单击onclick 和 双击ondblclick 触发冲突的解决

    需求说明:单击列表项内容后,吧啦吧啦,双击列表项内容后,巴拉巴拉巴拉~~~ 解决思路:卧槽 ,其实我是没思路的,当时唯一的想法就是,看个人点击鼠标的速度了,双击快一点,触发双击事件ლ(′◉❥◉`ლ), ...

  5. html 基本指令

    命令: <pre> </pre> 格式化输出 <ol></ol> 有序 HTML 列表:示例: <pre> here is outout & ...

  6. laravel 队列发送邮件

    批量处理任务的场景在我们开发中是经常使用的,比如邮件群发,消息通知,短信,秒杀等等,我们需要将这个耗时的操作放在队列中来处理,从而大幅度缩短Web请求和相应的时间.下面讲解下Laravel中队列的使用 ...

  7. Penetration testing _internal & wireless Penetration Testing

    第一部分 渗透测试步骤 ---参考资料  Ethical Hacking: The Value of Controlled Penetration Tests  下载地址  链接:https://pa ...

  8. Pychram IDE链接MySQL下更新数据的问题总结

    一.今天下午的数据库更新问题做个总结,数据更新的时候出现如下报错: Failed to retrieve routines in mysite_db.[42S02][1146] Table 'mysq ...

  9. cf965C 二分+推方程

    #include<bits/stdc++.h> using namespace std; #define ll long long ll n,k,M,D,anss; ll calc(ll ...

  10. Django中各目录文件的作用

    一般的项目结构如下(大同小异) my_site是一个项目,blog是项目下的应用之一,可以使用创建命令创建更多的应用. 最上层的django文件夹: 自己手动创建,名字随意. 第二层my_site文件 ...