POJ 3126 Prime Path(筛法,双向搜索)
题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数。
线性筛一遍以后bfs就好。我写的双向,其实没有必要。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int maxn = 1e5;
bool isNot[maxn]; void seive(int n = maxn-)
{
int m = sqrt(n+0.5);
for(int i = ; i <= m; i++){
if(!isNot[i])
for(int j = i*i; j <= n; j += i){
isNot[j] = true;
}
}
isNot[] = isNot[] = true;
} int d[maxn];
int vis[][maxn],clk;
#define PB push_back
#define PS push
const int wei[] = {,,,}; void doubleBfs(int s,int t)
{
if(s == t) { puts(""); return; }
queue<int> Q[];
queue<int> *q1 = Q, *q2 = Q+, *nxt = Q+;
int *v1 = *vis, *v2 = vis[];
q1->PS(s); q2->PS(t);
v1[s] = ++clk; v2[t] = clk;
d[s] = ; d[t] = ;
while(q1->size() && q2->size()){
if(q1->size()>q2->size()){
swap(q1,q2); swap(v1,v2);
}
while(q1->size()){
int u = q1->front(); q1->pop();
int dig[];
for(int i = ; i < ; i++){
dig[i] = u/wei[i]%;
}
for(int i = ; i < ; i++){
int t = u - dig[i]*wei[i];
for(int j = i == ; j < ; j++){
if(j != dig[i]){
int v = t+j*wei[i];
if(!isNot[v] && v1[v] != clk){
if(v2[v] == clk) {
printf("%d\n",d[v]+d[u]+);
return;
}
d[v] = d[u]+;
nxt->PS(v);
v1[v] = clk;
}
}
}
}
}
swap(q1,nxt);
}
puts("Impossible");
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
seive();
int T; scanf("%d",&T);
while(T--){
int a,b; scanf("%d%d",&a,&b);
doubleBfs(a,b);
}
return ;
}
POJ 3126 Prime Path(筛法,双向搜索)的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- poj 3126 Prime Path(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- (简单) 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】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
随机推荐
- JAVA对象创建的过程
Java中一个实例对象被创建的过程 一.类的加载过程 首先,Jvm在执行时,遇到一个新的类时,会到内存中的方法区去找class的信息,如果找到就直接拿来用,如果没有找到,就会去将类文件加载到方法区.在 ...
- PAT L2-014【二分】
思路: 最后发现对当前列车比我大的编号的栈有没有就好了,所以开个vector存一下,然后二分一下vector找一下第一个比我大的数就好了 #include <bits/stdc++.h> ...
- cookie中存取中文字符
1.存入中文: Cookie c = new Cookie("username", java.net.URLEncoder.encode(name,"utf-8" ...
- java部分基础知识整理----百度脑图版
近期发现,通过百度脑图可以很好的归纳总结和整理知识点,本着学习和复习的目的,梳理了一下java部分的知识点,不定期更新,若有不恰之处,请指正,谢谢! 脑图链接如下:java部分基础知识整理----百度 ...
- css 实现三级联动菜单
昨天因为项目中想要把二级联动菜单改成三级联动菜单,所以我就单独写了一个tab导航栏,用纯css的方式实现的三级联动.一开始我想着可以用js实现,但是js的hover事件和mouseenter,mous ...
- oracle 重做日志
原创转载请注明出处 重做日志:记录数据库数据的变化(DDL,DML) 重做日志组:由一个或者多个完全一样的重做日志文件组成,如果一个日志组有多个日志文件,后台进程LGWR会把事务变化写到同一个日志组的 ...
- [USACO09OCT]热浪Heat Wave Dijkstra
题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...
- chromedriver对应chrom版本
chromedriver版本 支持的Chrome版本 v2.37 v64-66 v2.36 v63-65 v2.35 v62-64 v2.34 v61-63 v2.33 v60-62 v2.32 v5 ...
- MySQL服务器与MySQL57服务器区别与不同处在哪里,他们各自的领域范围,能不能同时启动服务?
安装了MySQL-5.7.18.0版本数据库,版本中包含了MySQL Workbench可视化试图工具,在服务列表栏中会有MySQL的两个服务器:如果启动第一项MySQL服务器就只能操作数据库,外界不 ...
- Uva12174
#include <bits/stdc++.h> using namespace std; ; int t; int s,n; ]; ]; ]; void init(){ memset(a ...