题目链接: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. Codevs 1814 最长链

    1814 最长链 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少, ...

  2. (转)iOS学习之 plist文件的读写

    在做iOS开发时,经常用到到plist文件, 那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ...

  3. starling 中的 EventDispatcher 和 Flash中原生的 EventDispatcher

    starling 比较早之前就有开始解了,但只到最近参与一个用starling 做为框架的手游项目才真正做为一程来使用它. 项目也是刚开始搭建,在这做些笔记. 在写一个管理类时, 遇到 starlin ...

  4. ubuntu samba共享安装 配置

    参考: http://www.360doc.com/content/11/0615/12/3989678_127081905.shtml 参考: http://xfshean.blog.163.com ...

  5. wpf:DataGrid使用

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ...

  6. 2016030401 - java性能优化建议

    转载自:http://www.open-open.com/lib/view/open1399884636989.html#_label20 1.没有必要时不要使用静态变量 使用静态变量的目的是提高程序 ...

  7. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  8. sublime搭建c++/java/lua/python/ruby的配置文件

    本人电脑win7 64位 提前装一下convert to utf-8插件,编译运行出现乱码,组合键ctrl+shift+c把源文件转成gbk编码. 仍乱码的话,重启编辑器|电脑|重新编辑中文部分. c ...

  9. 网络流系列算法总结(bzoj 3438 1061)

    网络流嘛,怎么看都是一堆逗逼题嘛,反正遇到还是都做不起嘛.... 网络流的模板非常简单,难点都在于建图,网络流的建图解决问题范围之广,下至A+B Problem,上至单纯形,线性规划.所以如果对于网络 ...

  10. Spring 使用外部部署文件

    1.导入属性文件: <context:property-placeholder location="classpath:db.properties"/> 2.使用外部化 ...