题意:就是找最短的四位数素数路径

分析:然后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搜索的更多相关文章

  1. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  3. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  4. poj 3126 Prime Path(搜索专题)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20237   Accepted: 11282 Desc ...

  5. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  6. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  7. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  8. POJ 3126 Prime Path 广度优先搜索 难度:0

    http://poj.org/problem?id=3126 搜索的时候注意 1:首位不能有0 2:可以暂时有没有出现在目标数中的数字 #include <cstdio> #include ...

  9. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

随机推荐

  1. 微软职位内部推荐-Data Scientist

    微软近期Open的职位: Job Description:Extracting accurate, insightful and actionable information from data is ...

  2. SVN 迁移

    前段时间公司的SVN服务器做升级,需要做SVN迁移,百度谷歌了解了大概,在测试环境试了一下,没什么问题,然后改在正式环境做,迁移成功.之前用的是1.6,我看了下官网有1.8,征得同意后就直接升级加迁移 ...

  3. CQRS学习——Storage实现(EF+Code First+DynamicReponsitory)[其四]

    [这里是的实现,指的是针对各个数据访问框架的一个基础实现] 目标 定义仓储/QueryEntry的基本功能 实现仓储的基本功能,以利于复用 实现一些常用的功能 提供一些便利的功能 目标框架 博主使用的 ...

  4. SSH开发框架搭建参考

    一, 参考文章: 1, http://blog.csdn.net/communicate_/article/details/8644040 这篇文章讲的还算详尽,但是貌似有一些多余的代码: 2,

  5. jquery判断对象是否获得焦点

    var isFocus=$("#tRow").is(":focus"); if(true==isFocus){ alert("focus") ...

  6. 【leetcode】Container With Most Water(middle)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  7. 我见过的 Objective-C, 讲的最通俗易懂的入门教程....

    http://www.cnblogs.com/mjios/category/454764.html  ---- 给力...

  8. 设置window窗口的背景色为护眼色

    win7设置:桌面右键 -> 个性化 -> 窗口颜色 -> 高级外观设置 ->  '项目'下拉菜单 ->  '窗口'

  9. HeadFirst设计模式之工厂模式

    一. 1."工厂模式"不是种真正的设计模式,而是一种编程术语 2.The Factory Method Pattern defi nes an interface for crea ...

  10. ANDROID_MARS学习笔记_S01_009Relative_LAYOUT例子

    1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...