• poj 3126 Prime Path


    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 16198 Accepted: 9148
    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
    Source
    Northwestern Europe 2006
    题目大意:
    给定两个四位素数a b,要求把a变换到b
    变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能重复。
    求从a到b最少需要的变换次数。无法变换则输出Impossible

    /*
    埃式筛法素数判定+BFS+可行解判定+乱搞.
    我们先搞出[1000,10000]的prime.
    然后带入prime判是否可行(只有一位不同)
    用BFS跑最优解(神奇的BFS).
    */
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #define MAXN 10001
    using namespace std;
    int n,m,t,s[MAXN],tot,head,tail;
    bool b[MAXN];
    struct data
    {
        int step;
        int v;
    }
    q[MAXN];
    void prime()
    {
        for(int i=2;i<=10000;i++)
          if(!b[i])
          {
            for(int j=i+i;j<=10000;j+=i)
                b[j]=true;
          }
        for(int i=1001;i<=10000;i++)
          if(!b[i])
              s[++tot]=i;
    }
    bool jd(int n,int m)
    {
        int sum=0;
        while(n)
        {
            int i=n%10;n/=10;
            int j=m%10;m/=10;
            if(i!=j) sum++;
        }
        if(sum==1) return true;
        return false;
    }
    bool bfs(int n,int m)
    {
        head=0,tail=1;
        q[tail].step=0;
        q[tail].v=n;
        b[n]=true;
        while(head<tail)
        {
            head++;
            if(q[head].v==m) return true;
            for(int i=tot;i>=1;i--)
            {
                if(!b[s[i]]&&jd(q[head].v,s[i]))
                {
                    b[s[i]]=true;
                    q[++tail].v=s[i];
                    q[tail].step=q[head].step+1;
                }
            }
        }
        return false;
    }
    int main()
    {
        prime();
        cin>>t;
        while(t--)
        {
            memset(b,0,sizeof(b));
            scanf("%d %d",&n,&m);
            if(bfs(n,m))
            {
                printf("%d
    ",q[head].step);
            }
            else  printf("Impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    录制caf 转 mp3
    关于百度地图iOS中 paopaoView 警告的处理方法
    iphone JB开发小记(四)theos、iosOpenDev的调试
    USB 漏洞影响超100万来自不同供应商的路由器
    进程 线程 纤程 中断
    synchronized、ReentrantLock、volatile
    TimeUnit用法
    2021年vivo互联网技术最受欢迎文章TOP25
    前端质量提升利器马可代码覆盖率平台
    zyh@163.net
  • 原文地址:https://www.cnblogs.com/nancheng58/p/6070833.html
Copyright © 2020-2023  润新知