题目描述

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。

至于为什么用广搜,是因为他需要求最少的变化次数,所以搜索方法运用了广搜

输入

3
1033 8179
1373 8017
1033 1033

输出

6
7
0

贴一波代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <string>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#define maxn 1000000
const int MaxN=0x3f3f3f3f;
const int MinN=0xc0c0c00c;
typedef long long ll;
using namespace std;
bool isprim[100010];
void prim(){
    isprim[1]=false;
    for(int i=2;i<=100010;i++)
        if(isprim[i])
        for(int f=2*i;f<=100010;f=f+i)
        isprim[f]=false;
}
int num[10];
int x,y;
bool visited[100010];
int ans=0;
struct wazxy{
    int x;
    int steps;
}node,temp;

queue<wazxy>  q;

void bfs(){
    node.x=x,node.steps=0;
    q.push(node);
    while(!q.empty()){
        temp=q.front();
        q.pop();
        if(temp.x==y){
            ans=temp.steps;
            return ;
        }
        else{
            int ii=0;
            while(temp.x){
                num[++ii]=temp.x%10;
                temp.x/=10;
            }

// for(int i=1;i<=4;i++){
// cout<<num[i]<<" ";
// }
// cout<<endl;

            for(int i=1;i<=4;i++){  //分别用来分成改变个位十位百位千位的数字时
                for(int f=0;f<=9;f++){
                    int now;
                    if(i==1){
                        if(f==num[1])  continue;
                        now=num[4]*1000+num[3]*100+num[2]*10+f;//更改某位的数字,先将他拆开改变之后再合起来。
                        if(isprim[now]&&visited[now]){  //我这个visited用来去重,防止变成某数又在变回去,而且一定要确定他是个素数
                            node.steps=temp.steps+1;
                            node.x=now;
                            visited[now]=false;  //标记
                            q.push(node);
                        }
                    }
                    if(i==2){
                        if(f==num[2])  continue;
                        now=num[4]*1000+num[3]*100+f*10+num[1];
                        if(isprim[now]&&visited[now]){
                            node.steps=temp.steps+1;
                            node.x=now;
                            visited[now]=false;
                            q.push(node);
                        }
                    }
                    if(i==3){
                        if(f==num[3])  continue;
                        now=num[4]*1000+f*100+num[2]*10+num[1];
                        if(isprim[now]&&visited[now]){
                            node.steps=temp.steps+1;
                            node.x=now;
                            visited[now]=false;
                            q.push(node);
                        }
                    }
                    if(i==4){
                        if(f==0||f==num[4])  continue;//注意改变千位数字是不能为0
                        now=f*1000+num[3]*100+num[2]*10+num[1];
                        if(isprim[now]&&visited[now]){
                            node.steps=temp.steps+1;
                            node.x=now;
                            visited[now]=false;
                            q.push(node);
                        }
                    }
                }
            }
        }
    }
}


int main()
{
   memset(isprim,true,sizeof(isprim));
   prim();
   //for(int i=0;i<=100;i++) if(isprim[i]) cout<<i<<" ";
   int t;
   cin>>t;
   for(int i=0;i<t;i++){
        memset(visited,true,sizeof(visited));
        cin>>x>>y;
        visited[x]=false;
        while(!q.empty())  q.pop();  //将队列清空!!!!!!!(之前一个题没有清空找了好久)
        ans=0;
        bfs();
        cout<<ans<<endl;

   }

}