题意:一个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(筛法,双向搜索)的更多相关文章

  1. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  2. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  3. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  4. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. POJ - 3126 - Prime Path(BFS)

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

  6. poj 3126 Prime Path(搜索专题)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20237   Accepted: 11282 Desc ...

  7. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

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

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

  9. (简单) POJ 3126 Prime Path,BFS。

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  10. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

随机推荐

  1. vue -- 异常处理集合

    1.npm run dev 运行出错,报错如下: > webpack-dev-server --inline --progress --config build/webpack.dev.conf ...

  2. ubuntu 安装 mysql 5.7 简记

    安装: apt-get install mysql-server 会安装最新版本的 mysql ,安装时会提示输入 root 的密码 进入 mysql: mysql -u root -p 进入后会出现 ...

  3. [Xcode 实际操作]四、常用控件-(4)UILabel文本标签的自动换行

    目录:[Swift]Xcode实际操作 本文将演示标签控件的换行功能, 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class Vie ...

  4. PAT天梯赛L3-005 垃圾箱分布

    题目链接:点击打开链接 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住.所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方,同时还要保证每个居民点都在距离它一个不太远的 ...

  5. Django - 回顾(1)- 模型层的Meta选项详解

    一.模型层的Meta选项详解 Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.使用方法及参数解释如下: class Book(models.Model): nid ...

  6. jetty-env.xml

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE Configure PUBLIC &quo ...

  7. netstat命令怎么查看端口是否占用

    转自:http://www.ahlinux.com/start/cmd/527.html netstat命令是一个监控TCP IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接 ...

  8. sourceInsight4 破解笔记(完美破解)

    https://www.cnblogs.com/Napoleon-Wang/p/6706773.html 时隔好多年,sourceinsight4以迅雷不及掩耳之势的来了.与3.5相比,sourcei ...

  9. <Openssl下hash函数>

    hash函数:是不可逆的函数,它的输入可以是任意长度的字节流.它的输出是固定大小的,hash函数的作用就是给你的文件产生一个摘要,它是独一无二的. 例如:y=f(x) x代表输入  y代表输出   输 ...

  10. 《C#高效编程》读书笔记04-使用Conditional特性而不是#if条件编译

    #if/#endif语句常用来基于同一份源代码生成不同的编译结果,其中最常见的就是debug版和release版.但是这在实际应用中并不是非常友好,因为它们容易被滥用,其代码也难以理解或调试. C#为 ...