题目链接: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. ZOJ 2702 Unrhymable Rhymes(DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1702 题目大意:给定有很多数字组成的诗,譬如 “AABB”, “AB ...

  2. UVA 10739 String to Palindrome(动态规划 回文)

    String to Palindrome 题目大意:给出一个字符串s,现在可以进行3种操作(添加字母,删除字母,替换字母),将其变成回文串,求出最少的操作次数.比如abccda,可以用删除操作,删除b ...

  3. UVA 11384 Help is needed for Dexter(问题转化 递归)

    Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided to keep Dee ...

  4. 九度OJ 1084 整数拆分

    题目地址:http://ac.jobdu.com/problem.php?pid=1084 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 ...

  5. Python没有执行__init__

    疑惑 提出问题 前天同事问我一个问题,为什么这个脚本中的没有调用A 的__init__.脚本如下: class A(object): def __init__(self, *args, **kwarg ...

  6. ubuntu ll命令

    用过 Redhat 的朋友应该很熟悉 ll 这个命令,就相当于 ls -l,但在 Ubuntu 中就不行了.严格来说 ll 不是一个命令,只是命令的别名而已.很多 Linux 用户都使用 bash s ...

  7. (转)myrepo

    源作者主页:https://copr.fedoraproject.org/coprs/mosquito/myrepo/ 源作者github: https://github.com/1dot75cm/m ...

  8. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

    http://quote.eastmoney.com/center/list.html#28003501_0_2 http://bbs.tianya.cn/post-53726-21098-1.sht ...

  9. Sqlserver知识点1

    1.字符串     字符数据类型是SQL Server 中最常用的数据类型之一,它可以用来存储各种字母.数字符号和特殊符号.在使用字符数据类型时,需要在其前后加上英文单引号或者双引号. (1)char ...

  10. 错误 1 error C2065: “IDC_LISTBOX”: 未声明的标识符

    错误的可能原因及解决方法如下:1.出错文件中没有包含资源文件ID声明的resource.h文件.在出错文件中加入#include “resource.h”语句. 2.工程附件包含目录的路径下没有res ...