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 ...
随机推荐
- 对xml操作
已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...
- How to Make LastPass Even More Secure with Google Authenticator
Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...
- Java反射学习(java reflect)(二)
ok之前说了Java的反射和反射分析类,那这些东西有神马作用呢,下面就来说应用: 三.运行时使用反射分析对象 简单写一个Employee类,然后利用JAVA反射去取name域,getDeclareFi ...
- Linux ./configure && make && make install 编译安装和卸载
正常的编译安装/卸载: 源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). configure文件是一个可执行的脚本文件,它有很多选项, ...
- 关于css float 属性以及position:absolute 的区别。
1.float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.div一个典型的块 ...
- illegal mix of collcations表连接时非法的校对
背景:旧表导入新表,新表里的字段是字符串类型 新表是int类型 两个字段通过字符串处理后相等 (准备left join 关联起来)报错 把int类型字段更改成varchar字符串类型后成功
- Unix/Linux 'dirctory tree' command.
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' It se ...
- emmet插件的导入与实用
http://jingyan.baidu.com/article/ff4116259b057c12e48237b8.html http://www.iteye.com/news/27580 分享htm ...
- iOS题
对于语句NSString* testObject = [[NSData alloc] init];关于testObject是什么类型对象,以下说法正确的是: 答案:(A) A.编译时,NSString ...
- 常用 Linux 命令
Check page size: getconf PAGESIZE Check memory information: cat /proc/meminfo Check number of hugepa ...