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,每次枚举改变的数,有一个剪枝,就是假设这个 ...
随机推荐
- Telerik 控件事例(鼠标拖动行,拖动列,设置行对齐,行宽,是否显示)
People.cs using System;using System.Collections.Generic;using System.Data;using System.Linq;using Sy ...
- WCF入门教程一[什么是WCF]--转载只为学习收藏
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- SCRUM,一个采用迭代,增量方法来优化可预见控制风险
Scrum是一个用于开发和维持复杂产品的框架,是一个增量的,迭代的开发过程.在这个框架中,整个开发过程是由若干个短的迭代周期组成,一个短的迭代周期称为一个Sprint,每个Sprint的建议长度是2到 ...
- MyEclipse中文注释乱码解决
MyEclipse中文注释乱码解决 将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclipse编码 ...
- linux 安装firefox
从火狐官网下载的firefox-9.0.1.tar.bz2解压后,进入firefox文件夹,执行./firefox会提示缺少库,故采用yum安装Firefox9.1.切换到root用户 su - 2. ...
- The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemG:Give Me the Number
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971 题意:将输入的英文数字表达转化为阿拉伯数字. #include< ...
- Samza文档翻译 : Concepts
此页介绍啊Samza的一些高层级概念. Streams Samza处理Streams(流).流由同一类型的不可变的消息组成.例如,一个流可以是对一个网站的所有点击,或者对一个数据库表的所有更新,或者一 ...
- 深入浅出 ES6:ES6 与 Babel / Broccoli 的联用
深入浅出 ES6指的是添加在 ECMASript 标准第六版中的 JavaScript 编程语言的新特性,简称为 ES6. 虽然 ES6 刚刚到来,但是人们已经开始谈论 ES7 了,它未来的样子,以及 ...
- 谈 DevOps 自动化时,也应该考虑到 SOX 等法案
[编者按]作者 Aaron Volkmann 是 CERT Division 高级研究员,在本文中,他对 DevOps 自动化违反 SOX 法案进行了阐述.同时,也简单的提出了如何通过 CI 来避免这 ...
- Generating Huge reports in JasperReports
There are certain things to care while implementing the Jasper Reports for huge dataset to handle th ...