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 ...
随机推荐
- mksquash_lzma-3.2 编译调试记录
今天在编译mksquash_lzma-3.2的时候出现了如下问题: /home/test/RT288x_SDK/toolchain/mksquash_lzma-3.2/lzma443/C/7zip/C ...
- 百度地图API地址转换成经纬度
public class LngAndLatUtil { public static Map<String,Double> getLngAndLat(String address){ Ma ...
- 防止iframe嵌套
如果你哪个页面不想被嵌套 下面js代码可以解决(我的是火狐) 慎用 <script type="text/javascript"> window.on ...
- Chrome调试(转)
原文地址:http://blog.csdn.net/chenmoquan/article/details/44943245#comments 觉得写的很适合web开发的新手 Chrome 的开发者工具 ...
- Telerik_2012_Q3 破解全套下载链接
1.Telerik_OpenAccess_ORM_2012_3_1012_SDK.zip (暂未提供下载) 2. Telerik_OpenAccess_ORM_2012_3_1012.zip 3. T ...
- Bootstrap_网格系统
首先添加CSS样式: [class *= col-]{ background-color: #eee; border: 1px solid #ccc; } [class *= col-] [class ...
- devicePixelRatio
devicePixelRatio window.devicePixelRatio是设备上物理像素和逻辑像素的比例.公式表示就是:window.devicePixelRatio = 物理像素 / 逻辑像 ...
- 极光推送 api ios参数问题
这是首个app项目,推送用的是极光推送jpush 由于用官方文档出现接收多条的问题,在网上找到一套封装好的,非常感觉这位开发者 //推送.指定人error_reporting(E_ALL^E_NOTI ...
- ecshop数据库操作类
ECShop v2.7.2没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现. 好处:实现非常轻量,只有一个文件,27Kb,大大减小了分发包的文件大小. 当网站需要做me ...
- 分享29个超赞的响应式Web设计
原文自:http://www.csdn.net/article/2013-01-16/2813678-responsive-design-websites 最近几年,响应式Web设计不断印入人们眼帘, ...