zoukankan      html  css  js  c++  java
  • POJ 3126 Prime Path

    Prime Path

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 7   Accepted Submission(s) : 3
    Problem 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
    31033 81791373 80171033 1033
     

    Sample Output
    670
     

    Source
    PKU
     
    一道简单的BFS+素数读表:

      1 #include <iostream>
      2 #include <cstring>
      3 #include <cmath>
      4 #include <queue>
      5  
      6 using namespace std;
      7  
      8 int isprime[10005];
      9  
     10 struct nnd
     11 {
     12     int first;
     13     int second;
     14 };
     15  
     16 int a,e;
     17 int vis[10005];
     18  
     19 void dfs(queue<nnd> q)
     20 {
     21     while(!q.empty())
     22     {
     23         nnd t;
     24         nnd r;
     25         t=q.front();
     26         q.pop();
     27         //cout<<e<<endl;
     28         if(t.first==e)
     29         {
     30             //cout<<"hello";
     31             cout<<t.second<<endl;
     32             return ;
     33         }
     34  
     35         int k=t.first%1000;
     36         for(int i=1000;i<10000;i=i+1000)
     37         {
     38             if(isprime[k+i]&&!vis[k+i])
     39             {
     40                r.first=k+i;   r.second=t.second+1;
     41                q.push(r);
     42                vis[i+k]=1;
     43             }
     44         }
     45  
     46         int kk=t.first%100;
     47         int kkk=t.first/1000*1000;
     48         k=kk+kkk;
     49         for(int i=0;i<1000;i=i+100)
     50         {
     51             if(isprime[i+k]&&!vis[i+k])
     52             {
     53                 r.first=i+k;
     54                 r.second=t.second+1;
     55                 q.push(r);
     56                 vis[i+k]=1;
     57             }
     58         }
     59  
     60         kk=t.first%10;
     61         kkk=t.first/100*100;
     62         k=kk+kkk;
     63         for(int i=0;i<100;i=i+10)
     64         {
     65             if(isprime[i+k]&&!vis[i+k])
     66             {
     67                 r.first=i+k;
     68                 r.second=t.second+1;
     69                 q.push(r);
     70                 vis[i+k]=1;
     71             }
     72         }
     73  
     74         k=t.first/10*10;
     75         for(int i=1;i<10;i=i+2)
     76         {
     77             if(isprime[i+k]&&!vis[i+k])
     78             {
     79                 r.first=i+k;
     80                 r.second=t.second+1;
     81                 q.push(r);
     82                 vis[i+k]=1;
     83             }
     84         }
     85     }
     86  
     87     cout<<"Impossible"<<endl;
     88     return;
     89 }
     90  
     91  
     92 int main()
     93 {
     94     int n;
     95     cin>>n;
     96  
     97     for(int i=0;i<10005;i++)
     98     {
     99         isprime[i]=i;
    100     }
    101     for(int i=2;i*i<10010;i++)
    102     {
    103         if(isprime[i]!=0)
    104         for(int j=2;j*i<10010;j++)
    105         {
    106             isprime[i*j]=0;
    107         }
    108     }
    109  
    110     isprime[1]=0;
    111  
    112  
    113     for(int o=0;o<n;o++)
    114     {
    115         queue<nnd> q;
    116         nnd a;
    117         memset(vis,0,sizeof(vis));
    118         cin>>a.first>>e;
    119         a.second=0;
    120         q.push(a);
    121         vis[a.first]=1;
    122  
    123         dfs(q);
    124     }
    125     return 0;
    126 }

  • 相关阅读:
    pthread_rwlock_rdlock和“No such file or directory”
    Thrift线程和状态机分析
    Haodoop RPC解析
    Thrift结构分析及增加取客户端IP功能实现
    StarUML 5.0问题解决:Failed to open the model file. Invalid file format.
    检测Linux系统是否支持某系统调用
    eclipse的thrift插件
    常见gcc编译问题解决方法集
    Thrift编译错误('::malloc' has not been declared)
    安装Android SDK Manager的“Failed to fetch refused”问题解决方法
  • 原文地址:https://www.cnblogs.com/CKboss/p/3013427.html
Copyright © 2011-2022 走看看