POJ 3126 Prime Path 素数筛,bfs
题目: 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的更多相关文章
- 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]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- 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,数据有点弱 */ /**** ...
- Prime Path素数筛与BFS动态规划
埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 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+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
随机推荐
- 为Windows 7的winsxs目录瘦身,谨慎。
刚使用Win7 系统不久,前段时间在清理系统垃圾时发现,win7系统的windows文件夹下的winsxs 文件夹占用空间很大,想清理之,却提示无权限无法清理.随即在网上查了个到底,原来winsxs是 ...
- Java多线程-工具篇-BlockingQueue(转)
前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列 类,为我们快速搭建高质量的多线程程序带来极大的 ...
- Linux 释放cached内存
使用free -m 查看系统使用的内存情况: # free -m total used free shared buffers ...
- TCP/IP协议原理与应用笔记14:电路交换 和 分组交换
1. 电路交换: (1)建立连接 (2)数据传输 (3)拆除连接 2. 分组交换 (1)数据报: 根据网络的特性,将数据报分成不同大小的部分,经过不同网路传递到相同的目的地.如下: 这里A--X 和 ...
- uedoc 源码解析
思路分析 node 包使用 1. JSON5 2. art-template 3.
- Python开发笔记之正则表达式的使用
查找正则表达式 import re re_txt = re.compile(r'(\d)*.txt') m = re_txt.search(src) if not m == None: m.group ...
- magento addFieldToFilter()方法常用的过滤条件
记录一下Magento模型集合Model Collection中addFieldToFilter()方法常用的过滤条件.以下参数也同样适用于产品实体的addAttributeToFilter()方法. ...
- document.all的用法详解
all[] 已经被 Document 接口的标准的 getElementByid() 方法和 getElementsByTagName() 方法以及 Document 对象的 getElementsB ...
- Hyper-V Windows 8.1 & Windows Server 2012 R2 Q&A
从Windows8开始,x64位系统自带Hyper-V功能,很多开发者和专业用户往往希望利用的Microsoft提供的这一免费功能,但是微软在这方面并不是最佳. 主要写几个大家经常遇到的问题. Win ...
- EDMX更新实体后出现键值映射问题
近期做项目的EF改版时,在DB(ORACLE)中的表里添加一个新的PK,去除原有的PK. 在DB已添加完成操作,但这时在EDMX里进行从DB更新到EF里,更新完成后就发生如下错误提示: Error 6 ...