题目: 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] Shape Objects in GIF Loop Coder

    This lesson is a quick tour of the predefined shape objects in GIF Loop Coder. function onGLC(glc) { ...

  2. [TypeScript] 0.First Example

    Create a greeter.ts file: class Student { fullname : string; constructor(public firstname, public mi ...

  3. Jmail的邮件发送

    下载注册dll文件 1. dll文件下载 2.到jmail.dll所在目录,运行cmd regsvr32 目录/jmail.dll 3.c#程序中,行首引用代码 using jmail C#示例代码 ...

  4. OPTIMIZE TABLE

    INNODB 不支持 mysql> OPTIMIZE TABLE t; +--------+----------+----------+----------------------------- ...

  5. Android5.0之CoordinatorLayout的使用

    CoordinatorLayout,中文译作协调者布局,光听这名字你可能很难判断出协调者布局有什么特点,那么我们来看看下面一张图片: 由于CSDN对图片大小的要求,我只能录制一个快速播放的动画,请大家 ...

  6. Android5.0常用颜色属性说明

    在使用Eclipse的时代,我们很少去在style文件给整个应用或者Activity去设定颜色,那是因为即使设置也不会提升用户的视觉效果.但是材料设计号称让没有设计功底的人也能做出漂亮的App,那我们 ...

  7. 20160410javaweb之JDBC---DBUtils框架

    DBUtils 1.DbUtils 工具类 2.QueryRunner -- 两行代码搞定增删改查 (1)QueryRunner() --需要控制事务时,使用这组方法 int update(Conne ...

  8. SQL Server调优系列基础篇 - 索引运算总结

    前言 上几篇文章我们介绍了如何查看查询计划.常用运算符的介绍.并行运算的方式,有兴趣的可以点击查看. 本篇将分析在SQL Server中,如何利用先有索引项进行查询性能优化,通过了解这些索引项的应用方 ...

  9. (一)初识Android

    第一节:手机操作系统简介 目前的主流智能操作系统有:Android , IOS , windows mobile ; Android 开源,属于谷歌公司,市场份额较大,前景广阔: IOS 属于苹果公司 ...

  10. 基于SSM框架的简易的分页功能——包含maven项目的搭建

    新人第一次发帖,有什么不对的地方请多多指教~~ 分页这个功能经常会被使用到,我之前学习的时候找了很多资源,可都看不懂(笨死算了),最后还是在朋友帮助下做出了这个分页.我现在把我所能想到的知识 做了一个 ...