{"id":7188,"date":"2024-04-12T14:01:01","date_gmt":"2024-04-12T06:01:01","guid":{"rendered":""},"modified":"2024-04-12T14:01:01","modified_gmt":"2024-04-12T06:01:01","slug":"Hanio \u7b97\u6cd5\u4e2a\u4eba\u7406\u89e3","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7188.html","title":{"rendered":"Hanio \u7b97\u6cd5\u4e2a\u4eba\u7406\u89e3"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

Hanio \u7b97\u6cd5\u2014\u2014\u5206\u6cbb\u7b56\u7565<\/h3>\n
\n

Hanio \u95ee\u9898\u662f\u6e90\u4e8e\u5370\u5ea6\u4e00\u4e2a\u53e4\u8001\u4f20\u8bf4\u7684\u76ca\u667a\u73a9\u5177\u3002\u5927\u68b5\u5929\u521b\u9020\u4e16\u754c\u7684\u65f6\u5019\u505a\u4e86\u4e09\u6839\u91d1\u521a\u77f3\u67f1\u5b50\uff0c\u5728\u4e00\u6839\u67f1\u5b50\u4e0a\u4ece\u4e0b\u5f80\u4e0a\u6309\u7167\u5927\u5c0f\u987a\u5e8f\u645e\u774064\u7247\u9ec4\u91d1\u5706\u76d8\u3002\u5927\u68b5\u5929\u547d\u4ee4\u5a46\u7f57\u95e8\u628a\u5706\u76d8\u4ece\u4e0b\u9762\u5f00\u59cb\u6309\u5927\u5c0f\u987a\u5e8f\u91cd\u65b0\u6446\u653e\u5728\u53e6\u4e00\u6839\u67f1\u5b50\u4e0a\u3002\u5e76\u4e14\u89c4\u5b9a\uff0c\u5728\u5c0f\u5706\u76d8\u4e0a\u4e0d\u80fd\u653e\u5927\u5706\u76d8<\/strong>\uff0c\u5728\u4e09\u6839\u67f1\u5b50\u4e4b\u95f4\u4e00\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\u3002<\/p>\n<\/blockquote>\n

\"Hanio<\/p>\n

\u95ee\u9898\u63cf\u8ff0<\/h5>\n

\u4eca\u6709 A\u3001B\u3001C<\/strong> \u4e09\u4e2a\u5854\u5ea7\uff0c\u6700\u521d\u5728\u5854\u5ea7A\u4e0a\u6709 n<\/strong> \u4e2a\u5706\u76d8\uff0c\u5176\u4e2d\u8fd9n<\/strong>\u4e2a\u5706\u76d8\u7684\u5806\u53e0\u987a\u5e8f\u662f\u6309\u7167\u5927\u5c0f\u7531\u5927\u5230\u5c0f\u3001\u4ece\u4e0b\u5230\u4e0a\u8fdb\u884c\u6392\u5e8f\u3002\u5148\u5c06 A<\/strong> \u4e0a\u7684n\u4e2a\u5706\u76d8\u79fb\u52a8\u5230 C<\/strong> \u4e0a\u3002
\u79fb\u52a8\u89c4\u5219<\/strong>\uff1a<\/p>\n

    \n
  1. \u6bcf\u6b21\u53ea\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8<\/li>\n
  2. \u4e0d\u5141\u8bb8\u5c06\u5927\u5706\u76d8\u653e\u5728\u5c0f\u5706\u76d8\u4e0a<\/li>\n
  3. \u5728\u6ee1\u8db31\u30012\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u5c06\u5706\u76d8\u4efb\u610f\u79fb\u52a8\u5230A\u3001B\u3001C\u4e0a\u3002<\/li>\n<\/ol>\n

    \"Hanio<\/p>\n

    \u8bbe\u8ba1\u601d\u8def<\/h5>\n

    \u6211\u4eec\u5148\u58f0\u660eHanio\u7684\u7b97\u6cd5\u51fd\u6570\uff0c\u5e76\u6682\u65f6\u5c06\u6b64\u51fd\u6570\u770b\u505a\u662f\u4e00\u4e2a\u9ed1\u7bb1<\/strong>\u3002<\/p>\n

    \/\/ \u8f93\u5165\uff1a\u5854\u76d8A\u3001 \u5854\u76d8B\u3001 \u5706\u76d8\u4e2a\u6570n<\/span>\n\/\/ \u529f\u80fd\uff1a\u5c06\u5854\u76d8A\u4e2dn\u4e2a\u5143\u7d20\u6309\u539f\u59cb\u987a\u5e8f\u79fb\u52a8\u5230\u5854\u76d8B\u4e0a<\/span>\nvoid<\/span> Hanio<\/span>(<\/span>A,<\/span>B,<\/span>n)<\/span>;<\/span>\n<\/code><\/pre>\n

    \u8fd9\u662f\u4e00\u9053\u5178\u578b\u7684 \u5206\u6cbb\u7b56\u7565<\/strong> \u7b97\u6cd5\uff1a
    \u6211\u4eec\u5148\u56de\u987e\u4e00\u4e0b \u5206\u6cbb\u7b56\u7565<\/strong> \u7684 \u8bbe\u8ba1\u601d\u60f3<\/strong> \uff1a<\/p>\n

      \n
    1. \u5c06\u539f\u95ee\u9898\u5212\u5206\u6216\u5f52\u7ed3\u4e3a\u89c4\u6a21\u8f83\u5c0f\u7684\u5b50\u95ee\u9898<\/li>\n
    2. \u9012\u5f52\u6216\u8fed\u4ee3\u6c42\u89e3\u6bcf\u4e2a\u5b50\u95ee\u9898<\/li>\n
    3. \u5c06\u5b50\u95ee\u9898\u7684\u89e3\u7efc\u5408\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3<\/li>\n<\/ol>\n

      \u5728\u5212\u5206\u4e3a\u5b50\u95ee\u9898\u7684\u8fc7\u7a0b\u4e2d\u4e00\u822c\u6709 \u5bf9\u534a\u5212\u5206 \u548c \u7b49\u5dee\u5212\u5206 \u65b9\u5f0f\u3002\u6211\u4eec\u518d\u770bHanio<\/strong>\u8fd9\u4e2a\u95ee\u9898\uff0c\u5bf9\u534a\u5212\u5206\u5c06n\u4e2a\u95ee\u9898\u5212\u5206\u4e3a2\u4e2a n\/2 \u5b50\u95ee\u9898\u4e0d\u5408\u9002\uff0c\u800c\u5728\u6bcf\u6b21\u9012\u5f52\u6216\u8fed\u4ee3\u4e2d\u5c06n\u4e2a\u95ee\u9898\u5212\u5206\u6210 n - 1 \u4e2a\u548c 1 \u4e2a\u5b50\u95ee\u9898<\/strong>\u5219\u53ef\u5de7\u5999\u5730\u8bbe\u8ba1\u51faHanio\u95ee\u9898\u7684\u89e3\u51b3\u9014\u5f84\u3002<\/p>\n

      \u5982\u56fe\uff0c\u5728\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6211\u4eec\u5c06 \u5854\u76d8A \u4e2d\u7684 n \u4e2a\u5706\u76d8\u5316\u4e3a n - 1 \u548c 1 \u4e24\u4e2a\u90e8\u5206\uff0c\u6700\u5e95\u5c42\u7684\u5706\u76d8\u4e3a 1 \u4e2a\u5b50\u95ee\u9898\uff0c\u800c\u4e0a\u9762\u7684 n - 1 \u4e2a\u5706\u76d8\u6784\u6210 n - 1 \u4e2a\u5b50\u95ee\u9898\u3002
      \"Hanio
      \u5c06n-1\u4e2a\u5b50\u95ee\u9898\u770b\u6210\u4e00\u4e2a\u6574\u4f53\u540e\uff0c\u6b64\u65f6\u5c31\u76f8\u5f53\u4e8eA\u76d8\u4e0a\u4ec5\u6709\u4e24\u4e2a\u76d8\uff0c\u6211\u4eec\u5f88\u5bb9\u6613\u5c31\u80fd\u627e\u51fa\u89e3\u51b3\u7684\u65b9\u6cd5\u3002<\/p>\n

        \n
      1. \u7b2c\u4e00\u6b65\uff1a\u5c06A\u4e2d\u7684n-1\u4e2a\u5706\u76d8\uff08n-1\u4e2a\u5b50\u95ee\u9898\uff09\u6574\u4f53\u79fb\u52a8\u5230B\u4e0a\uff1b<\/li>\n
      2. \u7b2c\u4e8c\u6b65\uff1a\u5c06A\u4e2d\u6700\u540e\u7684\u4e00\u4e2a\u5706\u76d8\uff081\u4e2a\u5b50\u95ee\u9898\uff09\u79fb\u52a8\u5230C\u4e0a\uff1b<\/li>\n
      3. \u7b2c\u4e09\u6b65\uff1a\u6700\u540e\u518d\u5c06B\u4e2d\u7684n-1\u4e2a\u5706\u76d8\uff08n-1\u4e2a\u5b50\u95ee\u9898\uff09\u79fb\u52a8\u5230C\u4e0a\u3002<\/li>\n<\/ol>\n

        \u8fd9\u6837\u5c31\u5c06A\u4e2d\u7684\u5706\u76d8\u6309\u539f\u6765\u7684\u987a\u5e8f\u79fb\u52a8\u5230\u4e86C\u4e0a\u3002
        \u524d\u9762\u6211\u4eec\u5df2\u7ecf\u5b9a\u4e49\u4e86\u51fd\u6570 Hanio(A, C, n)<\/code> \u7684\u529f\u80fd: \u5c06\u5854\u76d8A\u4e2dn\u4e2a\u5143\u7d20\u6309\u539f\u59cb\u987a\u5e8f\u79fb\u52a8\u5230\u5854\u76d8B\u4e0a\u3002
        \u73b0\u5728\u53ef\u7528\u51fd\u6570\u6765\u8868\u793a\u4e0a\u9762\u7684\u6b65\u9aa4\uff1a<\/p>\n

          \n
        1. void Hanio(A, C, n)<\/li>\n
        2. \n
          \tHanio(A, B, n-1)\t\t\t\u5c06A\u4e2d\u7684n-1\u4e2a\u5706\u76d8\uff08n-1\u4e2a\u5b50\u95ee\u9898\uff09\u6574\u4f53\u79fb\u52a8\u5230B\u4e0a\n<\/code><\/pre>\n<\/li>\n
        3. \n
          \tMove(A, C)\t\t\t\t\t\u5c06A\u4e2d\u7684\u4e00\u4e2a\u5706\u76d8\u79fb\u52a8\u5230C\u4e0a\n<\/code><\/pre>\n<\/li>\n
        4. \n
          \tHanio(B,C)\t\t\t\t\t\u5c06B\u4e2d\u7684n-1\u4e2a\u5706\u76d8\uff08n-1\u4e2a\u5b50\u95ee\u9898\uff09\u79fb\u52a8\u5230C\u4e0a\n<\/code><\/pre>\n<\/li>\n<\/ol>\n

          \u800cn - 1\u4e2a\u5b50\u95ee\u9898\u53c8\u4f1a\u7ee7\u7eed\u5f52\u7ea6\uff0c\u5c06\u539f\u95ee\u9898\u5f52\u7ed3\u4e3an-2\u76844\u4e2a\u5b50\u95ee\u9898\uff0c\u7ee7\u7eed\u2026\u2026\u5f53\u5b50\u95ee\u9898\u89c4\u6a21\u4e3a1\u65f6\uff0c\u4e5f\u5c31\u662f\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6b64\u65f6\u4e0d\u80fd\u518d\u7ee7\u7eed\u5212\u5206\u5b50\u95ee\u9898\uff0c\u5f52\u7ea6\u622a\u6b62<\/strong>\uff0c\u800c\u5bf9\u4e8e\u4e00\u4e2a\u5706\u76d8\u53ea\u9700\u76f4\u63a5\u79fb\u52a8\u5230\u76ee\u6807\u5854\u76d8\u5373\u53ef\uff0c\u5373\u76f4\u63a5\u4f7f\u7528\u79fb\u52a8\u51fd\u6570 Move(A, C)<\/code> \u3002<\/p>\n

          \t\/\/ \u622a\u6b62\u6761\u4ef6<\/span>\n\tif<\/span> (<\/span>n ==<\/span> 1<\/span>)<\/span>\t\tMove<\/span>(<\/span>A,<\/span> C)<\/span>;<\/span>\n<\/code><\/pre>\n

          \u6700\u7ec8\u56de\u5f52\u7684\u8fc7\u7a0b\u4e2d\u89c4\u6a211 \u5230 n-1 \u9646\u7eed\u7ec4\u5408\u4e24\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u5230\u89c4\u6a21\u4e3an\u3002<\/strong>
          \u6211\u7528\u6808stack<\/code>\u6765\u5b9e\u73b0\u5854\u76d8\u3002<\/p>\n

          #include<\/span> <stack><\/span><\/span>\n#include<\/span> <iostream><\/span><\/span>\nusing<\/span> namespace<\/span> std;<\/span>\n\nstatic<\/span> stack<<\/span>int<\/span>><\/span> B;<\/span>\t\/\/ \u8f85\u52a9\u5854\u76d8<\/span>\n\n\/\/ \u79fb\u52a8\uff0c\u5c06A\u7684\u6808\u9876\u5143\u7d20\u79fb\u52a8\u5230C<\/span>\nvoid<\/span> Move<\/span>(<\/span>stack<<\/span>int<\/span>><\/span>&<\/span> A,<\/span> stack<<\/span>int<\/span>><\/span>&<\/span> C)<\/span>\n{ \n   <\/span>\n\tC.<\/span>push<\/span>(<\/span>A.<\/span>top<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n\tA.<\/span>pop<\/span>(<\/span>)<\/span>;<\/span>\t\t\/\/ \u51fa\u6808<\/span>\n}<\/span>\n\n\/\/ Hanio \u7b97\u6cd5\uff0c\u5c06A\u4e2d\u7684\u5143\u7d20\u6309\u539f\u6765\u7684\u987a\u5e8f\u79fb\u52a8\u5230C<\/span>\nvoid<\/span> Hanio<\/span>(<\/span>stack<<\/span>int<\/span>><\/span>&<\/span> A,<\/span> stack<<\/span>int<\/span>><\/span>&<\/span> C,<\/span> int<\/span> n)<\/span>\n{ \n   <\/span>\n\tif<\/span> (<\/span>n ==<\/span> 1<\/span>)<\/span>\t\t\/\/ \u622a\u6b62\u6761\u4ef6<\/span>\n\t\tMove<\/span>(<\/span>A,<\/span> C)<\/span>;<\/span>\n\telse<\/span> { \n   <\/span>\n\t\tHanio<\/span>(<\/span>A,<\/span> B,<\/span> n -<\/span> 1<\/span>)<\/span>;<\/span>\n\t\tMove<\/span>(<\/span>A,<\/span> C)<\/span>;<\/span>\n\t\tHanio<\/span>(<\/span>B,<\/span> C,<\/span> n -<\/span> 1<\/span>)<\/span>;<\/span>\n\t}<\/span>\n}<\/span>\n\/\/ \u4ece\u6808\u9876\u81f3\u6808\u5e95\u6253\u5370\u5143\u7d20\u4fe1\u606f<\/span>\nvoid<\/span> StackPrint<\/span>(<\/span>stack<<\/span>int<\/span>><\/span> s)<\/span>\n{ \n   <\/span>\n\twhile<\/span> (<\/span>!<\/span>s.<\/span>empty<\/span>(<\/span>)<\/span>)<\/span> { \n   <\/span>\n\t\tcout <<<\/span> s.<\/span>top<\/span>(<\/span>)<\/span> <<<\/span> ends;<\/span>\n\t\ts.<\/span>pop<\/span>(<\/span>)<\/span>;<\/span>\n\t}<\/span>\n\tcout <<<\/span>  endl;<\/span>\n}<\/span>\n\nint<\/span> main<\/span>(<\/span>)<\/span>\n{ \n   <\/span>\n\tstack<<\/span>int<\/span>><\/span> A;<\/span>\t\/\/ \u521d\u59cb\u76d8 <\/span>\n\tstack<<\/span>int<\/span>><\/span> C;<\/span>\t\/\/ \u76ee\u6807\u76d8<\/span>\n\t\/\/ \u521d\u59cb\u5316\u76d8A\u4e3a5,4,3,2,1<\/span>\n\tfor<\/span> (<\/span>int<\/span> i =<\/span> 5<\/span>;<\/span> i ><\/span> 0<\/span>;<\/span> --<\/span>i)<\/span>\n\t\tA.<\/span>push<\/span>(<\/span>i)<\/span>;<\/span>\n\n\tHanio<\/span>(<\/span>A,<\/span> C,<\/span> A.<\/span>size<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n\n\tStackPrint<\/span>(<\/span>C)<\/span>;<\/span>\n\treturn<\/span> 0<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n

          \u6211\u4eec\u5c06A\u521d\u59cb\u5316\u4e3a\uff1a1,2,3,4,5\uff08\u7531\u6808\u9876\u5230\u6808\u5e95\uff09<\/font>
          \u6700\u7ec8\u6211\u4eec\u8f93\u51faC\u7684\u7ed3\u679c\u4e5f\u4e3a\uff1a1,2,3,4,5<\/font><\/p>\n

          \u603b\u7ed3<\/h5>\n

          \u8bbe n \u4e2a\u76d8\u5b50\u7684\u79fb\u52a8\u6b21\u6570\u4e3a T(n)<\/p>\n

            \n
          1. T(n) = 2 T(n-1) + 1<\/li>\n
          2. T(1) = 1<\/li>\n
          3. \u53ef\u5f97\uff1aT(n) = 2n<\/sup> - 1<\/li>\n<\/ol>\n

            \u6df1\u5165\u7406\u89e3\u5206\u6cbb\u7b56\u7565<\/strong>\u7684\u4e2d\u5fc3\u601d\u60f3\uff1a<\/p>\n

              \n
            1. \u539f\u95ee\u9898\u53ef\u4ee5\u5212\u5206\u6216\u5f52\u7ea6\u4e3a\u89c4\u6a21\u8f83\u5c0f\u7684\u5b50\u95ee\u9898<\/li>\n
            2. \u5b50\u95ee\u9898\u89c4\u6a21\u8db3\u591f\u5c0f\u65f6\u53ef\u4ee5\u76f4\u63a5\u6c42\u89e3<\/li>\n
            3. \u9012\u5f52\u6216\u8fed\u4ee3\u5b9e\u73b0<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"Hanio \u7b97\u6cd5\u4e2a\u4eba\u7406\u89e3Hanio\u7b97\u6cd5\u2014\u2014\u5206\u6cbb\u7b56\u7565Hanio\u95ee\u9898\u662f\u6e90\u4e8e\u5370\u5ea6\u4e00\u4e2a\u53e4\u8001\u4f20\u8bf4\u7684\u76ca\u667a\u73a9\u5177","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7188"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7188"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7188\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}