POJ 3126 primepath bfs
题目链接:http://poj.org/problem?id=3126
题意:1维的坐标轴,给出起点和终点,求从起点到终点变换经历的最短的步数。起点,终点和中间变换的数字都是4位,而且都是质数。
思路:普通的广搜、精神状态不佳、找了许久的bug。后来发现是prime函数和pow函数都很丧心病狂的写错了、
附 AC 代码:
// T_T 电脑突然重启什么都没有了。。没有了、
//大概4*9入口的广搜。 从初始点开始 对于每个扩展点加入队列 知道找到ed、或者尝试完所有可能的情况、 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#include <math.h>
#define maxn 100000
#include <queue>
int st, ed;
bool vis[];
int step[]; int val1[] = {, , , , , , , , , };
int val2[] = {, , , , , , , , , };
int val3[] = {, , , , , , , , , };
int val4[] = {, , , , , , , , }; queue<int>que; bool prime(int n) {
for (int i=; i<=sqrt(n); ++i) {
if (n%i == )
return false;
}
return true;
} int pow(int a, int b) {
if (b == ) return ;
for (int i=; i<b; ++i) {
a *= ;
}
return a;
} void dfs() {
memset(vis, , sizeof(vis));
for (int i=; i<; ++i) {
step[i] = maxn;
}
while(!que.empty())
que.pop();
que.push(st);
step[st] = ;
vis[st] = ; while(!que.empty()) {
int now = que.front();
que.pop();
if (now == ed) {
return;
} int temp = now;
int val[];
int cnt = ;
while (temp) {
val[cnt] = temp % ;
temp /= ;
val[cnt] *= pow(, cnt);
cnt++;
} temp = now;
temp -= val[];
for (int i=; i<; ++i) {
temp += val1[i];
if (!vis[temp] && temp % && temp <= ed) {
if (prime(temp)) {
step[temp] = step[now] + ;
vis[temp] = ;
que.push(temp);
}
}
temp -= val1[i];
} temp = now;
temp -= val[];
for (int i=; i<; ++i) {
temp += val2[i];
if (!vis[temp] && temp % ) {
if (prime(temp)) {
step[temp] = step[now] + ;
vis[temp] = ;
que.push(temp);
}
}
temp -= val2[i];
} temp = now;
temp -= val[];
for (int i=; i<; ++i) {
temp += val3[i];
if (!vis[temp] && temp % ) {
if (prime(temp)) {
step[temp] = step[now] + ;
vis[temp] = ;
que.push(temp);
}
}
temp -= val3[i];
} temp = now;
temp -= val[]; for (int i=; i<; ++i) {
temp += val4[i];
if (!vis[temp] && temp % ) {
if (prime(temp)) {
step[temp] = step[now] + ;
vis[temp] = ;
que.push(temp);
}
}
temp -= val4[i];
}
}
return;
} int main() {
int t;
cin >> t;
while(t--) {
cin >> st >> ed;
dfs();
int ans = step[ed];
if (ans == maxn) {
cout << "Impossible\n";
}
else cout << ans << endl;
}
return ;
}
POJ 3126 primepath bfs的更多相关文章
- Prime Path(POJ - 3126)【BFS+筛素数】
Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- 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]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
随机推荐
- Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...
- Python多进程编程(转)
原文:http://www.cnblogs.com/kaituorensheng/p/4445418.html 阅读目录 1. Process 2. Lock 3. Semaphore 4. Even ...
- kubernetes实战(三):k8s v1.11.1 持久化EFK安装
1.镜像下载 所有节点下载镜像 docker pull kibana: docker tag kibana: docker.elastic.co/kibana/kibana: docker pull ...
- oracle(十一) scn
SCN(System Chang Number)作为oracle中的一个重要机制,在数据恢复.Data Guard.Streams复制.RAC节点间的同步等各个功能中起着重要作用. 理解SCN的运作机 ...
- 欧盟GDPR通用数据保护条例-原文
GDPR英文原文 https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=uriserv:OJ.L_.2016.119.01.0001.01.ENG&a ...
- 存储5——逻辑卷管理LVM
1. LVM概念 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现.LVM将一个或多个硬盘的分区在逻辑上 ...
- sql server 里的文件和文件组使用
转自:https://www.cnblogs.com/woodytu/p/5821827.html 参考:https://www.sqlskills.com/blogs/paul/files-and- ...
- 跟我学Makefile(四)
使用函数:函数调用,很像变量的使用,也是以“$”来标识的,其语法如下: $(<function> <arguments>) 或是 ${<function> < ...
- t检验&z检验学习[转载]
转自:https://blog.csdn.net/m0_37777649/article/details/74937242 1.什么是T检验? T检验是假设检验的一种,又叫student t检验(St ...
- java 的==和equals的区别(二)
java 的==和equals的区别 java 的==和equals的区别 ==通常表明引用的是同一个东西(引用的地址相同),equals通常表明两个对象的内容相同(值相同) ------------ ...