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: ...
随机推荐
- ARCGIS二维三维放大缩小
private void ULZoomPan() { ESRI.ArcGIS.SystemUI.ICommand com = new ControlsGlobeFixedZoomOutCommand( ...
- python版本简历
- HDFS的Java客户端操作代码(查看HDFS下所有的文件或目录)
1.查看HDFS下所有的文件或目录 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.h ...
- 正则表达式匹配(node.js)
参考文档如下: http://blog.csdn.net/huiguixian/article/details/6131048 http://www.91xueke.com/2013/04/05/30 ...
- 关于封装的一个小问题和TA的例子
写个小例子吧 -- 很多细节(如校验.判断等等)都略了 其实不是有意写成这样,而是很多朋友都这么写(当然里面也有点夸张的写法) 这么写其实也没什么不好,简单明了,不用动脑子,一看就很直白, 但是如果 ...
- SQL Server中的死锁
简介 死锁的本质是一种僵持状态,是多个主体对于资源的争用而导致的.理解死锁首先需要对死锁所涉及的相关观念有一个理解. 一些基础知识 要理解SQL Server中的死锁,更好的方式是通过类比从更大的面理 ...
- if....else
if....else语句是在特定的条件下成立执行的代码,在不成立的时候执行else后面的代码. 语法: if(条件) {条件成立执行}else{条件不成立执行} 下面来写一个简单的实例 以考试成绩为例 ...
- 【转】WF4.0 (基础篇)
转自:http://www.cnblogs.com/foundation/category/215023.html 作者:WXWinter —— 兰竹菊梅★春夏秋冬☆ —— wxwinter@16 ...
- 在Xcode4中给程序提供命令行参数(转)
网上xcode4的资料实在是不多,再加上xcode4相对3的改动还那么大,并且还只有英文版.我为了这个问题头痛了很久.后来终于找到了...方法如下 xcode菜单的Product->EditSc ...
- Java学习----对象间的继承
继承:子类可以使用父类非私有的成员变量和方法 public class Father { public String name; public String bloodType; private in ...