Prime path

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.

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
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

//

本人第一次在csdn上写博客,有点小激动哈^-^如有错误,有请大佬们指正!

题解:本题是一道bfs问题,但并不是非常明显,需要稍微对题目进行分析。下面放代码,详细说明见代码注释。

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int t;
int m,n;
int prime[10000];                //用于判断是否为素数

int que[10000][2];               //que[x][0]用于存放符合要求的素数,que[x][1]用于存放该素数被找到所用的步数

bool p[10000];                    //用于判断某个素数是否已经入队

void bfs()
{
que[1][0]=m;               //队列初始化
que[1][1]=0;
int h=0;
int t=1;
int x;
do
{
h++;
if (que[h][0]==n)
{
cout<<que[h][1]<<endl;                    //如果找到目标数字,则输出步数并停止搜索
return ;
}
for (int i=1;i<=9;i++)                                //改变千位的数字
{
x=i*1000+que[h][0]-que[h][0]/1000*1000;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
for (int i=0;i<=9;i++)                                //改变百位的数字
{
x=i*100+que[h][0]/1000*1000+que[h][0]%100;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
for (int i=0;i<=9;i++)                            //改变十位的数字
{
x=i*10+que[h][0]/100*100+que[h][0]%10;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
for (int i=1;i<=9;i+=2)                        //改变个位的数字
{
x=que[h][0]/10*10+i;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
}while(h<t);
cout<<"Impossible"<<endl;                    //如果全部找过了也没有目标数字,则输出Impossible
}
int main()
{
prime[2]=prime[3]=1;

//这是一种快捷的素数打表方法(之前在一篇博客上看到的,但不记的博 主的名字了),规律是除2,3以外,所有的素数要么是6的倍数加1,要么减1;

for (int i=5;i<10000;i++) 

{
if ((i+1)%6==0||(i-1)%6==0)
{
int j;
for (j=2;j<(int)(sqrt(i)+1);j++)
{
if (i%j==0)
break;
}
if (j==(int)(sqrt(i)+1))
prime[i]=1;
}
}
cin>>t;
while(t--)
{
memset(p,0,sizeof(p));
cin>>m>>n;
bfs();
}

}


//如果有什么建议,欢迎在评论区留言!

全部评论

相关推荐

上周组里招人,我面了六个候选人,回来跟同事吃饭的时候聊起一个让我挺感慨的现象。前三个候选人,算法题写得都不错。第一道二分查找,五分钟之内给出解法,边界条件也处理得干净。第二道动态规划,状态转移方程写对了,空间复杂度也优化了一版。我翻他们的简历,力扣刷题量都在300以上。后三个呢,就有点参差不齐了。有的边界条件没处理好,有的直接说这道题没刷过能不能换个思路讲讲。其中有一个女生,我印象特别深——她拿到题之后没有马上写,而是先问我:“面试官,我能先跟你确认一下我对题目的理解吗?”然后她把自己的思路讲了一遍,虽然最后代码写得不是最优解,但整个沟通过程非常顺畅。这个女生的代码不是最优的,但当我问她“如果这里是线上环境,你会怎么设计’的时候,她给我讲了一套完整的方案——异常怎么处理、日志怎么打、怎么平滑发布。她对这是之前在实习的时候踩过的坑。”我在想LeetCode到底在筛选什么?我自己的经历可能有点代表性。我当年校招的时候,也是刷了三百多道题才敢去面试。那时候大家都刷,你不刷就过不了笔试关。后来工作了,前三年基本没再打开过力扣。真正干活的时候,没人让你写反转链表,也没人让你手撕红黑树。更多的是:这个接口为什么慢了、那个服务为什么OOM了、线上数据对不上了得排查一下。所以后来我当面试官,慢慢调整了自己的评判标准。算法题我还会出,但目的变了。我出算法题,不是想看你能不能背出最优解。而是想看你拿到一个陌生问题的时候,是怎么思考的。你会先理清题意吗?你会主动问边界条件吗?你想不出来的时候会怎么办?你写出来的代码,变量命名乱不乱、结构清不清楚?这些才是工作中真正用得到的能力。LeetCode是一个工具,不是目的。它帮你熟悉数据结构和常见算法思路,这没问题。但如果你刷了三百道题,却说不清楚自己的项目解决了什么问题、遇到了什么困难、你是怎么解决的,那这三百道题可能真的白刷了。所以还要不要刷LeetCode?要刷,但别只刷题。刷题的时候,多问自己几个为什么:为什么用这个数据结构?为什么这个解法比那个好?如果换个条件,解法还成立吗?把刷题当成锻炼思维的方式,而不是背答案的任务。毕竟面试官想看到的,从来不是一台背题机器,而是一个能解决问题的人。
牛客51274894...:意思是光刷力扣还不够卷
AI时代还有必要刷lee...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务