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 ...
随机推荐
- .net 使用AjaxControlToolkit.dll 遇到的"Sys"未定义问题
1.配置文件一般都会缺少<httpHandlers></httpHandlers> 这一段, <httpHandlers> <remove verb=&quo ...
- 禁用windows 10自动更新
按Win键+R键调出运行,输入“gpedit.msc”点击“确定”,调出“本地组策略编辑器”.顺序依次展开计算机配置,管理模板 ,windows组件 ,windows更新 点击右边“配置自动更新”,选 ...
- 绘图quartz之渐变
实现线性渐变 径向渐变 自己新建的view中有一个drawRect:(cgrect)rect方法 在这个方法里 可以通过画图 将内容显示在画板上(即最下边的view) 渐变的方式分两种 ...
- Linux 添加epel源
1.epel-release yum install epel-release 这样有些没办法通过yum 安装 可以这样安装(例如redis)
- js正则验证方法大全
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Java设计模式(学习整理)---工厂模式
1.工厂模式 1.1 为什么使用工厂模式? 因为工厂模式就相当于创建实例对象的new,我们经常要根据类Class生成实例对象,如A a=new A() 工厂模式也是用来创建实例对象的,所以以后new时 ...
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
- 防止iframe嵌套
如果你哪个页面不想被嵌套 下面js代码可以解决(我的是火狐) 慎用 <script type="text/javascript"> window.on ...
- shell调用shell
在默认条件下,执行shell文件会出现permission denied报错,一般是没有可执行权限.用chmod修改权限 chomd 777 score.sh //把所有权限给aa文件 777代表 ...
- innerHtml写法
swt_center = "<div id='new_swt_wee'>"; swt_center += '<a href="javascript:vo ...