1002: Prime Path
题目链接:http://172.16.200.33/JudgeOnline/problem.php?id=1002
题意:给你两个四位数的素数,求最少经过多少步的变化能够从一个素数变到另一个素数。在变得过程中,要求都是素数,而且每个新的数和原来的数只有一位不一样。
思路:因为是四位的素数,所以先对素数打表,然后BFS,每次进入队列的是八个只有一个数字不同的四位数,并且还要是素数就入队
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
#define M 10000
int prime[M]; bool isprime(int x) //素数打表
{
for(int i=;i<=sqrt(x);i++)
if(x%i==) return false;
return true;
} void init()
{
for(int i=;i<=;i++)
{
if(isprime(i)) prime[i]=;
else prime[i]=;
}
} int bfs(int a,int b)
{
queue<int>q;
bool vis[M];
int cout[M],t[],temp;
memset(vis,false,sizeof(vis));
memset(cout,,sizeof(cout)); q.push(a);//入队
vis[a]=true; while(!q.empty())//判断队列是否为空
{
int x=q.front();
q.pop();
t[]=x/;
t[]=x/%;
t[]=x/%;
t[]=x%; for(int j=;j<;j++)
{
int temp=t[j];
for(int i=;i<;i++)
if(i!=temp)
{
t[j]=i;
int y=t[]*+t[]*+t[]*+t[];
if(!vis[y]&&prime[y])
{
cout[y]=cout[x]+;
vis[y]=true;
q.push(y);
}
if(y==b) return cout[y];
}
t[j]=temp;
}
if(x==b) return cout[x];
}
return -;
}
int main()
{
init();
int n,x,t,f;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&x); f=bfs(n,x);
if(f!=-) printf("%d\n",f);
else printf("Impossible\n");
}
return ;
}
1002: Prime Path的更多相关文章
- POJ 3126:Prime Path
Prime Path Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit St ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Prime Path(素数筛选+bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9519 Accepted: 5458 Description The m ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- Prime Path(POJ 3126 BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15325 Accepted: 8634 Descr ...
随机推荐
- [002] The Perks of Being a Wallflower - 读后记
The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower" ...
- VS2010修改默认配置路径
视图->属性管理器 弹出如下截图:
- 关于C++对汉字拼音的处理(2)
对于前面获取字符串汉字全拼音的功能,大家应该有个了解了.现在我又综合广大网友流传的获取字符串汉字拼音首字母的功能进行了整理.介绍如下 这个功能写的稍微有点复杂 使用3个函数解决了获取字符串汉字首拼音串 ...
- IOS 学习笔记 2015-04-03 OC-API-文件读写
// // WPFileHelper.m // OC-API-文件操作 // // Created by wangtouwang on 15/4/3. // Copyright (c) 2015年 w ...
- 添加PATH
在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时, ...
- 数据结构学习——shell排序的C语言实现
shell排序: 这个排序的命名是来自发明者的名字,和排序的方法没有字面上的联系.所以不要因为名字而感觉很难.在K&R的C程序设计语言中书中只用了几行代码很简洁的实现了这个排序算法.那就来看看 ...
- onmouseleave与onmouseout区别
1.onmouseleave.onmouseenter,鼠标进入到指定元素区域内触发事件,不支持冒泡,不包含子元素的区域. 2.onmouseout.onmouseover.鼠标进入指定元素触发事件, ...
- IE11的CSS兼容性问题
最近测试给了我一大堆BUG,一瞅发现全是IE11的.吐槽一下这个浏览器真的比较特立独行.很多默认的样式跟别的浏览器不同,而且最明显的一点应该是padding左右内边距往往比别的浏览器大了一倍.但是当需 ...
- vs2012生成的项目,如何在只装有VS2010的电脑上打开
步骤: 1.用记事本打开Vs2012生成的项目解决方案文件(.sln文件)文件 2.修改前两行 Microsoft Visual Studio Solution File, Format Versio ...
- 最浅显、易懂的Linux 硬链接与软链接的理解
正文: Linux上的文件可以这么理解:文件-->文件名.文件是一个Object,也就是磁盘上的二进制数据.一个文件可以有多个文件名,平时我们都是通过文件名访问文件Object. 这样,硬链接可 ...