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 ...
随机推荐
- linux下给网卡加VLAN标签和私网地址
1.加载8021q协议 moprobe 8021q 2.安装必要的包 yum -y groupinstall base linux 3.添加私网地址(写到开机启动项) vconfig add bond ...
- jdk 设计模式
摘自:http://blog.csdn.net/bakeloar/article/details/6608806
- Unity仪表盘显示UGUI制作小心得
最近在做设备仪表参数参数显示,由于模型摆放位置经常修改,加之要求不能在模型的下面添加东西,显示界面的位置也不得不跟着修改,一来二去就烦了,想了解决办法,现在总结如下: 1.仍然在模型下面新建Panel ...
- 软件工程(C编码实践篇)总结
陆伟丹 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com/course/USTC-1000002006 对软件工程 ...
- 启动tomcat部署项目时 ContainerBase.addChild: start:
严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component ...
- eclipse代码自动补全[转]
一.每次输入都自动提示 设置Window->preferences->Java->Editor->Content Assist 再右下角Auto activation trig ...
- solar system by HTML5
solar system by HTML5 星际穿越感觉很炫酷啊,网易貌似做了个专题在朋友圈挺火的.用canvas模拟太阳系,嗯,不错昂! 代码及效果 See the Pen GgpRjN by Na ...
- Linux时间设置及同步
Linux系统安装时选择的UTC时间是国际标准时间,而中国处于UTC+8时区,因此安装系统时不要选择UTC时区. 还有就是Linux有两个时钟: 1.Bios时钟及硬件时间 2.Kernel时钟及系统 ...
- DNS域名解析服务器
域名解析服务器,靠它把你要访问的网址找到然后把信息送到你电脑上.DNS 是域名系统 (Domain Name System) 的缩写,它是由解析器和域名服务器组成的.域名服务器是指保存有该网络中所有主 ...
- EasyUI的combobox控件使用onchange 问题
在项目中几次都遇到了同样的问题,现在都不知道怎样解决了! 路过的朋友们帮我看看嘛!谢谢了! 最后我想要实现的效果是这样的. 在下拉列表中不存在值.(这里的是下拉列表中存在值的!) 但是在我输入值 ...