HDOJ-三部曲一(搜索、数学)-1008-Prime Path
Prime Path
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 13

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.
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std; bool num[10000]={false},f[10000];
int n1,n2;
int que[20000],step[10000],m[10000]; //数组m表示上次变得位数。如果上次变了一位这次还变同一位就没有意义了
void judgeprime() //打标法判断1000到9999各个数是否是素数
{
for(int i=1000;i<10000;i++)
{
for(int j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{
num[i]=false;
break;
}
else
num[i]=true;
}
}
} int BFS()
{
int front=0,rear=1;
step[front]=0;
que[0]=n1;
f[n1]=true;
if(que[front]==n2)
return step[front];
while(front<rear)
{
int t=que[front]/10*10; //变个位
for(int i=0;i<=9;i++)
{
if(num[t]&&!f[t]&&m[front]!=1)
{
que[rear]=t;
f[t]=true;
m[rear]=1;
step[rear]=step[front]+1;
if(que[rear]==n2)
return step[rear];
rear++;
}
t++;
}
t=que[front]/100*100+que[front]%10; //变十位
for(int i=0;i<=9;i++)
{
if(num[t]&&!f[t]&&m[front]!=2)
{
que[rear]=t;
f[t]=true;
m[rear]=2;
step[rear]=step[front]+1;
if(que[rear]==n2)
return step[rear];
rear++;
}
t+=10;
}
t=que[front]/1000*1000+que[front]%100; //变百位
for(int i=0;i<=9;i++)
{
if(num[t]&&!f[t]&&m[front]!=3)
{
que[rear]=t;
f[t]=true;
m[rear]=3;
step[rear]=step[front]+1;
if(que[rear]==n2)
return step[ rear];
rear++;
}
t+=100;
}
t=1000+que[front]%1000;
for(int i=0;i<=8;i++) //变千位
{
if(num[t]&&!f[t]&&m[front]!=4)
{
que[rear]=t;
f[t]=true;
m[rear]=4;
step[rear]=step[front]+1;
if(que[rear]==n2)
return step[rear];
rear++;
}
t+=1000;
}
front++;
}
} int main()
{
judgeprime();
int T;
cin>>T;
/*for(int i=1000;i<10000;i++)
if(num[i])
cout<<i<<' ';*/
while(T--)
{
cin>>n1>>n2;
memset(que,0,sizeof(que));
memset(f,false,sizeof(f));
memset(step,0,sizeof(step));
memset(m,0,sizeof(m));
cout<<BFS()<<endl;
}
}
HDOJ-三部曲一(搜索、数学)-1008-Prime Path的更多相关文章
- (广度搜索)A - Prime Path(11.1.1)
A - Prime Path(11.1.1) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- 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 ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
随机推荐
- [saiku] 免登陆进入管理后台
上一篇分析了介绍了如何简化和修改saiku的界面[http://www.cnblogs.com/avivaye/p/4877882.html] 这一篇说明下如何去掉免登陆进入saiku 管理台 1.修 ...
- 《javascript高级程序设计》 第20章 JSON
20.1 语法 20.1.1 简单值 20.1.2 对象 20.1.3 数组 20.2 解析与序列化 20.2.1 JSON 对象 20.2.2 序列化选项 20.2.3 解析选项 JSON 对象有两 ...
- 小例子(三)、winform控件的移动
程序:Do You Love Me ? 说明:就是鼠标移动到“不爱”按钮上按钮就会移动到其他地方 代码: //鼠标进入控件表面的事件MouseEnter //this.ClientSize.Width ...
- hdu----(5047)Sawtooth(大数相乘+数学推导)
Sawtooth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- nyoj------170网络的可靠性
网络的可靠性 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助商.它将提供先进的网络协作技术,展 ...
- jquery.query.js 插件的用法
转载自:http://www.cnblogs.com/dachie/archive/2010/09/16/1827840.html 代码如下: var url = location.search; & ...
- Go语言并发与并行学习笔记(一)
转:http://blog.csdn.net/kjfcpua/article/details/18265441 如果不是我对真正并行的线程的追求,就不会认识到Go有多么的迷人. Go语言从语言层面上就 ...
- noip知识点总结之--欧几里得算法和扩展欧几里得算法
一.欧几里得算法 名字非常高大上的不一定难,比如欧几里得算法...其实就是求两个正整数a, b的最大公约数(即gcd),亦称辗转相除法 需要先知道一个定理: gcd(a, b) = gcd(b, a ...
- HtmlHelper—DropDownList:SelectList、SelectListItem
前言 在项目中经常使用到DropDownList来显示数据库中的数据,典型的例子为为某书籍选择所属类型. 使用SelectList来实现: 实现一: Controller 代码 SelectList ...
- 判断子元素(or属性)是否存在
if(typeof($("#aid").attr("rel"))=="undefined") 即可