<题目链接>

题目大意:

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

解题分析:

先打一张素数表,然后进行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. Centos7 设置静态IP地址

    一:  修改网卡配置文件(操作前先备份一下该文件),/etc/sysconfig/network-scripts/ 具体操作如下: 1:进入修改目录 [root@localhost ~]# clear ...

  2. Confluence 6 启用远程 API

    XML-RPC 和 SOAP 远程 API 从 Confluence 5.5 开始已经废弃了.我们推荐你使用完全支持的Confluence Server REST API. 希望启用 XML-RPC ...

  3. Confluence 6 增加和减少你许可证的用户数

    增加你许可证的用户数 如果你使用的用户数超过你许可证允许的用户数的话,你的 Confluence 实例将会变成为只读. 只读的意思是没有用户可以创建和编辑内容,直到你的用户数满足你的许可证需求为止. ...

  4. vue阿里上传图片报400错误

    首先我用vue上传阿里图片用的是分片上传,分片上传由于一片是以100kb为起始的,所以当图片大小小于100kb的时候不分片,可以正常上传,当大于100kb的时候,会报400错误如下 One or mo ...

  5. bat如何实现自动创建文件夹(以当前时间命名)

    先比较直接的查看当前的日期和时间:(或者cmd中直接输入date,time查看) @echo off color 0a set dt=%date%%time% echo %dt% pause 1.使用 ...

  6. flask中的wtforms使用

    一.简单介绍flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装: pip3 install wtforms 二.简单使用wtfo ...

  7. 页面初始化的js函数要放在最最最最最最最前边!否则没效果

    简单说一下这个情况,html的页面的各部分都是动态渲染的,所以头部的某些个样式调用函数需要在页面初始化的时候被加载,这个我也是知道的,结果后边代码敲着敲着,就把这个事儿给忘了,然后启动项目的时候,页面 ...

  8. arm指令1

    .section .text.writeFUNCTION(write) ldr r12, =__NR_write swi #0 bx lr LDR: LDR 的两种用法 1)LDR pc, =MyHa ...

  9. bzoj 1222

    比较简单的背包dp,设计状态f[i][j]表示到了前i个物品,第一台机器加工时间为j,第二台机器加工所用的最小时间,然后背包转移即可 本题卡空间,需要滚动数组优化 本题卡时间,稍微卡下常就行 #inc ...

  10. Python笔记记录

    python2和python3的不同: Unicode(统一码.万国码),在3里面可以直接写中文了. python2里rae_input与python3中的input效果一样 在计算机内存中,统一用U ...