题目: http://poj.org/problem?id=3126

困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行。人品啊。

 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; bool prime[];
int vis[];
int s, t; void prime_init()
{
memset(prime, , sizeof(prime));
prime[] = ;
for(int i = ; i < ; i += )
prime[i] = ;
for(int i = ; i < ; i += )
{
if(prime[i])
{
for(int j = i*i; j < ; j += i+i)
prime[j] = ;
}
}
} queue<int>q;
void bfs()
{
while(!q.empty())q.pop();
memset(vis, , sizeof(vis));
q.push(s);
vis[s] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
if(u == t)
{
printf("%d\n", vis[u]-);
return;
}
int a = u % ;
int b = u % - a;
int c = u % - a - b;
int d = u - u % ;
for(int i = u-a; i <= u-a+; i++)
{
if(prime[i] && !vis[i])
{
q.push(i);
vis[i] = vis[u] + ;
}
}
for(int i = u-b; i <= u-b+; i += )
{
if(prime[i] && !vis[i])
{
q.push(i);
vis[i] = vis[u] + ;
}
}
for(int i = u-c; i <= u-c+; i += )
{
if(prime[i] && !vis[i])
{
q.push(i);
vis[i] = vis[u] + ;
}
}
for(int i = u-d+; i <= u-b+; i += )
{
if(prime[i] && !vis[i])
{
q.push(i);
vis[i] = vis[u] + ;
}
}
}
} int main()
{
prime_init();
int n;
scanf("%d", &n);
while(n--)
{
scanf("%d %d", &s, &t);
bfs();
}
return ;
}

POJ 3126 Prime Path 素数筛,bfs的更多相关文章

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

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

  2. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

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

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

  4. BFS POJ 3126 Prime Path

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

  5. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  6. 双向广搜 POJ 3126 Prime Path

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

  7. poj 3126 Prime Path bfs

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

  8. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

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

  9. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

随机推荐

  1. [GIF] GIF Loop Coder - Interpolation

    This video discusses the default interpolation in GIF Loop Coder, and four distinct ways to change t ...

  2. [React] React Fundamentals: First Component

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. Linux crontab 命令格式与具体样例

    基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...

  4. java class 文件解析

    参考下面两个文章对一个class文件进行解析: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.6 htt ...

  5. 如何在mysql命令窗口获取到程序正在执行的sql语句

    步骤: 1.进入mysql的命令窗口: 2.运行use information_schema; 3.运行select * from PROCESSLIST where info is not null ...

  6. Python 基础【第一篇】环境部署

    一.Windows基础环境配置部署 1.1.下载python安装程序 下载地址:https://www.python.org/ftp/python/3.4.1/python-3.4.1.msi 1.2 ...

  7. verilog语法:进程名

    always @ (posedge clk, negedge rstn)begin:State_flipflops if(!rstn) y<=S1; else y<=Y;end请问这段程序 ...

  8. js基础1

    一.JavaScript 不同于Java 有三部分组成 核心(ECMAScript) 文档对象模型(DOM) 浏览器对象模型(BOM) 二.var 是定义数据前加的前缀   三.弹出 alert( ) ...

  9. 安卓百度地图开发so文件引用失败问题研究

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 # 问题 首先,下面的问题基本都是在Android Studio下使用不当导致,eclipse是百 ...

  10. 自定义控件学习 Day44

    自定义控件学习 Day44 onMeasure 测量控件的宽高. onLayout 设置位置 onDarw 绘制控件 问题堆栈 1. 事件监听传递 最外层获取到控件,根据事件事件传递机制,返回值fal ...