POJ 3126 Prime Path【BFS】
<题目链接>
题目大意:
给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四位数。
解题分析:
先打一张素数表,然后进行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】的更多相关文章
- poj 3126 Prime Path 【bfs】
题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- (简单) POJ 3126 Prime Path,BFS。
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
随机推荐
- Confluence 6 选择一个外部数据库
注意: 选择一个合适的数据库通常需要花费很多时间.同时 Confluence 自带的 XML 数据备份和恢复功能通常也不适合合并和备份有大量数据的数据库.如果你想在系统运行后进行数据合并,你通常需要使 ...
- caffe2安装
参考http://blog.csdn.net/Andy965/article/details/70808909?locationNum=5&fps=1 建议将make 和make instal ...
- jQuery之jQuery扩展和事件
一.jQuery事件 常用事件 blur([[data],fn]) 失去焦点 focus([[data],fn]) 获取焦点( 搜索框例子) change([[data],fn]) 当select下拉 ...
- wireless Penetration Testing & Honeypot and Mis-Association attacks
重新记一遍 ,在捕获握手数据包的时候不容易获取,所以使用ARP请求.使用自己的无线网卡的地址发送请求,会容易使得无线开启端掉线,迫使重新连接. 1.使用命令 aireplay-ng -3 -b a ...
- Sequence Number
1570: Sequence Number 时间限制: 1 Sec 内存限制: 1280 MB 题目描述 In Linear algebra, we have learned the definit ...
- 升级centos6.8内核
1.查看默认版本:uname -r 2.更新nss 3.安装elrepo的yum源,升级内核需要使用elrepo的yum源,在安装yum源之前还需要我们导入elrepo的key rpm --impor ...
- 全局安装的 webpack运行时 报错 Error: Cannot find module 'webpack' ......
全局安装的webpack 安装指令如下 cnpm install wepack -save-dev -g 但是 在我的项目空间运行webpack指令的时候 会报如下错误 为了方便抓取{ Error ...
- 四.idea本地调试hadoop程序
目录: 目录见文章1 1.先上案例代码 WordCount.java: import java.io.IOException; import java.util.StringTokenizer; im ...
- MediatR
1.MediatR是什么? 微软官方eshopOnContainer开源项目中使用到了该工具, mediatR 是一种中介工具,解耦了消息处理器和消息之间耦合的类库,支持跨平台 .net Standa ...
- sed 简单修改配置文件ip地址
sed -i 's/old ip/new ip/g' file.txt