• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 3,  comments - 1,  trackbacks - 0
            第N道的廣搜,這幾天就準備做廣搜了...真的需要好好練習下...



            Prime Path
            Time Limit: 1000MS Memory Limit: 65536K
            Total Submissions: 1877 Accepted: 1215

            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
            這題主要思路就是對每一位進行0-9的改變..第一位不能位0...得到的新數入隊...循環直到找到Y


            Source Code

            Problem: 3126 User: luoguangyao
            Memory: 344K Time: 47MS
            Language: C++ Result: Accepted
            • Source Code
            •   1#include <iostream>
                2#include <math.h>
                3#include <queue>
                4#include <stdio.h>
                5
                6using namespace::std;
                7
                8int cnumber[10000];
                9
               10bool mark[10000];
               11
               12
               13bool isprimer(int npp)
               14{
               15    for (int q = 2; q <= sqrt(double(npp)); ++q)
               16    {
               17        if (npp % q == 0)
               18        {
               19            return 0;
               20        }

               21    }

               22    return 1;
               23
               24}

               25
               26
               27int main()
               28{
               29
               30//    freopen("1.txt","r",stdin);
               31
               32    int n;
               33    cin >> n;
               34
               35    while (n)
               36    {
               37        queue<int> number;
               38        int x;
               39        int y;
               40        int i;
               41        int j;
               42        int newnumber;
               43
               44        memset(cnumber,0,sizeof(cnumber));
               45        memset(mark,0,sizeof(mark));
               46        
               47        cin >> x >> y;
               48
               49
               50        cnumber[x] = 0;
               51
               52        number.push(x);
               53
               54        while (number.size())
               55        {
               56            newnumber = number.front();
               57
               58            int p = (newnumber % 1000/ 100;
               59
               60            int p1 = newnumber % 1000 % 100 / 10;
               61
               62            number.pop();
               63
               64            mark[newnumber] = 1;
               65    
               66            if (newnumber == y)
               67            {
               68                break;
               69            }

               70
               71            int num;
               72            
               73            for (i = 0; i <= 9++i)
               74            {
               75                num = newnumber % 1000 + i * 1000;
               76
               77                
               78                if (i != 0 && mark[num] != 1 && isprimer(num))
               79                {
               80                    number.push(num);
               81                    cnumber[num] = cnumber[newnumber] + 1;  
               82                    mark[num] = 1;
               83                }

               84        
               85                int num1;
               86                num1 = newnumber - p * 100 + i * 100;
               87
               88
               89                if (mark[num1] != 1 && isprimer(num1))
               90                {
               91
               92                    number.push(num1);
               93                    cnumber[num1] = cnumber[newnumber] + 1
               94                    mark[num1] = 1;
               95                }

               96                int num2;
               97                num2 = newnumber - p1 * 10 + i * 10;
               98
               99
              100                if (mark[num2] != 1 &&  isprimer(num2))
              101                {
              102                    number.push(num2);
              103                    cnumber[num2] = cnumber[newnumber] + 1
              104                    mark[num2] = 1;
              105                }

              106
              107                int num3 = newnumber / 10 * 10 + i;
              108    
              109                if (mark[num3] != 1 &&  isprimer(num3))
              110                {
              111                    number.push(num3);
              112                    cnumber[num3] = cnumber[newnumber] + 1
              113                    mark[num3] = 1;
              114                }

              115            }

              116            
              117        }

              118
              119        cout << cnumber[y] << endl;
              120
              121        n--;
              122    }

              123
              124    return 0;
              125}

              126
            posted on 2009-03-08 11:23 生活要低調 閱讀(1446) 評論(1)  編輯 收藏 引用

            FeedBack:
            # re: POJ 3126 學會廣搜隊列的用法
            2009-03-08 20:25 | cppexplore
            removed by cppexplore  回復  更多評論
              
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久99精品免费一区二区| 久久婷婷五月综合97色直播| 亚洲va久久久噜噜噜久久| 久久精品国产日本波多野结衣| 国内精品久久久久影院亚洲| 日韩十八禁一区二区久久| 欧美亚洲国产精品久久高清| 精品无码久久久久久午夜| 99久久精品费精品国产| 亚洲美日韩Av中文字幕无码久久久妻妇 | 亚洲精品无码久久久影院相关影片| 思思久久精品在热线热| 久久久久国产精品熟女影院| 88久久精品无码一区二区毛片| 99久久综合国产精品免费| 97久久超碰成人精品网站| 久久高清一级毛片| 嫩草伊人久久精品少妇AV| 久久精品国产第一区二区| 久久棈精品久久久久久噜噜| 99久久99久久精品国产| 久久ww精品w免费人成| 日本久久中文字幕| 久久精品国产影库免费看| 久久99精品国产麻豆宅宅| 久久99精品久久久久久野外| 久久久久免费精品国产| 亚洲va久久久噜噜噜久久天堂 | 久久婷婷人人澡人人爽人人爱 | 亚洲国产日韩欧美久久| 国产成人久久精品区一区二区| 伊人久久无码精品中文字幕| 国产亚州精品女人久久久久久 | 国产精品一区二区久久精品涩爱| 久久er国产精品免费观看2| 色欲综合久久躁天天躁蜜桃| 久久国产AVJUST麻豆| 久久久久国产精品三级网| 99久久伊人精品综合观看| 亚洲午夜久久久影院伊人| 欧美激情精品久久久久久久九九九|