题目链接: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的更多相关文章

  1. Prime Path(POJ - 3126)【BFS+筛素数】

    Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...

  2. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  3. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  4. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  5. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  6. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  7. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  8. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

随机推荐

  1. xdotool xdotool模拟击键和鼠标移动--CutyCapt是一个截图工具,xvfb-run

    最近在做一个生成网站缩略图的功能,从网上查到相关资料,现与大家分享,xvfb这个软件,安装上之后一条命令就能执行此操作.很容易的就生成了自己想要的缩略图. xvfb-run -运行在一个虚拟的X服务器 ...

  2. 程序入口函数和glibc及C++全局构造和析构

    分类: CRT Machnasim 2011-06-15 17:45 144人阅读 评论(0) 收藏 举报 c++汇编linuxlist语言编译器 1,程序入口函数和初始化 操作系统在装载可执行文件后 ...

  3. ConcurrentHashMap实现解析

    ConcurrentHashMap是线程安全的HashMap的实现,具有更加高效的并发性.与HashTable不同,ConcurrentHashMap运用锁分离技术,尽量减小写操作时加锁的粒度,即在写 ...

  4. 小米范工具系列之六:小米范 web查找器2.x版本发布

    小米范web查找器是一款快速识别端口及服务的小工具. 此工具使用java 1.8以上版本运行. 下载地址:http://pan.baidu.com/s/1c1NDSVe  文件名web finder ...

  5. docker搭建oracle 11.2.0.3.0

    dockerfile 如下: FROM oraclelinux:-slim ARG ORACLE_BASE=/opt/oracle ARG ORACLE_HOME=/opt/oracle/produc ...

  6. Java-idea-FindBugs、PMD和CheckStyle对比

    一.对比 工具 目的 检查项 备注 FindBugs 检查.class 基于Bug Patterns概念,查找javabytecode (.class文件)中的潜在bug 主要检查bytecode中的 ...

  7. SaltStack系列(二)之常用模块

    一.saltstack的内置模块汇总 acl,    aliases,    alternatives,    apache,    archive,    artifactory,    block ...

  8. 查T结果与Z结果的P值[转载]

    T检验P值表格来自:https://blog.csdn.net/m0_37777649/article/details/74937242 Z检验表格来自:https://wenku.baidu.com ...

  9. PAT 1073 Scientific Notation[字符串处理][科学记数法]

    1073 Scientific Notation(20 分) Scientific notation is the way that scientists easily handle very lar ...

  10. pyqt简单介绍和使用

    QML和PyQT5联合编程 安装pyqt pip3 install PyQT5 main.QML import QtQuick 2.2 import QtQuick.Controls 1.1 impo ...