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的更多相关文章

  1. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  2. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  3. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  4. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

  5. Prime Path(poj 3126)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  6. Prime Path(素数筛选+bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9519   Accepted: 5458 Description The m ...

  7. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

  8. Prime Path(POJ 3126 BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Descr ...

  9. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  10. Prime Path (poj 3126 bfs)

    Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Ac ...

随机推荐

  1. [转][JAVA]定时任务之-Quartz使用篇

    [BAT][JAVA]定时任务之-Quartz使用篇 定时任务之-Quartz使用篇 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与 ...

  2. 利用 SysRq 键排除和诊断系统故障

    说白了,SysRq手动触发而不用命令, /proc/sysrq-trigger 这个是通过命令接口 实验:LINUX窗口下 ,CTRL+ALT+F1,切换到TTY1,在文本模式下,按下 ALT+Sys ...

  3. [转] ReactNative Animated动画详解

    http://web.jobbole.com/84962/     首页 所有文章 JavaScript HTML5 CSS 基础技术 前端职场 工具资源 更多频道▼ - 导航条 - 首页 所有文章 ...

  4. [转] 使用NVM快速搭建NODE开发环境

    export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node 本文主要介绍最近自己碰到的几个坑以及搜索到的相应解决方案: 如何快速搭 ...

  5. Django模型之Meta选项详解

    Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.而可用的选项大致包含以下几类 abstract 这个属性是定义当前的模型是不是一个抽象类.所谓抽象类是不会对应数据 ...

  6. 第二篇:python基础之文件读写

    python基础之文件读写   python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使 ...

  7. webfont自定义字体的实现方案

    对于做Web前端的人来说,现在不知道webfont为何物似乎显得有点low了.webfont固然可爱,但似乎仍只可远观,不可亵玩.原因就在于中文字体库体积庞大,远比26个字母来的复杂.于是有同学就说了 ...

  8. HTML5 文件域+FileReader 读取文件并上传到服务器(三)

    一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...

  9. 与数据库打交道的Adapter----SimpleCursorAdapter

    http://www.cnblogs.com/wenjiang/p/3196486.html 程序员是这个世界上最神奇的职业,因为几乎所有其他职业的人都能转到该行来,只要他智力正常,有接受过正规的编程 ...

  10. opencart 模块开发详解

    opencart 模块开发详解 由 xiekanxiyang » 2013年 7月 11日 10:17 pm opencart 将页面分成若干模块, 每个模块可以有多个实例(可能这样说不是很恰当) 每 ...