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
简单的bfs+素数筛法。
这里使用的是埃式筛法来构建素数表isprime
- isprime[i]=true, i为素数
- isprime[i]=false, i不是素数
然后分个位、十位、百位、千位入口,个位可以排除偶数(包括0),千位的变化范围是[1,9](不能取0)。
值得注意的是,这里没有使用stl中的queue容器,而是用数组自定义了一个线性队列。
其原因引用dalao原话:
你如果不了解STL的内部实现逻辑或者没看过代码,还是少用,因为使用不当就会拖慢程序效率的。STL的queue是在deque基础上封装的,而deque维护的是一个双端队列。换而言之,queue的数据结构决定了,其入队和出队操作的时间复杂度都是0(n),而不是你以为的O(1)。随着队列元素的增长,queue的出入队效率越差。
代码样例
//F-Prime Path
#include<iostream>
#include<cstring>
using namespace std;
#define MAXN 10005
int a,b;
bool isprime[MAXN];
bool vist[MAXN];
struct node{
int prime, step;
};
void init(){
memset(isprime, true, sizeof(isprime));
for(int i=2; i*i<MAXN; i++){
if(isprime[i]){
for(int j=i*i; j<MAXN; j+=i){
isprime[j]=false;
}
}
}
}
void bfs(){
int head, tail;
head=tail=0;
node Q[MAXN];
Q[tail].prime=a;
Q[tail].step=0;
vist[a]=true;
tail++;
while(tail>head){
node temp=Q[head];
head++;
if(temp.prime==b){
cout<<temp.step<<endl;
return;
}
int ones=temp.prime%10;
int tens=(temp.prime/10)%10;
for(int i=1; i<10; i+=2){
int test_ones=(temp.prime/10)*10+i;
if(!isprime[test_ones]||vist[test_ones]){
continue;
}
vist[test_ones]=true;
Q[tail].prime=test_ones;
Q[tail].step=temp.step+1;
tail++;
}
for(int i=0; i<10; i++){
int test_tens=(temp.prime/100)*100+10*i+ones;
if(!isprime[test_tens]||vist[test_tens]){
continue;
}
vist[test_tens]=true;
Q[tail].prime=test_tens;
Q[tail].step=temp.step+1;
tail++;
}
for(int i=0; i<10; i++){
int test_hundreds=(temp.prime/1000)*1000+100*i+10*tens+ones;
if(!isprime[test_hundreds]||vist[test_hundreds]){
continue;
}
vist[test_hundreds]=true;
Q[tail].prime=test_hundreds;
Q[tail].step=temp.step+1;
tail++;
}
for(int i=1; i<10; i++){
int test_thousands=temp.prime%1000+1000*i;
if(!isprime[test_thousands]||vist[test_thousands]){
continue;
}
vist[test_thousands]=true;
Q[tail].prime=test_thousands;
Q[tail].step=temp.step+1;
tail++;
}
}
cout<<"Imopssible"<<endl;
return;
}
int main(void){
init();
int n;
cin>>n;
for(int i=0; i<n; i++){
cin>>a>>b;
memset(vist, false, sizeof(vist));
bfs();
}
return 0;
}
代码参考至http://exp-blog.com/2018/06/23/pid-818/
是个真大佬。
- 1605集训计划第6天任务
- 按时完成