POJ 3126 Prime Path
给定两个四位素数a b,要求把a变换到b
变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能重复。
求从a到b最少需要的变换次数。无法变换则输出Impossible
输入:
第一行 是一个数T 表示下面有T个测试数据
下面T行 每行两个数a b
解题思路:每次变换一次,个十百千,四个位置上的数每个从0-9循环一边一共需要4 * 10 = 40次循环 剪枝之后就没多少了 所以直接暴力BFS能过
下面是代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define N 10000
#define MAXV 10000
using namespace std;
bool Prime[];//Prime[i] = false证明 i 是素数
void MakePrime()//对素数打表
{
int i, j;
for(i = ; i <= N; i++) {
for(j = ; j < i; j++) {
if(i % j == ) {
Prime[i] = false;
break;
}
}
if(j == i) Prime[i] = true;
}
}
void BFS(int s, int e)
{
bool maps[N]; //用来记录此数是不是已经变换过
int step[N], d[], temp, e_temp, i, j; //d代表个十百千四个位置,step[i]用来储存变换为 i 时一共变换了几次
queue<int>q;
memset(step, , sizeof(step));
memset(maps, false, sizeof(maps));
q.push(s);
maps[s] = true;
while(!q.empty()) {
s = q.front();
q.pop();
d[] = s % ;//个
d[] = s / % ;//十
d[] = s / % ;//百
d[] = s / ;//千
for(i = ; i < ; i++) { //四个位置
temp = d[i]; //原本在此位置的数字 先用temp保存 因为后面如果改变的是其他位置这个位置不变
for(j = ; j < ; j++) { //0-9十个数
if(temp != j) { //自己不需要再次判断
d[i] = j;
e_temp = d[] + d[] * + d[] * + d[] * ;
if(!maps[e_temp] && Prime[e_temp]) { //判断这个数有没有判断过,是不是质数
maps[e_temp] = true;
step[e_temp] = step[s] + ;
q.push(e_temp);
}
if(e_temp == e) { //查找到 输出转换次数
printf("%d\n", step[e_temp]);
return;
}
}
d[i] = temp;
}
}
if(s == e) {
printf("%d\n", step[s]);
return;
}
}
//所有的数都变换过 无法得到结果
printf("Impossible\n");
}
int main()
{
MakePrime();
int start, end, T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &start, &end);
BFS(start, end);
}
}
POJ 3126 Prime Path的更多相关文章
- 双向广搜 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 ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- 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)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- (简单) POJ 3126 Prime Path,BFS。
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- poj 3126 Prime Path(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
随机推荐
- Countries in War -POJ3114Tarjan缩点+SPFA
Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...
- mysql 链接失败(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES))
mysql链接失败(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)) 修改: # ...
- 前端面试-----JavaScript题
用面试题,复习一下,js基础. 1.综合题 function Foo() { getName = function () { alert (1); }; return this; } Foo.getN ...
- mysql主从配置(基于mysql5.5.x)
安装mysql数据库主从复制,步骤如下:1.主从服务器分别作以下准备: 1.1.数据库版本完全一致 1.2.修改root的密码, 不改也可以 2.修改主服务器master #vi /etc ...
- 通知Notification
步骤: 1.调用getSystemService()获取NotificationManager:NotificationManager manager = (NotificationManager)g ...
- python 最小公倍数
最小公倍数 求解两个整数(不能是负数)的最小公倍数 方法一:穷举法 def LCM(m, n): if m*n == 0: return 0 if m > n: lcm = m else: lc ...
- 3d游戏模型及地形提取及导航
支持所有DirectX的游戏模型提取 有需要的可以直接联系我!QQ290387340
- SpringMVC生成Excel下载
SpringMVC controller里的方法: @RequestMapping(value="/notify/download",produces = {"appli ...
- 【学习笔记】在原生javascript中使用ActiveX和插件
什么是插件 现在的浏览器提供了大量的内置功能,但仍然有一些工作无法完成,如播放音频和视频.插件及其扩展浏览器功能就尤为重要. 插件是可下载的应用程序,可以插入到浏览器中,现在有很多不同的插件,常用的有 ...
- DNS域名解析服务器
域名解析服务器,靠它把你要访问的网址找到然后把信息送到你电脑上.DNS 是域名系统 (Domain Name System) 的缩写,它是由解析器和域名服务器组成的.域名服务器是指保存有该网络中所有主 ...