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,每次枚举改变的数,有一个剪枝,就是假设这个 ...
随机推荐
- 微软职位内部推荐-Principal Dev Manager for Windows Phone Shell
微软近期Open的职位: Location: China, BeijingDivision: Operations System Group Engineering Group OverviewOSG ...
- 微软职位内部推荐-Senior SDE for Cloud Platform
微软近期Open的职位: Microsoft Launched the public cloud service-Azure in China on May 2013, Cosmos is the k ...
- (转)[C++语法] 关键字typedef用法
转自http://www.cnblogs.com/SweetDream/archive/2006/05/10/395921.html C/C++语言中的typedef相信大家已经不陌生,本文对C/C+ ...
- 1017: [JSOI2008]魔兽地图DotR - BZOJ
Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Anc ...
- POJ - 1741 Tree
DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001).Defin ...
- XP纯净版光盘ISO镜像文件
原版xp的下载地址,直接发链接得了-.-打开迅雷,按新建任务,把下面下载地址复制进来,下载就可以了 thunder://QUFodHRwOi8vc29mdC51c2Fpa2EuY24vstnX98+1 ...
- JAVA面试题:String 堆内存和栈内存
java把内存划分为两种:一种是栈(stack)内存,一种是堆(heap)内存 在函数中定义的一些基本类型的变量和对象的引用变量都在栈内存中分配,当在一段代码块定义一个变量时,java就在栈中为这个变 ...
- live555源码研究(三)------UsageEnvironment类
一.UsageEnvironment类作用 1,不使用的时候回收当前的使用环境. 2,对返回结果消息和错误消息的维护. 二.类UsageEnvironment继承关系图
- Servlet课程0424(一) 通过实现Servlet接口来开发Servlet
//这是我的第一个Servlet,使用实现Servlet接口的方式来开发 package com.tsinghua; import javax.servlet.*; import java.io.*; ...
- 安装Hadoop系列 — 安装Eclipse
1.下载 Eclipse从 http://www.eclipse.org/downloads/index-developer.php下载合适版本,如:Eclipse IDE for C/C++ Dev ...