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 ...
随机推荐
- ORA-00257 archiver error. 错误的处理方法
archive log 日志已满 方法/步骤 1 SecureCRT登录服务器,切换用户oracle,连接oracle [root@userbeta~]# su - oracle [oracle@us ...
- web前端识别文字转语音
const msg = new SpeechSynthesisUtterance("hello world"); window.speechSynthesis.speak(msg) ...
- java多线程快速入门(十八)
Lock锁是JDK1.5之后推出的并发包里面的关键字(注意捕获异常,释放锁) Lock与synchronized的区别 Lock锁可以人为的释放锁(相当于汽车中的手动挡) synchronized当线 ...
- LoadRunner JAVA Vuser接口测试
注:JDK只支持1.6 1.创建工程Test2.写个经典的HelloWorld类.3.Runas--->Java Application运行下4.将工程下的整个com包拷贝到loadrunner ...
- VisualSVN Server如何安装和使用
首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http://subversion.apache.org/packages.html 这是二进制文件 ...
- 论文阅读笔记六:FCN:Fully Convolutional Networks for Semantic Segmentation(CVPR2015)
今天来看一看一个比较经典的语义分割网络,那就是FCN,全称如题,原英文论文网址:https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn ...
- 下载中间件--随机IP代理以及随机User_Agent
下载中间件随机IP代理以及随机User_Agent 1.在settings.py中设置开启代理功能 # 设置下载中间件 DOWNLOADER_MIDDLEWARES = { # 随机的 User-Ag ...
- haproxy admin_stats端口启动错误解决
/var/log/message里的错误消息大概如下: Feb 13 09:32:50 cluster-node2 haproxy-systemd-wrapper: [ALERT] 043/09325 ...
- 使用JDBC连接ElasticSearch6.3(ElasticSearch SQL JDBC)
使用JDBC连接ElasticSearch6.3(ElasticSearch SQL JDBC) https://blog.csdn.net/scgaliguodong123_/article/det ...
- 去除ArrayList集合中的重复自定义对象元素
要求去除ArrayList集合中重复的Student的对象(什么叫重复,所有属性值都相同叫做重复). 思路: 1.创建一个新集合 2.遍历旧集合中的每一个元素,去新集合中找这个元素,如果这个元素不存在 ...