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: ...
随机推荐
- thinkphp实现短信验证注册
前言 注册时经常需要用到短信验证码,本文记录一下思路和具体实现. 短信验证平台使用云片,短信验证码的生成使用thinkphp. 思路 1.用户输入手机号,请求获取短信验证码. 2.thinkphp生成 ...
- 亲测PHpnow 安装环境
出现问题1: "C:\Windows\system32\7z.exe"' 不是 或批处理文件. 找不到 C:\Windows\system32\7z.exe------------ ...
- SDL Game Development InputHandler类的一处bug
个人十分推荐SDL Game Development 这本书,它并不是死抠SDL的api,而是一步步带着我们如何对一个游戏进行构架. 虽然我没用过游戏引擎,也基本不会写游戏,但是我认为这本书本身就是在 ...
- 如何在单元测试中测试异步函数,block回调这种
大概有四种方法: runloop 阻塞主进程等待结果 semphaore 阻塞主进程等待结果 使用XCTestExpectation 阻塞主线程等待(我用这个,xcode自带的,为啥不用) 使用第三方 ...
- 11.2 afternoon
#include<iostream> #include<cstdio> #define ll long long using namespace std; ll n,ans; ...
- PHP获取文件行数
原文出处 提供两种实现方法,但是第一种效率最好 第一种: <?php $file_path = 'test.txt'; //文件路径 此处找一个1094644行的TXT文件 test.txt $ ...
- linux 脚本编写基础(一)
1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...
- Linux命令:chmod命令
chmod命令:改变文件或目录的存取权限 #权限代号 -r 文件被读取 4 -w 文件被写入 2 -x 文件被执行 1 #权限范围 -u 文件所有者 -g 文件所有者所在组 -o 其他 -a 全部 # ...
- [LeetCode OJ] Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- SGU 199 Beautiful People(DP+二分)
时间限制:0.25s 空间限制:4M 题意: 有n个人,每个人有两个能力值,只有一个人的两个能力都小于另一个的能力值,这两个人才能共存,求能同时共存的最大人数. Solution: 显然这是一个两个关 ...