F - Prime Path
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std; #define maxn 10000 int p[maxn];//标记4位数的素数 int Prime(int n)
{
int i, k=sqrt(n); for(i=; i<=k; i++)
if(n % i == )
return ; return ;
}
int Turn(int n, int k)//把n的第k位转换成0
{
char s[]={}; sprintf(s, "%d", n);
s[k] = '';
sscanf(s, "%d", &n); return n;
}
int BFS(int s, int e)
{
int i, j, k, q;
int v[maxn]={};
queue<int> Q; Q.push(s);
v[s] = ; while(Q.size())
{
s = Q.front();Q.pop(); if(s == e)
return v[s]-; int t = ; for(i=; i<; i++)
{
q = Turn(s, i); for(k=; k<; k++)
{
j = q+k*t; if(p[j] == && v[j] == )
{
Q.push(j);
v[j] = v[s] + ;
}
} t /= ;
}
} return -;
} int main()
{
int i, s, e, T; for(i=; i<maxn; i++)
p[i] = Prime(i); scanf("%d", &T); while(T--)
{
scanf("%d%d", &s, &e); int ans = BFS(s, e); if(ans == -)
printf("Impossible\n");
else
printf("%d\n", ans);
} return ;
}
F - Prime Path的更多相关文章
- [kuangbin带你飞]专题一 简单搜索 - F - Prime Path
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- 双向广搜 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 ...
随机推荐
- Web Service属性介绍
每个 Web Service都需要唯一的命名空间,它可使客户端应用程序区分出可能使用相同方法名称的 Web Service.在 Visual Studio.NET中创建的Web Service的默认命 ...
- NSString 的三种截取方法
1.定义一个字符串a, 截取a 的某一个项目组,复制给b, b必须是int型 NSString *a = @"1.2.30"; int b= [[a substringWithR ...
- sublime text snippet代码片断
$0 代表补全代码后放的位置 0 的权重是最低的 $1 最高 也等于${1:} ${1: name} 1输入点的序号(1权重最高) name 自动补全的默认值 <conte ...
- 64位系统下System32文件系统重定向
前言 因为一次偶然的机会,需要访问系统目录“C:/Windows/System32“文件夹下的内容,使用的测试机器上预装了win7 64系统.在程序运行中竟然发生了该文件路径不存在的问题!!通过查看网 ...
- Zend Server安装后首次运行就出现Internal Server Error的解决(转)
新近学习php,结果装了Zend Server上来就报错,网上找到了解决方法,照着做果然可行,转之. 刚才安装了Zend Server,安装后首次运行就爆出了一个Internal Server Err ...
- 2 - Annotations标注
下面是TestNG标注和参数的一个快速预览 @BeforeSuite 被标注的方法会在这个套件的所有测试执行之前执行 @AfterSuite 被标注的方法会在这个套件的所有测试执行之后执行 @Bef ...
- 原型链和new
http://www.cnblogs.com/objectorl/archive/2010/01/11/Object-instancof-Function-clarification.html 构造器 ...
- 汇总前端最最常用的JS代码片段
html5选择器 //参数均接收一个合法的css选择器 element = document.querySelector('.foo,.bar');//返回带有foo或者bar样式类的首个元素 ele ...
- Java包详解
背景: 在java中要求文件名和类名相同,所以如果把多个类放在一起,就可能出现文件名冲突 所以用包来解决,一个包中可以包含多个类 包是java提供的一种用于区别类的名字空间的机制,是类的组织方式,是一 ...
- [Windows] php开发工具,zendstudio13使用方法补丁
官网原版下载 http://downloads.zend.com/studio ... win32.win32.x86.exe 破解补丁: 链接:http://pan.baidu.com/s/1gdi ...