zoukankan      html  css  js  c++  java
  • POJ

    先上题目

    Prime Path
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9259   Accepted: 5274

    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

      题意比较简单,就是给你两个4位的素数,问你能不能经过有限的步骤将第一个素数转化为第二个素数,其中转化的要符合一下的要求:每一次只可以改变这个四位数的某一位,首位不能为0,每一次转化以后得到的数要还是素数。如果不可以转化得到目标素数,输出Impossible。
      做法也是比较简单,先筛出10000以内的素数出来,然后枚举当前的这个素数可以转化的其他素数,然后进行bfs,当遇到目标素数的时候就输出步数;如果转化不了就会把可以转化的素数都转化了一遍,最后循环就会结束。这里为了能够判断是否能转化,需要标记它转化过哪些素数,转化过的素数如果再出现,就不需要再搜索这个数了。



    上代码:

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <queue>
      4 #include <map>
      5 #define LL long long
      6 #define MAX (10000+10)
      7 using namespace std;
      8 
      9 bool f[MAX],M[MAX];
     10 
     11 typedef struct
     12 {
     13     int l;
     14     int st;
     15 }S;
     16 queue<S> q;
     17 
     18 void dedeal()
     19 {
     20     LL i,j,n;
     21     n=MAX;
     22     for(i=2;i<=n;i++)
     23     {
     24         if(!f[i])
     25         {
     26             for(j=i*i;j<=n;j+=i)  f[j]=1;
     27         }
     28     }
     29 }
     30 
     31 int check(int x,int y)
     32 {
     33     int t,i;
     34     S d;
     35     d.l=x;
     36     d.st=0;
     37     memset(M,0,sizeof(M));
     38     while(!q.empty()) q.pop();
     39     q.push(d);
     40     M[d.l]=1;
     41     while(!q.empty())
     42     {
     43         d=q.front();
     44         q.pop();
     45         if(d.l==y) return d.st;
     46         d.st++;
     47         x=d.l;
     48         t=x/10*10;
     49         for(i=0;i<10;i++)
     50         {
     51             d.l=t+i;
     52             if(!f[d.l] && d.l!=x && !M[d.l])
     53             {
     54                 M[d.l]=1;
     55                 q.push(d);
     56             }
     57         }
     58         t=x/100*100+x%10;
     59         for(i=0;i<100;i+=10)
     60         {
     61             d.l=t+i;
     62             if(!f[d.l] && d.l!=x && !M[d.l])
     63             {
     64                 M[d.l]=1;
     65                 q.push(d);
     66             }
     67         }
     68         t=x/1000*1000+x%100;
     69         for(i=0;i<1000;i+=100)
     70         {
     71             d.l=t+i;
     72             if(!f[d.l] && d.l!=x && !M[d.l])
     73             {
     74                 M[d.l]=1;
     75                 q.push(d);
     76             }
     77         }
     78         t=x%1000;
     79         for(i=1000;i<10000;i+=1000)
     80         {
     81             d.l=t+i;
     82             if(!f[d.l] && d.l!=x && !M[d.l])
     83             {
     84                 M[d.l]=1;
     85                 q.push(d);
     86             }
     87         }
     88     }
     89     return -1;
     90 }
     91 
     92 int main()
     93 {
     94     int n,c,x,y;
     95     //freopen("data.txt","r",stdin);
     96     dedeal();
     97     scanf("%d",&n);
     98     while(n--)
     99     {
    100         scanf("%d %d",&x,&y);
    101         c=check(x,y);
    102         if(c==-1) printf("Impossible
    ");
    103         else printf("%d
    ",c);
    104     }
    105     return 0;
    106 }
    3126
  • 相关阅读:
    Linux文件系统属性权限chattr与lsattr命令
    Linux权限管理之ACL权限
    慈悲
    存在的四个象限
    白细胞低的原因
    释放能量和注意力
    [C#] 走进异步编程的世界
    改变世界的工程师:荣耀背后深刻着孤独
    分享基于EF+MVC+Bootstrap的通用后台管理系统及架构(转)
    如何拿到半数面试公司Offer——我的Python求职之路(转)
  • 原文地址:https://www.cnblogs.com/sineatos/p/3231109.html
Copyright © 2011-2022 走看看