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.

 
  题目就是说每一次变一个数,而且要求是素数,然后问最小变几次变成目标数,典型的BFS,先筛选出所有素数之后就直接爆搜就好。
 
代码如下:
#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。的更多相关文章

  1. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  3. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  4. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  5. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  6. POJ 3126 Prime Path BFS搜索

    题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...

  7. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

  8. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  9. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

随机推荐

  1. 1.2 sikuli API

    sikuli API网站:http://nightly.sikuli.de/docs/index.html eclipse中如果要用到相应的 sikuli 功能,可以查看API ,然后import相应 ...

  2. Spring Boot 系列教程1-HelloWorld

    入门 如果你用过Spring JavaConfig的话,会发现虽然没有了xml配置的繁琐,但是使用各种注解导入也是很大的坑, 然后在使用一下Spring Boot,你会有一缕清风拂过的感觉, 真是爽的 ...

  3. \t 的理解

    在同一个缓冲区内横向跳8个空格. 如果在 \t 前面有字符串,则包括 \t之前出现的字符串之内一共是8个空格.

  4. isinstance使用方法

    #!/usr/bin/python2.7    def displayNumType(num):    print num, 'is',    if isinstance(num,(int, long ...

  5. 转载 Deep learning:六(regularized logistic回归练习)

    前言: 在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在lo ...

  6. 快学Scala-第八章 继承

    知识点: 1.扩展类 extends关键字,在定义中给出子类需要而超类没有的字段和方法,或者重写超类的方法. 2.重写方法 在Scala中重写一个非抽象方法必须 override 修饰符 public ...

  7. [转]程序开发基础学习二(C++ Google Style 命名规则)

    无规矩不成方圆,新的岗位就需要服从团队的编码规则.很开心团队用的是Google的C++编码规则,大概看了下Google 的编码规则,正如九天翔雁说的:“Google的 C++ Style Guide远 ...

  8. HDU1372:Knight Moves(BFS)

    Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  9. poj 3020 Antenna Placement (最小路径覆盖)

    二分图题目 当时看到网上有人的博客写着最小边覆盖,也有人写最小路径覆盖,我就有点方了,斌哥(kuangbin)的博客上只给了代码,没有解释,但是现在我还是明白了,这是个最小路径覆盖(因为我现在还不知道 ...

  10. CSS3中的skew()属性

    刚开始接触CSS3的2D变换属性,就被这个skew()搞的一头雾水,不知道具体是怎么变化的! 研究了一会才发现,CSS3的斜切坐标系和数学中的坐标系完全不一样(设置斜切原点为左上角) <styl ...