题目链接:http://172.16.200.33/JudgeOnline/problem.php?id=1002

题意:给你两个四位数的素数,求最少经过多少步的变化能够从一个素数变到另一个素数。在变得过程中,要求都是素数,而且每个新的数和原来的数只有一位不一样。

思路:因为是四位的素数,所以先对素数打表,然后BFS,每次进入队列的是八个只有一个数字不同的四位数,并且还要是素数就入队

#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
#define M 10000
int prime[M]; bool isprime(int x) //素数打表
{
for(int i=;i<=sqrt(x);i++)
if(x%i==) return false;
return true;
} void init()
{
for(int i=;i<=;i++)
{
if(isprime(i)) prime[i]=;
else prime[i]=;
}
} int bfs(int a,int b)
{
queue<int>q;
bool vis[M];
int cout[M],t[],temp;
memset(vis,false,sizeof(vis));
memset(cout,,sizeof(cout)); q.push(a);//入队
vis[a]=true; while(!q.empty())//判断队列是否为空
{
int x=q.front();
q.pop();
t[]=x/;
t[]=x/%;
t[]=x/%;
t[]=x%; for(int j=;j<;j++)
{
int temp=t[j];
for(int i=;i<;i++)
if(i!=temp)
{
t[j]=i;
int y=t[]*+t[]*+t[]*+t[];
if(!vis[y]&&prime[y])
{
cout[y]=cout[x]+;
vis[y]=true;
q.push(y);
}
if(y==b) return cout[y];
}
t[j]=temp;
}
if(x==b) return cout[x];
}
return -;
}
int main()
{
init();
int n,x,t,f;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&x); f=bfs(n,x);
if(f!=-) printf("%d\n",f);
else printf("Impossible\n");
}
return ;
}

1002: Prime Path的更多相关文章

  1. POJ 3126:Prime Path

    Prime Path Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit St ...

  2. 双向广搜 POJ 3126 Prime Path

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

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

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

  4. hdu 1973 Prime Path

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

  5. POJ2126——Prime Path(BFS)

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

  6. Prime Path(poj 3126)

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

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

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

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

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

  9. Prime Path(POJ 3126 BFS)

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

随机推荐

  1. HDU 4708 Rotation Lock Puzzle(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 题目大意:给定一个方形矩阵,边长为3-10的奇数.每一圈的数字可以沿着顺时针方向和逆时针方向旋转 ...

  2. SETLOCAL

    Quote from: http://ss64.com/nt/setlocal.html SETLOCAL Set options to control the visibility of envir ...

  3. Socket 死连接详解

    Socket 死连接详解 当使用 Socket 进行通信时,由于各种不同的因素,都有可能导致死连接停留在服务器端,假如服务端需要处理的连接较多,就有可能造成服务器资源严重浪费,对此,本文将阐述其原理以 ...

  4. grunt-npm编译bootstrap源码

    因为gmu需要用编译的方式下载,于是联想到bootstrap什么less sass编译 less先学,据说基于js编译和less编译器,安装起来详细, sass据网上介绍基于ruby, 以下为在mac ...

  5. 上传文件到服务器端后进一步推送到sftp服务器

    扩展安装 要想sftp服务端发送文件,就需要php脚本具有作为ssh客户端的能力,所以需先为php安装如下扩展 openssl openssl-dev libssh php ssh 扩展 按照下面的命 ...

  6. 用Python和Django实现多用户博客系统(二)——UUBlog

    这次又更新了一大部分功能,这次以app的形式来开发. 增加博客分类功能:博客关注.推荐功能(ajax实现) 增加二级频道功能 更多功能看截图及源码,现在还不完善,大家先将就着看.如果大家有哪些功能觉的 ...

  7. Dom学习笔记-(一)

    一.概述 DOM(文档对象模型)是针对HTML和XML文档的一个API,其脱胎于DHTML. DOM可以将任意HTML和XML文档描绘成一个由多层节点构成的结构. 每一个文档包含一个根节点-文档节点, ...

  8. hairline!ios实现边框0.5px

    在2014WWDC上,Ted O’Connor提出了“retina hairlines”的解决方案,即在ratina屏幕上可以显示0.5px宽度的边框.他的方案是这样的: 1 Standard bor ...

  9. 菜鸟Android之路(上)

    自己为什么要学android 本人作为应届毕业生,自己进入社会前做过好多梦,可是呢,现实还是打败了无邪!!面对社会的压力和残酷的竞争力自己如何生成下去??我自己对自己说:第一步 先养活自己,才能走好以 ...

  10. C#中引用(ref关键字)参数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 函数的参 ...