Prime Path (poj 3126 bfs)
|
Language:
Default
Prime Path
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.
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
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input 3 Sample Output 6 Source |
题意: 给定两个素数(四位数),求第一个数经过几次转换可以得到第二个素数。
转换方式:是变换数中某一位的数字(第一位不能为零,其它的变换数字是0~~9),变换之后的数也为素数。
思路:bfs。搜索求最短路径,非常easy就想到广度优先搜索。由于广度优先搜索。第一次搜到得到的步数就是最少的步数。另外打素数表提高推断的时候的效率。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 10005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std; bool ISprime[maxn];
bool visit[maxn];
int m,n,a,b,c,d; struct Node
{
int p[4];//用数组存各位数
int step;
}; void prime() //素数筛法
{
for (int i=2;i<maxn;i++)
{
if (i%2)
ISprime[i]=true;
else
ISprime[i]=false;
}
int m=sqrt(10010.0);
for (int i=3;i<m;i++)
{
if (ISprime[i])
{
for (int j=i+i;j<maxn;j+=i)
ISprime[j]=false;
}
}
} int bfs()
{
Node st,now;
memset(visit,false,sizeof(visit));
queue<Node>Q;
while (!Q.empty())
Q.pop();
visit[m]=true;
st.p[0]=m/1000;st.p[1]=(m/100)%10;st.p[2]=(m/10)%10;st.p[3]=m%10;
// printf("%d %d %d %d\n",st.a[0],st.a[1],st.a[2],st.a[3]);
st.step=0;
Q.push(st);
while (!Q.empty())
{
st=Q.front();
Q.pop();
if (st.p[0]==a&& st.p[1]==b&&st.p[2]==c&&st.p[3]==d)
{
return st.step;
}
for (int i=0;i<=3;i++)
{
for (int j=0;j<10;j++)
{
if (st.p[i]==j)
continue;
if (i==0&&j==0)
continue;
now.p[0]=st.p[0];
now.p[1]=st.p[1];
now.p[2]=st.p[2];
now.p[3]=st.p[3];
now.p[i]=j;
int x=now.p[0]*1000+now.p[1]*100+now.p[2]*10+now.p[3];
if (ISprime[x]&&!visit[x])
{
visit[x]=true;
now.step=st.step+1;
Q.push(now);
}
}
}
}
return -1;
} int main()
{
prime();
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%d%d",&m,&n);
a=n/1000;b=(n/100)%10;c=(n/10)%10;d=n%10;
// printf("%d %d %d %d\n",a,b,c,d);
int ans=bfs();
if (ans==-1)
printf("Impossible\n");
else
printf("%d\n",ans);
}
return 0;
}
/*
3
1033 8179
1373 8017
1033 1033
*/
Prime Path (poj 3126 bfs)的更多相关文章
- Prime Path(POJ 3126 BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15325 Accepted: 8634 Descr ...
- Prime Path (POJ - 3126 )(BFS)
转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82697622 作者:Mercury_Lc 题目链接 题意:就是给你一个n, ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- (广度搜索)A - Prime Path(11.1.1)
A - Prime Path(11.1.1) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- POJ 3126 Prime Path(筛法,双向搜索)
题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...
- UVa 1599 Ideal Path (两次BFS)
题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...
- 2018.10.21 codeforces1071B. Minimum path(dp+贪心+bfs)
传送门 唉考试的时候写错了两个细节调了一个多小时根本没调出来. 下来又调了半个小时才过. 其实很简单. 我们先dpdpdp出最开始最多多少个连续的aaa. 然后对于没法继续连续下去的用贪心+bfsbf ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- hdu3483之二项式展开+矩阵快速幂
A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- FineReport实现Java报表主题分析的效果图
Java报表-財务主题-EVA经济附加 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVzdF9yZXBvcnQ=/font/5a6L5L2T/font ...
- BZOJ 3315: [Usaco2013 Nov]Pogo-Cow( dp )
我真想吐槽USACO的数据弱..= = O(n^3)都能A....上面一个是O(n²), 一个是O(n^3) O(n^3)做法, 先排序, dp(i, j) = max{ dp(j, p) } + w ...
- Oracle百问百答(二)
Oracle百问百答(二) 11. nvl函数有什么用? NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则 ...
- 双卡双待支持双电池 夏新N808深度评测_夏新手机评测-泡泡网
双卡双待支持双电池 夏新N808深度评测_夏新手机评测-泡泡网 双卡双待支持双电池 夏新N808深度评测
- Android TextView自己主动换行文字排版參差不齐的原因
今天项目没什么进展,公司后台出问题了.看了下刚刚学习Android时的笔记,发现TextView会自己主动换行,并且排版文字參差不齐.查了下资料,总结原因例如以下: 1.半角字符与全角字符混乱所致:这 ...
- C++如何屏蔽双击运行程序功能?
问题描述: 我们开发过程中可能会经常遇到,需要屏蔽EXE的双击运行功能,只能通过宿主程序(Service或EXE)来启动.比如腾讯的迷你弹窗,就只能通过主程序来启动,而不能直接通过双击来运行. 实现原 ...
- SonicUI在MFC中的使用
SonicUI是一个GUI引擎,提供了一些简单的UI组件实现高效率的UI效果,例如:自绘按钮.异形窗体.动画.超链接和图像操作方法.此项目作者开源到CodeProject,地址为:http://www ...
- 高性能MySql进化论(四):Summary,Cache,Counter表的使用
在实际的应用中,往往会定期的对一个周期内的系统数据进行统计分析.例如某购物网站定期的统计商品在一个月/年期内的销售情况,如果采用扫描所有相关表的方式在某个时间点进行统计分析, 由于数据量很大,以及表结 ...
- CSDN-Code平台使用过程中的5点经验教训
昨天又创建了一个项目,fucms,可是本地一直没有权限提交,搞了非常久,试了几十次,都不行,我是非常的灰心和郁闷. 刚刚,和CSDN-Code的官方客服咨询了非常久非常久,最终摸索出来了一些心得体会 ...
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.