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

    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

    1A~~爽

    题意:就是给你两个素数只有四位的a,b,问a如何转换能到达b,转换规则是只能转换一个数字的一位,且每次转换后该数依旧是素数

    思路:将1000-10000的素数打表,然后BFS,一个一个的枚举。

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    const int MAXN=10000;
    int vis1[MAXN],vis2[MAXN];
    
    void init()
    {
        int i,j;
        memset(vis1,0,sizeof(vis1));
        for(i=2;i<MAXN;i++)
        {
            if(!vis1[i])
            {
                for(j=i*2;j<MAXN;j+=i)
                  vis1[j]=1;
            }
        }
    }
    
    int BFS(int a,int b)
    {
        int step[MAXN];
        int s,head,next;
        memset(step,0,sizeof(step));
        memset(vis2,0,sizeof(vis2));
        queue<int>Q;
        Q.push(a);
        vis2[a]=1;
        while(!Q.empty())
        {
            head=Q.front();
            Q.pop();
            if(head==b) return step[head];
            for(int i=1;i<=4;i++)
            {
                if(i==1) s=1;
                else s=0;
                for(int j=s;j<=9;j++)
                {
                    if(i==1) next=j*1000+head%1000;
                    else if(i==2) next=head/1000*1000+j*100+head%100;
                    else if(i==3) next=head/100*100+j*10+head%10;
                    else next=head/10*10+j;
                    if(vis1[next]==1) continue;
                    if(!vis2[next])
                    {
                        step[next]=step[head]+1;
                        if(next==b) return step[next];
                        vis2[next]=1;
                        Q.push(next);
                    }
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        int T,a,b;
        init();
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&a,&b);
            int ans=BFS(a,b);
            if(ans>=0) printf("%d\n",ans);
            else printf("Impossible\n");
        }
        return 0;
    }
  • 相关阅读:
    fedora20安装hadoop-2.5.1
    超简单fedora20(linux)下JDK1.8的安装
    解决A program file was not specified in the launch configuration.问题
    java中的四则运算
    spring util命名空间
    java中常用的数据加密算法
    C语言实现栈
    百度ueditor富文本编辑器的使用
    C#进程管理程序实现
    ABP领域层-仓储
  • 原文地址:https://www.cnblogs.com/zsboy/p/2646519.html
Copyright © 2011-2022 走看看