UVA 12101 Prime Path (素数筛+BFS)
题意:给一个四位数的素数,求通过几步变换(一次只能换一个位置的数,且变换过程中只能出现素数)变为目标四位数
分析:素数筛法+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)相关推荐
- POJ 3126 - Prime Path + Python(BFS)
()原题链接: # 原题:POJ 3126 - Prime Path | 眈眈探求 # 解题思路:https://blog.csdn.net/LYHVOYAGE/article/details/182 ...
- prime sieve 素数筛
index > Algebra > prime sieve 主要内容 本篇只收录较快的线性筛法,作用就是求 2 到 n 的所有素数,顺便得到判断数组. 单纯求素数,标记所有合数.(常用) ...
- POJ 3126 Prime Path(BFS + 素数打表)
题意:给定两个四位素数, 从一个素数到另一个素数,最少用几步,可以一次更改四位中的任意一位,但每次改变都只能是素数. 解题思路:四位数每一位情况有十种情况0-9, 四位共有40种情况, 枚举40种情况 ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- Prime Path(bfs)广度优先搜索
题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- poj3216 Prime Path(BFS)
题目传送门 Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...
- poj3126 Prime Path BFS
点击打开链接 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26158 Accepted: 143 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- Prime Number Aizu - 0009(素数筛)
题意: 给一个数n,问1~n内有多少个素数 题目: Write a program which reads an integer n and prints the number of prime nu ...
最新文章
- python什么时候用框架_Python 中三大框架各自的应用场景
- I.MX6 android 源码下载
- oracle数据库视图存放位置,oracle数据库审计
- java实现图像处理高通滤波,图像处理入门——滤波 - leo_de_macondo的个人页面 - OSCHINA - 中文开源技术交流社区...
- 通过RSS订阅、邮件转发自动同步多个Blog
- MYSQL的空间查询
- 7-25 总结 Junit 测试 和断言 /ArrayList 和LinkedList 的区别/HashCode用来存放数据.
- telink泰凌微 8266 8250 8258IO中断唤醒睡眠
- MATLAB打不开,只在任务栏闪一下就无反应了怎么办?
- 装满了自我提升的33个学习平台
- 开放平台–扫描微信二维码登录
- 杨氏集团出品:打怪小游戏
- OpenCV学习笔记(Python)———— 画轮廓
- R | 如何更新R版本及Rstudio
- 【愚公系列】2022年01月 MinIO文件存储服务器-客户端创建和桶操作(Python版)
- Python的提交表单功能
- Idea2020创建一个Servlet
- 8月份补丁更新:微软修补了121个安全漏洞
- 我国5G移动电话用户达5.61亿户,基站总量占全球超60%!到底什么是5G?一分钟给你说清楚!
- oracle SGA