• POJ 3126


    Prime Path

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 7   Accepted Submission(s) : 3
    Problem Description
    POJ 3126 - qhn999 - 码代码的猿猿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
     







    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <queue>

    using namespace std;

    int isprime[10005];

    struct nnd
    {
        int first;
        int second;
    };

    int a,e;
    int vis[10005];

    void dfs(queue<nnd> q)
    {
        while(!q.empty())
        {
            nnd t;
            nnd r;
            t=q.front();
            q.pop();
            //cout<<e<<endl;
            if(t.first==e)
            {
                //cout<<"hello";
                cout<<t.second<<endl;
                return ;
            }

            int k=t.first%1000;
            for(int i=1000;i<10000;i=i+1000)
            {
                if(isprime[k+i]&&!vis[k+i])
                {
                   r.first=k+i;   r.second=t.second+1;
                   q.push(r);
                   vis[i+k]=1;
                }
            }

            int kk=t.first%100;
            int kkk=t.first/1000*1000;
            k=kk+kkk;
            for(int i=0;i<1000;i=i+100)
            {
                if(isprime[i+k]&&!vis[i+k])
                {
                    r.first=i+k;
                    r.second=t.second+1;
                    q.push(r);
                    vis[i+k]=1;
                }
            }

            kk=t.first%10;
            kkk=t.first/100*100;
            k=kk+kkk;
            for(int i=0;i<100;i=i+10)
            {
                if(isprime[i+k]&&!vis[i+k])
                {
                    r.first=i+k;
                    r.second=t.second+1;
                    q.push(r);
                    vis[i+k]=1;
                }
            }

            k=t.first/10*10;
            for(int i=1;i<10;i=i+2)
            {
                if(isprime[i+k]&&!vis[i+k])
                {
                    r.first=i+k;
                    r.second=t.second+1;
                    q.push(r);
                    vis[i+k]=1;
                }
            }
        }

        cout<<"Impossible"<<endl;
        return;
    }


    int main()
    {
        int n;
        cin>>n;

        for(int i=0;i<10005;i++)
        {
            isprime=i;
        }
        for(int i=2;i*i<10010;i++)
        {
            if(isprime!=0)
            for(int j=2;j*i<10010;j++)
            {
                isprime[i*j]=0;
            }
        }

        isprime[1]=0;


        for(int o=0;o<n;o++)
        {
            queue<nnd> q;
            nnd a;
            memset(vis,0,sizeof(vis));
            cin>>a.first>>e;
            a.second=0;
            q.push(a);
            vis[a.first]=1;

            dfs(q);
        }
        return 0;
    }

  • 相关阅读:
    Python基本数据类型
    Python内存相关
    Python运算符和编码
    js比较日期大小 判断日期
    js判断一个数是不是正整数
    sql查询排序
    js获取select标签选中的值
    PL/sql配置相关
    搜狗的好玩用法
    Oracle数据库中的dual表
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351103.html
Copyright © 2020-2023  润新知