(简单) POJ 3126 Prime Path,BFS。
Description
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on 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.
#include<iostream>
#include<cstring>
#include<queue> using namespace std; bool rem[];
int rans[]; void getPrime()
{
for(int i=;i<;++i)
if(rem[i]==)
for(int j=i*;j<;j+=i)
rem[j]=;
} int bfs(int S,int E)
{
memset(rans,-,sizeof(rans)); queue<int> que;
int t;
int temp; que.push(S);
rans[S]=; while(!que.empty())
{
t=que.front();
que.pop(); if(t==E)
return rans[E]; for(int i=;i<=;++i)
{
temp=t%+i*;
if(rans[temp]==-&&rem[temp]==)
{
rans[temp]=rans[t]+;
que.push(temp);
}
}
for(int i=;i<=;++i)
{
temp=t%+*i+(t/)*;
if(rans[temp]==-&&rem[temp]==)
{
rans[temp]=rans[t]+;
que.push(temp);
}
}
for(int i=;i<=;++i)
{
temp=t%+*i+(t/)*;
if(rans[temp]==-&&rem[temp]==)
{
rans[temp]=rans[t]+;
que.push(temp);
}
}
for(int i=;i<=;++i)
{
temp=(t/)*+i;
if(rans[temp]==-&&rem[temp]==)
{
rans[temp]=rans[t]+;
que.push(temp);
}
}
} return -;
} int main()
{
ios::sync_with_stdio(false); getPrime(); int T;
int a,b;
int ans; cin>>T; while(T--)
{
cin>>a>>b; ans=bfs(a,b); if(ans==-)
cout<<"Impossible\n";
else
cout<<ans<<endl;
} return ;
}
(简单) POJ 3126 Prime Path,BFS。的更多相关文章
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- POJ 3126 Prime Path (BFS+剪枝)
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
随机推荐
- 获取Excel部分数据并很据项目要求计算适宜性等级综合指数判断该地区的土壤适宜性
代码运行前请先导入jxl架包,以下代码仅供学习参考: 下图为项目中的Excel: ExcelTest02类代码如下: // 读取Excel的类 import java.io.BufferedWrite ...
- OpenGL--------纹理处理
我们可以将像素数据按照矩形进行缩小和放大,但是还不足以满足我们的要求.例如要将一幅世界地图绘制到一个球体表面,只使用glPixelZoom这样的函数来进行缩放显然是不够的.OpenGL纹理映射功能支持 ...
- NullPointerException
if(userName.equals("zhansan")){} 可能会报空指针异常
- 转:SQL Case when 的使用方法
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- 如何修改apache的默认web端口
在apache的安装文件夹里搜索 httpd.conf 文件,用记事本打开,搜索 Listen 80 ,把80(默认端口)改为你想用的端口,保存,重新启动apache服务即可!
- java工程开发之图形化界面之(第五课)
下面我们将: 一)更加完整的解释Graphics类 二)使用方法来更清晰的重新编写前面图形小应用程序之一 三)介绍一些其他的绘图的方法 四)介绍方法init,它是类似于paint但是用于不同用途的另一 ...
- mysql分页pagination
http://www.phpjabbers.com/php--mysql-select-data-and-split-on-pages-php25.html returns 20 records so ...
- 剑指offer之有序二维数组查找
大多数人注意到元素是行列有序的,会马上想到对每行(或列)进行二分查找,每行(或列)需要logN时间,N行(或列)共需要NlogN时间,很容易写出如下代码 1 2 3 4 5 6 7 8 9 10 11 ...
- Android开发实现HttpClient工具类
在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.你可以把HttpClient想 ...
- Android利用canvas画各种图形 及Paint用法 .
引自:http://blog.csdn.net/carlfan/article/details/8139984 1.首先说一下canvas类: Class Overview The Canvas cl ...