Prime Path 筛法打表+记忆化搜索+BFS

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去寻找最短步数 数字是固定的四位数 所以很容易想到改变每一位数 再说对于素数的处理 因为每个 if 都要去判断素数 所以打表的效率显然是最高的(虽然浪费了好多内存 但禁不住快啊。。) 最后一个需要注意的地方就是记忆化搜索 也可以说是剪枝 如果不去把已经遍历过的数字去掉 那复杂度就太高了

以下是ac代码:

#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
int m,n,sum;
int flag[10000+10];
int vis[10000+10];
//char b[5];
struct node
{
    int a;
    int steps;
};
queue<struct node> que;
//char * swap(int a)   
//{
//    int ans = 3;
//    while(a>0)
//    {
//        b[ans--] = a%10+'0';
//        a/=10;
//    }
//    b[4] = '\0';
//    return b;
//}
//int swapbb(char * b)
//{
//    int flag = 1;
//    flag = (b[0]-'0')*1000+(b[1]-'0')*100+(b[2]-'0')*10+(b[3]-'0');
//    return flag;
//}
void bfs()
{
    while(que.size())
    {
        struct node dd;
        dd = que.front();
        que.pop();
        if(dd.a == n) 
        {
            cout << dd.steps << endl;
            return ;
        }
        for(int i = 1 ; i<=9 ; i++)
        {
//            int fla = dd.a;
//            char *b = swap(fla);
//            b[0]+=i;
//            fla = swapbb(b);
            int fla = dd.a / 10 * 10 + i;
            if(flag[fla]==0 && vis[fla]==0 && fla!=dd.a)
            {
            	vis[fla] = 1;
                struct node mm = {fla,dd.steps+1};
                que.push(mm);
            }
        }
                for(int i = 0 ; i<=9 ; i++)
        {
//            int fla = dd.a;
//            char *b = swap(fla);
//            b[1]+=i;
//            fla = swapbb(b);
			int fla = dd.a / 100 * 100 + i * 10 + dd.a % 10;
            if(flag[fla]==0 && vis[fla]==0 && fla!=dd.a)
            {
            	vis[fla] = 1;
                struct node mm = {fla,dd.steps+1};
                que.push(mm);
            }
        }
                for(int i = 0 ; i<=9 ; i++)
        {
//            int fla = dd.a;
//            char *b = swap(fla);
//            b[2]+=i;
//            fla = swapbb(b);
			int fla = dd.a / 1000 * 1000 + i * 100 + dd.a % 100;
            if(flag[fla]==0 && vis[fla]==0 && fla!=dd.a)
            {
            	vis[fla] = 1;
                struct node mm = {fla,dd.steps+1};
                que.push(mm);
            }
        }
                for(int i = 1 ; i<=9 ; i++)
        {
//            int fla = dd.a;
//            char *b = swap(fla);
//            b[3]+=i;
//            fla = swapbb(b);		
            int fla = i * 1000 + dd.a % 1000;
            if(flag[fla]==0 && vis[fla]==0 && fla!=dd.a)
            {
            	vis[fla] = 1;
                struct node mm = {fla,dd.steps+1};
                que.push(mm);
            }
        }
    }
    cout << "Impossible\n";
}
int main()
{
    cin >> sum;
    flag[0] = 1,flag[1] = 1;
    for(int i = 2 ; i <= sqrt(double(10000));++i)
    {
        if(flag[i] == 0)
        for(int j = i*i ; j < 10000 ; j += i)
            flag[j] = 1;
    }
    while(sum--)
    {
    	while(!que.empty()) que.pop();
        cin >> m >> n;
        memset(vis,0,sizeof(vis));
        vis[m] = 1;
        struct node tmp = {m,0};
    	que.push(tmp);
        bfs();
    }
    return 0;
}

注释过的地方是我第一次写的时候为了改变每位数字的值而写的函数 这样导致每次判断都会引用两个函数 看到别人的写法瞬间清醒 一行代码的事何必用函数呢,

猜你喜欢

转载自blog.csdn.net/weixin_43705457/article/details/89036963