E - Prime Path(BFS+埃式筛)

it2025-02-25  27

E - 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.

输入

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).

输出

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

样例输入

3 1033 8179 1373 8017 1033 1033

样例输出

6 7 0

题意大概就是首先会给出数字T(T<=100)代表样例数,然后每个样例会给你两个四位素数a,b(不含前导零,比如0123)。

每次你可以将a的一位数变为任意一个数字,但是变化之后的a1必须还是一个素数。

输出a变成b所需的最小步数,如果a无法变成b输出“Impossible”。

思路 搜索题,按照正常的BFS思路就可以解决。 但在写代码之前我们还需要先解决一下几个关键的点。

第一,我们需要在搜索之前把1000~9999中所有的素数先预处理一下,避免超时。

这里我用的是埃式筛法(当然,用欧拉筛更好,更有一种无敌的感觉[doge]) 埃式筛法

int prime[maxn]; void init() //埃式筛法 { memset(prime,0,sizeof(prime)); //全部默认是素数,标记为0 prime[1]=1; for(int i=2; i<=sqrt(maxn); i++) { if(prime[i]==0) //素数为0 { for(int j=i+i; j<=maxn; j+=i) prime[j]=1; //所有的合数标记为1 } } }

想要打表的,就当没看见上面这部分吧…

其次,在BFS中的移动环节里,比如tmp=1033,对于它的每一位数,我们都需要进行改变然后将符合素数条件的数存入队列中。

但注意,后三位可以变成0~9中任意数字,第一位不能取0(前导0情况):

//next.x代表变化之后的数 //now.x代表当前数字 for(int j=0; j<4; j++) //j=0:处理个位 j=1:处理十位 j=2:处理百位 j=3:处理千位 { for(int i=0; i<=9; i++) { if(j==0)//个位 { next.x=now.x-now.x%10+i; } else if(j==1)//十位 { next.x=now.x+(i-now.x%100/10)*10; } else if(j==2)//百位 { next.x=now.x+(i-now.x%1000/100)*100; } else if(j==3&&i!=0)//千位,千位不能为0 { next.x=now.x+(i-now.x/1000)*1000; //这几个公式如果不理解可以自己手动算一下,比较简单 } if(prime[next.x]==0&&!vis[next.x]) //判断改变后的数是否是素数并且之前从未用过 { q.push(next); vis[next.x]=true; } } }

最后,就是整理一下出代码了,「伊丽莎白」! 代码

#define _CRT_SBCURE_NO_DEPRECATE #include <set> #include <cmath> #include <queue> #include <stack> #include <vector> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #include <functional> using namespace std; int n,a,b; //n:样例数 int prime[10004]; //这里我是直接搬的模板,将10000之内的数全部处理了 bool vis[10004]; struct node { int x; int step; }; void init() //埃式筛法 { prime[1]=1; for(int i=2; i<=sqrt(10000); i++) { if(prime[i]==0) { for(int j=i+i; j<=10000; j+=i) prime[j]=1; } } } void bfs() { node now,next; queue<node>q; now.x=a; now.step=0; q.push(now); vis[a]=true; while(!q.empty()) { now=q.front(); q.pop(); next.step=now.step+1; //无论如何操作,步数都+1 if(now.x==b) { printf("%d\n",now.step); return ; } for(int j=0; j<4; j++) //j=0:处理个位 j=1:处理十位 j=2:处理百位 j=3:处理千位 { for(int i=0; i<=9; i++) { if(j==0)//个位 { next.x=now.x-now.x%10+i; } else if(j==1)//十位 { next.x=now.x+(i-now.x%100/10)*10; } else if(j==2)//百位 { next.x=now.x+(i-now.x%1000/100)*100; } else if(j==3&&i!=0)//千位,千位不能为0 { next.x=now.x+(i-now.x/1000)*1000; //这几个公式如果不理解可以自己手动算一下,比较简单 } if(prime[next.x]==0&&!vis[next.x]) { q.push(next); vis[next.x]=true; } } } } printf("Impossible\n"); return; } int main() { memset(prime,0,sizeof(prime)); //全部初始化为0 init(); //预处理素数 scanf("%d",&n); while(n--) { memset(vis,false,sizeof(vis)); scanf("%d%d",&a,&b); if(a==b) //特判一下相等的时候 printf("0\n"); else bfs(); } return 0; }

记住我们的宗旨:一条路走到黑! “吾心吾行,澄如明镜;所作所为,皆属正义。”

溜了溜了~

最新回复(0)