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

    Prime Path
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14864   Accepted: 8379

    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
    比如从1033到8179,,,枚举每一个位数上(左数第1-4)改变后的每一个值,关键是要用到bfs的思想
    由斤及远,,在队列中最先找到的必定是最小的step
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    struct Node{
         int step;
         bool prime;
    }node[10001];
    int x[5];
    void  init()
    {
        for(int i=2;i<=9999;i++)
            node[i].prime=1;
         for(int j=2;j<=9999;j++)
                 if(node[j].prime)
                 {
                     for(int i=j*2;i<=9999;i+=j)
                         node[i].prime=0;
                 }
    }
    int bfs(int a,int b)
    {
        queue<int> q;
        q.push(a);
        int p;
        while(q.size())
        {
            p=q.front();
            q.pop();
            x[4]=p%10;
            x[3]=(p/10)%10;
            x[2]=(p/100)%10;
            x[1]=(p/1000)%10;//取个位,十位,百位,千位
            for(int i=1;i<=4;i++)
            {
               int k=x[i];
               for(int j=0;j<=9;j++)
            {
                if(i==1&&j==0)
                    continue;
                x[i]=j;
                int temp=x[1]*1000+x[2]*100+x[3]*10+x[4];
                if(node[temp].prime&&node[temp].step==0&&(temp!=p))//注意排除掉本身的值,否则                                                               会多加个一
                 {
                   node[temp].step=node[p].step+1;
                   if(temp==b)
                     {
                         printf("%d
    ",node[temp].step);
                         return 1;
                     }
                     q.push(temp);
                 }
            }
                x[i]=k;//记得最后要将x[i]的值复原,,不能干扰到下一个i的判断。
            }
        }
        printf("Impossible
    ");
        return 0;
    }
    void chu()
    {
        for(int i=1000;i<=9999;i++)
                 node[i].step=0;
    }
    int main()
    {
         int a,b,cas;
         init();
         cin>>cas;
         while(cas--)
         {
             chu();
             cin>>a>>b;
             if(a==b)
               printf("0
    ");
             else
             bfs(a,b);
         }
         return 0;
    }
    
  • 相关阅读:
    mexHttpBinding协议 【发布元数据终结点】
    Revit API创建一个拷贝房间内对象布局命令
    Revit API判断是不是柱族模板
    Revit API封装一个通用函数“过名称找元素”
    Revit手工创建族
    Revit API根据参数类型取得参数的值
    Revit Family API 创建参考平面
    Revit Family API 添加参数与尺寸标注
    osgearth earth文件规范-符号参考
    Revit API创建几何实体Solid并找到与之相交的元素
  • 原文地址:https://www.cnblogs.com/smilesundream/p/6642559.html
Copyright © 2011-2022 走看看