[kuangbin带你飞]专题一 简单搜索 F

来源:互联网 发布:淘宝店运营方案 编辑:程序博客网 时间:2024/04/30 08:52

F - 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
31033 81791373 80171033 1033
Sample Output
670

题意:第一个数是几组测试数据,后边每行两个素数,都是四位数,让你从第一个变到第二个数;方式是:每次只能变某一位的数字,而且你每次变成的数字必须为素数;

思路:BFS;先素数打表;然后变某一位的值变为0-9的某个数,然后判定这个新得到的数字(我写的可能比较麻烦...大家可以自己想想办法)是不是素数和有没有出现过,如果没出现过而且是素数就步数加1压入队列。

#include<iostream>#include<cmath>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=10001;int s,e,v[maxn];bool prime[maxn];typedef struct node{int num,ans;}N;void is_prime()//线性筛素数打表 {memset(prime,true,sizeof(prime));prime[0]=prime[1]=false;int m=sqrt(maxn+0.5);for(int i=2;i<=m;i++){if(prime[i]==true){for(int j=i*i;j<maxn;j=j+i){prime[j]=false;}}}}int NEXT(int n,int k,int p){int A=n/1000;int B=n/100%10;int C=n/10%10;int D=n%10;if(k==1)//{return p*1000+B*100+C*10+D;}else if(k==2){return A*1000+p*100+C*10+D;}else if(k==3){return A*1000+B*100+p*10+D;}else if(k==4){return A*1000+B*100+C*10+p;}}int bfs(){if(s==e){return 0;}queue<N>q;int l=0;N now;now.num=s;now.ans=0;v[s]=1;q.push(now);while(!q.empty()){now=q.front();q.pop();for(int i=1;i<=4;i++)//个十百千位 {for(int j=0;j<=9;j++)//十个数0-9 {if(i==1&&j==0)//防止千位为10 {continue;}int X=NEXT(now.num,i,j);//把now.num这个数字的第i位变为jif(X==e){return now.ans+1;}if(prime[X]==true&&v[X]==0){v[X]=1;N next;next.num=X;next.ans=now.ans+1;q.push(next);}}}}return -1;}int main(){is_prime();int T;scanf("%d",&T);while(T--){memset(v,0,sizeof(v));scanf("%d%d",&s,&e);int ans=bfs();if(ans!=-1){printf("%d\n",ans);}else{printf("Impossible\n");}}}


0 0
原创粉丝点击