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$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
随机推荐
- ubuntu ibus ,chinese input-method
第一:安装IBus框架, sudo apt-get install ibus ibus-clutter ibus-gtk ibus-gtk3 ibus-qt4 启动IBus框架,在终端输入: im-s ...
- 设计模式之——Chain of Responsibility
Chain of Responsibility模式又叫做责任链模式,是将多个对象组成一条职责链,然后按照职责链上的顺序一个一个的找出是谁来负责处理. 这个模式很简单,下面就是一个实例程序,有六个处理器 ...
- conda
Conda是什么? Conda 是Anaconda下用于包管理和环境管理的命令行工具, Conda下一切都是包,包括Python和conda自己 Conda ≍ pip(包管理) + vitualen ...
- mysql 数据操作 单表查询 where约束 练习
create table employee( id int not null unique auto_increment, name ) not null, sex enum('male','fema ...
- 创建WCF服务的过程
一.创建控制台WCF工程 1.创建一个控制台工程2.System.ServiceModel的引用3.可创建多个WCF服务,如:IService.cs和Service.cs 顺序:右键->添 ...
- 002-线程实现方式【thread、runnable、callale、thread和runnable对比】
一.概述 1.实现方式 在java中对于多线程实现一定要有一个线程的主类,而这个线程的主类往往是需要操作一些资源,但是对于多线程主类的实现是: 继承Thread父类 从java的Thread类继承实现 ...
- Linux系统——防火墙
防火墙的作用 一种将内部网络和外部网络分开的方法,是一种隔离技术.防火墙在内网与外网通信是进行访问控制,一句锁设置的规则对数据包做出判断,最大限度地阻止网络中的黑客破坏企业网络,从而加强企业网络安全. ...
- tp基础补充
ThinkPHP php框架 真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分 ...
- SNMP Introduction
Basic command of SNMP: GET: The GET operation is a request sent by the manager to the managed device ...
- RESTful源码笔记之RESTful Framework的Mixins小结
0x00 引言 本篇对drf中的mixins进行简要的分析总结.Mixins在drf中主要配合viewset共同使用,实现http方法与mixins的相关类与方法进行关联. from rest_fra ...