• <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>

            Problem description

            Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
            Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.

            Now your task is to use minimal steps to open the lock.

            Note: The leftmost digit is not the neighbor of the rightmost digit.


            ?
            Input
            The input file begins with an integer T, indicating the number of test cases.

            Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.


            ?
            Output
            For each test case, print the minimal steps in one line.


            Sample Input
            2
            1234
            2144

            1111
            9999
            ?
            Sample Output
            2
            4




            廣度搜索:

            ??1 #include? < iostream >
            ??2 #include? < queue >
            ??3 #include? < algorithm >
            ??4
            ??5 using ? namespace ?std;
            ??6
            ??7 bool ??mark[ 10000 ];
            ??8
            ??9 struct ?Node
            ?10 {
            ?11 ???? int ???step;
            ?12 ???? char ??state[ 5 ];
            ?13
            ?14 ????Node()
            ?15 ???? {}
            ?16
            ?17 ????Node(? int ?i,? char * ?str?)
            ?18 ????????:step(i)
            ?19 ???? {
            ?20 ????????strcpy(?state,?str?);
            ?21 ????}

            ?22 }
            ;
            ?23
            ?24
            ?25 int ?getn(? char ?str[ 5 ]?)
            ?26 {
            ?27 ???? return ?(str[ 0 ] - ? ' 0 ' ) * ? 1000 + ?(?str[ 1 ] - ? ' 0 ' ?) * ? 100 + ?(?str[ 2 ] - ? ' 0 ' ?) * ? 10 + ?(str[ 3 ] - ? ' 0 ' ?);
            ?28 }

            ?29
            ?30
            ?31 int ?main()
            ?32 {
            ?33 ???? char ????initial[ 5 ];
            ?34 ???? char ????open[ 5 ];
            ?35 ???? int ?????Cases;
            ?36
            ?37 ????scanf( " %d " , & Cases);
            ?38 ???? while ?(?Cases -- ?)
            ?39 ???? {
            ?40 ????????memset(?mark,? false ,? sizeof (mark)?);
            ?41
            ?42 ????????queue < struct ?Node > ??q;
            ?43 ????????scanf( " %s%s " ,initial,open);
            ?44
            ?45 ????????q.push?(?Node( 0 ,initial)?);
            ?46 ????????mark[?getn(initial)?] = ? true ;
            ?47
            ?48 ???????? while ?(? ! q.empty?()?)
            ?49 ???????? {
            ?50 ???????????? struct ?Node?head = ?q.front?();
            ?51 ????????????q.pop?();
            ?52
            ?53 ???????????? if ?(?strcmp(?head.state?,?open) == ? 0 ?)
            ?54 ???????????? {
            ?55 ????????????????printf( " %d " ,head.step?);
            ?56 ???????????????? break ;
            ?57 ????????????}

            ?58
            ?59 ???????????? for ?(? int ?i = ? 0 ;?i < ? 3 ;? ++ i?)
            ?60 ???????????? {????????
            ?61 ???????????????? char ?temp[ 5 ];
            ?62 ????????????????strcpy(?temp,?head.state?);
            ?63
            ?64 ????????????????std::swap?(?temp[i],?temp[i + 1 ]?);
            ?65 ???????????????? if ?(? ! mark[?getn(temp)?]?)
            ?66 ???????????????? {
            ?67 ????????????????????q.push?(?Node(head.step + ? 1 ,?temp?)?);
            ?68 ????????????????????mark[?getn(temp)?] = ? true ;
            ?69 ????????????????}

            ?70 ????????????}

            ?71
            ?72 ???????????? for ?(? int ?i = ? 0 ;?i < ? 4 ;? ++ i?)
            ?73 ???????????? {
            ?74 ???????????????? char ?temp[ 5 ];
            ?75 ????????????????strcpy(?temp,?head.state?);
            ?76
            ?77 ???????????????? if ?(?temp[i] == ? ' 9 ' ?)?temp[i] = ? ' 1 ' ;
            ?78 ???????????????? else ?temp[i] = ?temp[i] + ? 1 ;
            ?79
            ?80 ???????????????? int ?n = ?getn(temp);
            ?81 ???????????????? if ?(? ! mark[n]?)
            ?82 ???????????????? {
            ?83 ????????????????????q.push?(?Node(?head.step + ? 1 ,?temp?)?);
            ?84 ????????????????????mark[n] = ? true ;
            ?85 ????????????????}

            ?86 ????????????}

            ?87
            ?88 ???????????? for ?(? int ?i = ? 0 ;?i < ? 4 ;? ++ i?)
            ?89 ???????????? {
            ?90 ???????????????? char ?temp[ 5 ];
            ?91 ????????????????strcpy(?temp,?head.state?);
            ?92
            ?93 ???????????????? if ?(?temp[i] == ? ' 1 ' ?)?temp[i] = ? ' 9 ' ;
            ?94 ???????????????? else ?temp[i] = ?temp[i] - ? 1 ;
            ?95
            ?96 ???????????????? int ?n = ?getn(temp);
            ?97 ???????????????? if ?(? ! mark[n]?)
            ?98 ???????????????? {
            ?99 ????????????????????q.push?(?Node(?head.step + ? 1 ,?temp?)?);
            100 ????????????????????mark[n] = ? true ;
            101 ????????????????}

            102 ????????????}

            103 ????????}

            104
            105 ???????? if ?(?Cases?)?printf( " \n " );
            106 ????}

            107
            108 ???? return ? 0 ;
            109 }

            110
            posted on 2008-08-19 16:38 Darren 閱讀(424) 評論(0)  編輯 收藏 引用 所屬分類: 搜索
            伊人热热久久原色播放www| 国产精久久一区二区三区| 欧美与黑人午夜性猛交久久久 | 日韩va亚洲va欧美va久久| 要久久爱在线免费观看| 久久久久久夜精品精品免费啦| 色综合合久久天天综合绕视看| 精品久久久久久无码中文野结衣| 久久久精品无码专区不卡| 亚洲va久久久噜噜噜久久| 99久久国产综合精品五月天喷水 | 亚洲精品乱码久久久久久按摩| 97久久综合精品久久久综合| 亚洲一级Av无码毛片久久精品| 人妻无码久久一区二区三区免费| 伊人色综合久久| 成人久久久观看免费毛片 | 精品熟女少妇a∨免费久久| 精品久久久久久国产牛牛app| 亚洲精品乱码久久久久久中文字幕 | 精品久久久久久中文字幕大豆网| 色综合久久综精品| 亚洲色婷婷综合久久| 亚洲国产精品嫩草影院久久 | 91麻豆国产精品91久久久| 精品国产一区二区三区久久| 亚洲AV无码1区2区久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 91久久精品电影| 久久免费精品视频| 欧美伊香蕉久久综合类网站| 国产婷婷成人久久Av免费高清 | 久久国产精品一区| 丰满少妇人妻久久久久久4| 99国产精品久久久久久久成人热| 国产激情久久久久久熟女老人| 久久精品国产99久久久古代| 综合久久国产九一剧情麻豆 | 亚洲级αV无码毛片久久精品| 久久精品国产久精国产果冻传媒| 国产精品久久新婚兰兰|