POJ3126 Prime Path(BFS)
题目链接。
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <ctype.h> using namespace std; const int maxn = ; struct node {
int x, s;
}; int end;
bool vis[maxn], prime[maxn]; int BFS(int s) { if(s == end) return ; queue<node> Q;
memset(vis, , sizeof(vis)); vis[s] = true;
Q.push((node){s, }); while(!Q.empty()) {
node e = Q.front(); Q.pop();
for(int i=; i<=; i *= ) {
for(int j=; j<; j++) {
if(i == && j == ) continue;
int t = e.x%i+j*i+e.x/(i*)*(i*);
if(t == end) return e.s+;
if(prime[t]) {
if(vis[t]) continue;
vis[t] = true;
Q.push((node){t, e.s+});
}
}
}
}
return -;
} int main() {
int T, s;
scanf("%d", &T); memset(prime, true, sizeof(prime));
prime[] = prime[] = false; for(int i=; i*i<maxn; i++) {
if(prime[i])
for(int j=i*i; j<maxn; j += i) {
prime[j] = false;
}
} while(T--) {
scanf("%d%d", &s, &end);
printf("%d\n", BFS(s));
} return ;
}
POJ3126 Prime Path(BFS)的更多相关文章
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj3126 Prime Path(c语言)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...
- [POJ]P3126 Prime Path[BFS]
[POJ]P3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35230 Accepted: ...
随机推荐
- media query
accepted Another useful media feature is device-aspect-ratio. Note that the iPhone 5 does not have a ...
- Quartz Features
Runtime Environments Quartz can run embedded within another free standing application Quartz can be ...
- 使用Spring简化JDBC操作数据库
Spring的开发初衷是为了减轻企业级开发的复杂度,其对数据库访问的支持亦如此,使用Spring访问数据库能带来以下好处: 1.1 简化代码 使用原生的JDBC访问数据库,一般总是要执行以下步 ...
- C#和asp.net中链接数据库中 参数的几种传递方法
#region 参数传递方法第一种 //参数设置方法(第一种) //SqlParameter sp = new SqlParameter("@Name", str_Name); / ...
- oracle 数据库关闭的的几种方式总结
shutdown的几种方式,shutdown abort的一些弊端有哪些 1.shutdown normal 正常方式关闭数据库. 2.shutdown immediate ...
- mem 族函数的实现
1.void * memcpy ( void * dest, const void * src, size_t num ); 头文件:#include <string.h>memcpy() ...
- 仿照淘宝首页做的一个高度伪对齐demo
功能就是当右边高度没有左边高的情况下做的一些处理,由于本人技术有限,不兼容所有浏览器, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...
- 如果数据为null,则转成数据库可识别的DBNULL.Value
// <summary> /// 如果数据为null,则转成数据库可识别的DBNULL.Value /// </summary> /// <param name=&quo ...
- javascript 函数声明问题
(function(){ //运行正常 test1(); function test1() { console.log('123'); }; })() (function(){ //出错,test2未 ...
- Winform控件Enable=false显示优化
在B/S开发中(ASP.NET),往往可以css样式表来让页面控件更加美观,但是在C/S中(Winform)里面,我们则需要通过其他取巧的 方式来实现.例如:当你因为某个需求需要将控件设置为Reado ...