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 ...
随机推荐
- Android如何创建背景透明的Dialog
一:控制Dialog 的背景方法:1.定义一个无背景主题主题<!--去掉背景Dialog--> <style name="NobackDialog" parent ...
- navigation的pushViewController卡顿问题
问题:在ios中一个viewController中,添加下面代码: <pre name="code" class="objc">UIViewCont ...
- bluetooth-蓝牙事件监听
今天在做项目的时候,需要监听一个蓝牙耳机的连接状态.就写了一个小的测试方法.记录如下 看代码 这要处理的是蓝牙监听事件 package com.example.alert; import androi ...
- 【IBM】Merlin 给 Java 平台带来了非阻塞 I/O
Merlin 给 Java 平台带来了非阻塞 I/O 新增的功能大幅降低了线程开销 Java 技术平台早就应该提供非阻塞 I/O 机制了.幸运的是,Merlin(JDK 1.4)有一根几乎在各个场合都 ...
- c# 操作.config中AppSettings配置节
ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...
- Quartz.NET管理类
最近做项目设计到Quartz.NET,写了一个Quartz.NET管理类,在此记录下. public class QuartzManager<T> where T : class,IJob ...
- C# - string 转为 DateTime(自定义)
上代码: string dt = " 1 11 1961"; DateTime day; System.Globalization.DateTimeFormatInfo dtFor ...
- 详解SQL Server 2005 Express下的事件探查器
安装Visual Studio 2008会有附带的SQL Server 2005 Express版 我们开发一般都用那个都不单独安装SQL Server的 大家都知道express版的sql是没有 事 ...
- linux创建用户
创建用户 sudo adduser xxx 删除用户 sudo userdel xxx 删除用户和目录 sudo userdel -r xxx
- CSRF 攻击的应对之道
转载自imb文库 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在 ...