1002: Prime Path
题目链接: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的更多相关文章
- POJ 3126:Prime Path
Prime Path Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit St ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Prime Path(素数筛选+bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9519 Accepted: 5458 Description The m ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- Prime Path(POJ 3126 BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15325 Accepted: 8634 Descr ...
随机推荐
- 工具系列之Sublime Text 3 使用总结
1.Sublime Text 2/3如何支持中文GBK编码(解决中文乱码) Sublime Text默认是只支持UTF8的编码,所以有些时候,当我们打开GBK文件时候,文件内会出先部分的乱码,如下图 ...
- 九度OJ 1451 不容易系列之一 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1451 题目描述: 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好“一件”事情尚且不易,若 ...
- Spring---Web MVC关于前台传值转换问题
Cannot convert value of type [java.lang.String] to required type [java.util.List]. 问题在于:(String to E ...
- ECMAScript一元操作符
在ECMAScript中提供了一元操作符进行简单的运算,一元操作符是ECMAScript中最简单的操作符,它只能对一个值进行操作. 一元操作符有两种类型,一种是递增和递减操作符,一种是一元加和一元减操 ...
- SQL Server中count(*), count(col), count(1)的对比
让我们先看一下BOL里面对count(*)以及count(col)的说明: COUNT(*) 返回组中的项数.包括 NULL 值和重复项. COUNT(ALL expression) 对组中的每一行都 ...
- 【转】oracle Sequence
http://blog.csdn.net/zhoufoxcn/article/details/1762351 在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没 ...
- js 中如何通过提示框跳转页面
通过提示框跳转页面 <!doctype html> <html lang="en"> <head> <meta charset=" ...
- Sass中常用的函数
字符串函数 To-upper-case() 函数将字符串小写字母转换成大写字母 To-lower-case() 函数 与 To-upper-case() 刚好相反,将字符串转换成小写字母 数字函数 S ...
- 字符串匹配的python实现
所有字符串匹配算法的核心问题是,当出现不匹配时,如何向后移动模式串 一.暴力匹配算法 如果要匹配一个字符串s 和一个模式串p,则从i=0开始依次匹配s[i:(i+len(p))],简单粗暴,代码如下: ...
- lucene 实现word,pdf全文检索源码
创建索引: import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import jav ...