题意:给一个四位数的素数,求通过几步变换(一次只能换一个位置的数,且变换过程中只能出现素数)变为目标四位数

分析:素数筛法+BFS,BFS时更换一个数字如果符合条件就加入队列

代码:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 1000000007
#define mem(a) memset(a,0,sizeof(a))using namespace std;typedef pair <int,int> pii;
const int maxn = 10000 + 5 , inf = 0x3f3f3f3f;
bool vis[maxn];
void isPrime(){memset(vis,true,sizeof(vis));vis[1]=false;vis[2]=true;for(int i=2;i<=maxn;i++){for(int j=i*2;j<=maxn;j+=i){vis[j]=false;}}
}
struct node{int x,d;node(int xx,int dd):x(xx),d(dd){};
};
bool done[maxn];
int Start,End;
int bfs(int d){mem(done);queue<node>q;while(!q.empty())q.pop();q.push(node(Start,d));while(!q.empty()){node now = q.front();q.pop();if(now.x==End) return now.d;else{for(int i=1;i<=9;i+=2){int temp = now.x/10*10+i;if(vis[temp]&&temp!=now.x&&!done[temp]){done[temp]=true;q.push(node(temp,now.d+1));}}for(int i=0;i<=9;i++){int nn = now.x%10;int temp = now.x/100*100+i*10+nn;if(vis[temp]&&temp!=now.x&&!done[temp]){done[temp]=true;q.push(node(temp,now.d+1));}}for(int i=0;i<=9;i++){int nn = now.x%100;int temp = now.x/1000*1000+i*100+nn;if(vis[temp]&&temp!=now.x&&!done[temp]){done[temp]=true;q.push(node(temp,now.d+1));}}for(int i=1;i<=9;i++){int nn = now.x%1000;int temp = i*1000+nn;if(vis[temp]&&temp!=now.x&&!done[temp]){done[temp]=true;q.push(node(temp,now.d+1));}}}}return -1;
}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int T;scanf("%d",&T);isPrime();while(T--){cin>>Start>>End;int ans = bfs(0);if(ans==-1) cout<<"Impossible"<<endl;else cout<<ans<<endl;}
}

转载于:https://www.cnblogs.com/seven7777777/p/10278713.html

UVA 12101 Prime Path (素数筛+BFS)相关推荐

  1. POJ 3126 - Prime Path + Python(BFS)

    ()原题链接: # 原题:POJ 3126 - Prime Path | 眈眈探求 # 解题思路:https://blog.csdn.net/LYHVOYAGE/article/details/182 ...

  2. prime sieve 素数筛

    index > Algebra > prime sieve 主要内容 本篇只收录较快的线性筛法,作用就是求 2 到 n 的所有素数,顺便得到判断数组. 单纯求素数,标记所有合数.(常用) ...

  3. POJ 3126 Prime Path(BFS + 素数打表)

    题意:给定两个四位素数, 从一个素数到另一个素数,最少用几步,可以一次更改四位中的任意一位,但每次改变都只能是素数. 解题思路:四位数每一位情况有十种情况0-9, 四位共有40种情况, 枚举40种情况 ...

  4. POJ - 3126 - Prime Path(BFS)

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

  5. Prime Path(bfs)广度优先搜索

    题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  6. poj3216 Prime Path(BFS)

    题目传送门  Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...

  7. poj3126 Prime Path BFS

    点击打开链接 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26158   Accepted: 143 ...

  8. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  9. Prime Number Aizu - 0009(素数筛)

    题意: 给一个数n,问1~n内有多少个素数 题目: Write a program which reads an integer n and prints the number of prime nu ...

最新文章

  1. CentOS6.9中搭建FTP服务器
  2. nginx: [error] invalid PID number in /run/nginx.pid
  3. php怎么实现md5加密,php如何进行md5加密
  4. 在Java SE中使用Hibernate Bean Validator
  5. POJ 1163 The Triangle
  6. 带你了解Python炫酷的颜色输出与进度条打印
  7. RabbitMQ学习之spring-amqp的重要类的认识
  8. 关于scanf 函数,你很少了解的“秘密”
  9. paip.消除 Java 的冗长try/catch/finally
  10. java 实例化是什么及五种实例化方法
  11. Glide用法总结--缓存与下载方法
  12. ansi_up---实现后端实时日志(带颜色)前端展示
  13. 警惕新骗术:虚假二维码生成器盗取 4.6 万美元!
  14. 年度榜单!我整理了2020年优质的Python文章和资源。
  15. redis 五大数据类型
  16. JAVA---冒泡排序
  17. 如何利用OMIM数据库获取肿瘤相关所有突变基因?
  18. iPad该如何运行Python代码?
  19. Android7.0分屏功能
  20. 就决定是你啦!苏菲婆5! —— 谈谈我对Surface Pro 5的使用体验以及各种骚操作

热门文章

  1. 如何在IIS上配置多个网站,及网站发布配置讲解
  2. ASP.NET企业开发框架IsLine FrameWork系列之七--AppLogProvider日志框架(上)
  3. 用RDLC报表(一)
  4. mysql 200列_Mysql数据表基本
  5. 利用Python进行「基金投资组合优化」(一)
  6. 机器学习——贝叶斯分类器
  7. OpenCV--Mat类相关操作
  8. leetcode50:Pow(x, n)详解——pygo
  9. 多项式的链式存储方案
  10. python loc iloc_【python】python vs Excel ( loc与iloc的使用)