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 ...
随机推荐
- Typography 文字排版
标签的语义 1. 含语义的标签 2. 不含语义, 但是具有样式的class <h1></h1> <p class="h1"></p> ...
- 使用imp命令和exp命令对oracle数据库进行导入导出操作
命令说明 imp命令和exp命令需要在cmd命令窗口下执行,并且需要在系统环境变量中配置imp,exp命令所在目录 导出命令 --全库导出 exp system/systempassword@orcl ...
- mysql设置最大连接数量
mysql>mysql -uuser -ppassword(命令行登录MySQL) mysql>show variables like 'max_connections';(查可以看当前的 ...
- ie9下网页设计兼容模式
个人实践使用:ie9下使用低版本ie兼容模式,在网站第一个页面的<head>标签后使用<meta http-equiv="X-UA-Compatible" con ...
- 关于after和before
你可曾'百度一下'? 在以前的很多时候,当我断网了,或者网络出了莫名其妙的问题时,我总是第一个输入它的网址.它不仅仅是一个搜索引擎.它还是我检验网络的唯一标准(手动滑稽). CSS中的after和be ...
- A^B mod (大数运算)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ; ; cha ...
- LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...
- new 和 delete 用法
1. 这两个其实是 C++ 语言标准库的库函数,原型分别如下: void *operator new(size_t); //allocate an object void *operator dele ...
- Spark Mllib里相似度度量(基于余弦相似度计算不同用户之间相似性)(图文详解)
不多说,直接上干货! 常见的推荐算法 1.基于关系规则的推荐 2.基于内容的推荐 3.人口统计式的推荐 4.协调过滤式的推荐 协调过滤算法,是一种基于群体用户或者物品的典型推荐算法,也是目前常用的推荐 ...
- HBase 相关API操练(一):Shell操作
HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”. HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建. ...