POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径
分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stdlib.h>
#include<string>
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=0x3f3f3f3f;
int t,s;
int a[maxn];
bool vis[maxn];
bool fun(int x)
{
for(int i=; i*i<=x; ++i)
if(x%i==)return ;
return ;
}
struct Data
{
int b[];
} d,e;
queue<Data>q;
int get(Data x)
{
int ans=;
for(int i=; i>=; --i)
ans=ans*+x.b[i];
return ans;
}
int main()
{
int T;
for(int i=; i<; ++i)
if(fun(i))vis[i]=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&s,&t);
memset(a,-,sizeof(a));
while(!q.empty())q.pop();
if(!vis[s]||!vis[t])
{
printf("Impossible\n");
continue;
}
a[s]=;
for(int i=; i<; ++i)
{
d.b[i]=s%;
s/=;
}
q.push(d);
while(!q.empty())
{ e=d=q.front();
q.pop();
int k=get(d);
if(k==t)break;
for(int i=; i<; ++i)
{
for(int j=; j<; ++j)
{
if(j==d.b[i])continue;
e.b[i]=j;
int x=get(e);
if(!vis[x]||a[x]>=)continue;
a[x]=a[k]+;
q.push(e);
}
e.b[i]=d.b[i];
}
}
if(a[t]==-)
{
printf("Impossible\n");
continue;
}
printf("%d\n",a[t]);
}
return ;
}
POJ 3126 Prime Path BFS搜索的更多相关文章
- 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求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- poj 3126 Prime Path(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path 广度优先搜索 难度:0
http://poj.org/problem?id=3126 搜索的时候注意 1:首位不能有0 2:可以暂时有没有出现在目标数中的数字 #include <cstdio> #include ...
- POJ 3126 Prime Path (BFS+剪枝)
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...
随机推荐
- 在系统方法中调用navigationController的标准写法
在系统方法中调用navigationController的标准写法 -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animat ...
- Extjs-4.2.1(一)——编辑 hello word
前言:在搭建好环境后,就可以利用Extjs进行开发了. 一.在项目中拷贝相关的Extjs文件,如下图: 注意:不需要整个Ext开发包全部导入,这样很容易造成eclipse卡死,因为eclipse会自动 ...
- having——至少被订购过两回的订单
此篇介绍having的用法 一.表:订单表,产品表 说明:订单表order ,包含prodectid 二.查询至少被订购过两回的订单 800x600 Normal 0 7.8 磅 0 2 false ...
- 在MAC下调试运行暗黑全世界客户端及部分代码注解(基于Firefly)
原地址:http://www.myexception.cn/program/1399860.html 在MAC下调试运行暗黑全世界客户端及部分代码注解(基于Firefly) 在MAC下调试运行暗黑世界 ...
- html5移动web开发实战必读书记
原文 http://itindex.net/detail/50689-html5-移动-web 主题 HTML5 一.配置移动开发环境 1.各种仿真器.模拟器的下载安装 http://www.mob ...
- FZU-1924+判断环/DFS/BFS
dfs: /* dfs */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include< ...
- Android 控制ScrollView滚动到底部
scrollView.fullScroll(ScrollView.FOCUS_DOWN);滚动到底部 scrollView.fullScroll(ScrollView.FOCUS_UP);滚动到顶部 ...
- http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html
http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html
- CodeChef November Challenge 2014
重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...
- AppStore 沙箱环境的测试流程
1:请确保你打得版本是 沙箱环境的版本 2:请确保的手机序列号已经加入沙箱环境3:请确保你的手机Apple ID 账户已经退出 ------ 如果这些都准备好了,再进行测试吧,不然一堆的未知问题等着你 ...