Prime Path
poj3126:http://poj.org/problem?id=3126
题意:给你两个数n,k,两个数都是四位数的素数。现在让你改变n的一位数,让n变成另外一个素数。然后把这个素数在改变其中的以为又变成一个新的素数,问你最少有这样变换几步,才能使得使他变成k。
题解:求最短的问题,很自然的就想到了BFS,此外这一题还要处理10000以内的素数,可以采用素数筛法。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int prime[];//标记该数是不是素数
struct Node{
int x;
int step;
};
int counts[];
int n,k;
void DealPrime(){//素筛
memset(prime,,sizeof(prime));
for(int i=;i<=;i++){
if(prime[i])
for(int j=*i;j<=;j+=i)
prime[j]=;
}
}
int BFS(int x){
queue<Node>Q;
for(int i=;i<=;i++)
counts[i]=;
Node tt;
tt.x=x;
tt.step=;
Q.push(tt);
counts[x]=;
while(!Q.empty()){
Node temp=Q.front();
Q.pop();
int xx=temp.x;
int a=xx%;//取出个位
int b=xx/%;//十位
int c=xx/%;//百位
int d=xx/;//千位
int step=temp.step;
for(int i=;i<=;i++){//个位的变换
int xxx=d*+c*+b*+i;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//十位的变换
int xxx=d*+c*+i*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//百位的变换
int xxx=d*+i*+b*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//千位的变换,注意千位不能为0
int xxx=i*+c*+b*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
}
return counts[k];
}
int main(){
int cas;
scanf("%d",&cas);
DealPrime();
while(cas--){
scanf("%d%d",&n,&k);
int ans=BFS(n);
if(ans==)
printf("Impossible\n");
else
printf("%d\n",ans);
} }
Prime Path的更多相关文章
- 双向广搜 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 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- Prime Path (poj 3126 bfs)
Language: Default Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11703 Ac ...
随机推荐
- WebKit历史项管理的实现
历史项管理依据标准定义,由Page管理一个Joint Session History, 包括了各个子Frame的历史项.逻辑上相应例如以下的关系: 从上面看三个层次:Page,Frame,以及JS B ...
- mysqldump 定时任务 执行后备份的文件为空
#!/bin/bash mysql_host="127.0.0.1" mysql_user="root" mysql_passwd="******** ...
- Java读取WEB-INF目录下的properties配置文件
如何在Java代码中读取WEB-INF目录下的properties配置文件,下文给出了一个解决方案. 我们习惯将一些配置信息写在配置文件中,比如将数据库的配置信息URL.User和Password写在 ...
- (转载)Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...
- iOS开发集成微信支付
首先需要理清楚流程: 1.用户使用APP客户端,选择商品下单. 2.商户客户端(就是你做的APP)将用户的商品数据传给商户服务器,请求生成支付订单. 3.商户后台调用统一下单API向微信的服务器发送请 ...
- 关于echarts绘图,主题的更换
echarts主题进行更换步骤: 模块化单文件引入(推荐) 1.查看自己想要的主题 echarts官网 http://echarts.baidu.com/echarts2/doc/example/t ...
- phpstorm 快捷方式 (备用)
常用快捷键 设置快捷键:File -> Settings -> IDE Settings -> Keymap -> 选择“eclipse” -> 然后“Copy”一份 ...
- 对象-关系映射ORM(Object Relational Mapping)(转)
ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现 Hibernate在实现ORM功能的时候主要用到的文件有:映射类(*.java).映射文件(*.hbm.xml)和数据库配置文件 ...
- python字符串跟整型互转
print ("整型:",int(50))a=int(50)print("整型:",a)numStr = "50";print (" ...
- WampServer修改端口及菜单Localhost
一.修改Apache端口 1.在界面中选Apache,弹出隐藏菜单选项,打开配置文件httpd.conf: 2.找到 Listen 80: 3.将 80 改成 8080(当然自己也可以设定别的不使用的 ...