zoukankan      html  css  js  c++  java
  • poj 3126 Prime Path (BFS)

    Description

    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

    大致题意:

    给定两个四位素数a  b,要求把a变换到b

    变换的过程要保证  每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数  与  前一步得到的素数  只能有一个位不同,而且每步得到的素数都不能重复。

     求从a到b最少需要的变换次数。无法变换则输出Impossible

    大致思路就是BFS+素数判定,还要注意千位的数不得为零,递交为G++,C++不知为何会编译错误

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 using namespace std;
     7 const int maxn=10000;
     8 struct node
     9 {
    10     int num;
    11     int step;
    12 };
    13 bool prime[maxn],vis[maxn];
    14 void fun_prime()
    15 {
    16     prime[0]=prime[1]=false;
    17     for(int i=2; i*i<maxn; i++)
    18     {
    19         if(prime[i])
    20         {
    21             for(int j=i+i; j<maxn; j+=i)
    22                 prime[j]=false;
    23         }
    24     }
    25 }
    26 void bfs(int a,int b)
    27 {
    28     int sz[10]={1,10,100,1000};
    29     if(a==b)
    30     {
    31         cout<<0<<endl;
    32         return ;
    33     }
    34     memset(vis,false,sizeof(vis));
    35     queue<node >q;
    36     vis[a]=true;
    37     q.push((node)
    38     {
    39         a,0
    40     });
    41     while(!q.empty())
    42     {
    43         node head=q.front();
    44         q.pop();
    45         for(int i=0; i<4; i++)
    46         {
    47             for(int j=0; j<10; j++) 
    48             {
    49                 if(i==3 && j==0)  //千位不得为零
    50                     continue;
    51                 int t=head.num%sz[i]+j*sz[i]+head.num/(sz[i]*10)*(sz[i]*10);
    52                 if(t==b)
    53                 {
    54                     cout<<head.step+1<<endl;
    55                     return ;
    56                 }
    57                 if(prime[t]&&!vis[t])
    58                 {
    59                     vis[t]=true;
    60                     q.push((node)
    61                     {
    62                         t,head.step+1
    63                     });
    64                 }
    65 
    66             }
    67         }
    68     }
    69     cout<<"Impossible"<<endl;
    70 }
    71 int main()
    72 {
    73     int t,a,b;
    74     cin>>t;
    75     memset(prime,true,sizeof(prime));
    76     fun_prime();
    77     while(t--)
    78     {
    79         cin>>a>>b;
    80         bfs(a,b);
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    【zookeeper】
    关于redis-windows环境下的一些配置:
    mybatis-注解开发
    jQuery的Validate插件
    Thymeleaf 学习笔记-实例demo(中文教程)
    thymeleaf 学习笔记-基础篇(中文教程)
    AGC 043C
    JOISC 2020 部分题解
    Loj #2687
    CF 1270I
  • 原文地址:https://www.cnblogs.com/cxbky/p/4892811.html
Copyright © 2011-2022 走看看