(广度搜索)A - Prime Path(11.1.1)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description

their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
Input
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
bool num[10001],used[10001];
int s,e;
void prim()//判断素数
{
memset(num,true,sizeof(num));
for(int i=2;i*i<10001;i++)
{
if(num[i])
{
for(int j=i+i;j<10000;j=j+i)
num[j]=false;
}
}
memset(num,false,1000);
}
struct node
{
int x,step;
};
int d[4]={1,10,100,1000};//位数
int bfs(int x)
{
queue<node> que;
node tp,num;
int tx;
num.x=x,num.step=0;
used[x]=false,que.push(num);
while(!que.empty())
{
tp=que.front();
que.pop();
for(int i=0;i<4;i++)
for(int j=0;j<=9;j++)
if(i==3&&j==0)
continue;
else
{
if(j!=(tp.x/d[i]%10))
{
tx=((tp.x/d[i]/10)*10+j)*d[i]+tp.x%d[i];
if(used[tx])
{
used[tx]=false,num.x=tx,num.step=tp.step+1,que.push(num);
if(num.x==e)
return num.step;
}
}
} }
}
int main()
{
int t;
cin>>t;
prim();
while(t--)
{
cin>>s>>e;
memcpy(used,num,10000);
if(s==e)
cout<<"0"<<endl;
else cout<<bfs(s)<<endl;
}
return 0;
}
(广度搜索)A - Prime Path(11.1.1)的更多相关文章
- 【搜索】 Prime Path
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include& ...
- [kuangbin带你飞]专题一 简单搜索 - F - Prime Path
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- poj 3126 Prime Path(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- POJ 3126:Prime Path
Prime Path Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit St ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- Prime Path (poj 3126 bfs)
Language: Default Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11703 Ac ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
随机推荐
- 调用 setState 之后发生了什么?
(1)代码中调用 setState 函数之后,React 会将传入的参数对象与组件当前的状态合并,然后触发所谓的调和过程(Reconciliation).(2)经过调和过程,React 会以相对高效的 ...
- Spring MVC 中使用 Google kaptcha 验证码
验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...
- rman输出日志的几种方法(转)
在使用rman的时候经常会碰到以下两种场景,需要把rman的日志输出到文件中: 1.显示的日志太多,导致一个屏幕显示不完,影响了问题的诊断,这时候需要把rman的log输出到文本中,整个的诊断过程就相 ...
- 百度「Web 前端研发部」面试过程和常见问题 可能会采用哪些方法来面试 STAR 面试法 喜欢什么样的面试者 喜欢问的问题
http://segmentfault.com/a/1190000002498800 在他们的github上看到的,收藏一下备用.看完觉得还有很多要努力的地方. FEX 的面试过程 我们一般会有 3 ...
- [Aaronyang] 写给自己的WPF4.5 笔记13[二维自定义控件技巧-可视化状态实战,自定义容器,注册类命令,用户控件补充]
我的文章一定要做到对读者负责,否则就是失败的文章 --------- www.ayjs.net aaronyang技术分享 博文摘要:欢迎大家来支持我的<2013-2015 Aar ...
- 【转】iPhone X
iPhone X 在 CIIA 第一期报告中,我剖析了 iPhone 从诞生以来就存在的,以及后来产生的一些设计问题.昨天在苹果店里玩了一下 iPhone X,发现它不但继承了以往的 iPhone 的 ...
- Android UI系列-----CheckBox和RadioButton(1)
主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...
- GraphQL,你准备好了么?
一个多月前,facebook 将其开源了一年多的 GraphQL 标记为 production ready ( http://graphql.org/blog/production-ready/ ), ...
- 【emWin】例程三十二:窗口对象———Progbar
简介: 进度条通常在应用程序中用于实现虚拟化:本例程实现液晶亮度显示 . 触摸校准(上电可选择是否进入校准界面) 实验现象: 实验指导书及代码包下载: 链接:http://pan.baidu.com/ ...
- AI金融知识自学偏量化方向-目录0
前提: 统计学习(统计分析)和机器学习之间的区别 金融公司采用机器学习技术及招募相关人才 了解不同类型的机器学习 有监督学习 vs 无监督学习 迭代和评估 偏差方差权衡 结合有监督学习和无监督学习(半 ...