题目链接: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. CRM - 销售与客户

    一.销售与客户 - 表结构 ---公共客户(公共资源) 1.没有报名 2.3天没有跟进 3.15天没有成单 客户分布表 龙泰 男 yuan 2018-5-1 3天未跟进 龙泰 男 三江 2018-5- ...

  2. Hotel---poj3667(线段树区间问题)

    题目链接:http://poj.org/problem?id=3667 题意:酒店有n个房间,现有m个团队,每个团队需要连续 d 个房间,现在有两个操作,1:需要 d 个房间,2:从 x 开始连续 d ...

  3. MongoDB: 原子性和事务

    在MongoDB中, 文档级别的的写操作是原子性的, 甚至是在对某个文档的操作中修改其多个内嵌的子文档, 也是原子性的. 在一个写操作同时修改多个文档的情况, 对其中单独的某个文档而言是原子的, 但是 ...

  4. python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon)

    python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon) 前言 今天小编YOYO请xiaoming和xiaowang吃火锅,吃完火锅的时候会有以下三种场景: - ...

  5. mysql int 整数类型 解释显示宽度 和 存储宽度

    存储宽度 是实际存储记录宽度 存储宽度默认是写死的,就算修改宽度也改变不了,改变的是显示宽度 ============有符号和无符号int============= 创建一个 无符号的 int 整数类 ...

  6. easyUI datagrid 清空

    最近在做一个管理系统,出于一些需要,经常要将一些datagrid清空.然后easyUI本身并没有自带的方法,然后自己动手丰衣足食吧. 清空无外乎两种思路,删除现有数据和填充空数据. 1.删除数据 va ...

  7. python拼接字符串

    python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello' + ' ' + 'World' + '!' print(s) 输出结果:Hello World! 使用 ...

  8. Parallel Decision Tree

    Decision Tree such as C4.5 is easy to parallel. Following is an example. This is a non-parallel vers ...

  9. gcc安装多个版本

    http://blog.csdn.net/chid/article/details/6251781

  10. 001-springmvc之原理图

    1.流程. 2.时序图. 3.方法调用. (0)DispatcherServlet类.提供了HandlerMapping成员关联关系. /** MultipartResolver used by th ...