{"id":1802,"date":"2012-01-31T17:14:00","date_gmt":"2012-01-31T09:14:00","guid":{"rendered":""},"modified":"2013-11-23T14:58:06","modified_gmt":"2013-11-23T06:58:06","slug":"2011%e5%b9%b4%e5%ba%a6%e6%9c%80%e5%8f%98%e6%80%81%e7%9a%84%e8%bf%b7%e5%ae%ab%e9%9a%be%e9%a2%98","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1802.html","title":{"rendered":"2011\u5e74\u5ea6\u6700\u53d8\u6001\u7684\u8ff7\u5bab\u96be\u9898"},"content":{"rendered":"<p>\u4e0b\u9762\u5927\u5bb6\u5c06\u4f1a\u770b\u5230\u7684\u662f\u4e00\u4e2a\u6781\u5176\u7b80\u5355\u800c\u53c8\u6781\u5176\u590d\u6742\u7684\u201c\u8ff7\u5bab\u201d\uff0c\u8fd9\u65e0\u7591\u662f\u6211\u5728\u672c\u5e74\u5ea6\u89c1\u5230\u7684\u6700\u53d8\u6001\u7684\u8c1c\u9898\uff1a\u4ece\u5de6\u8fb9\u5165\u53e3\u5904\u7684 2011 \u8fdb\u53bb\uff0c\u5728\u8ff7\u5bab\u91cc\u8f6c\u60a0\uff0c\u6700\u540e\u53d8\u6210 2012 \u4ece\u53f3\u8fb9\u51fa\u6765\u3002\u4f60\u53ef\u4ee5\u5728\u8ff7\u5bab\u91cc\u8f6c\u5708\uff0c\u53ef\u4ee5\u91cd\u590d\u4e4b\u524d\u8d70\u8fc7\u7684\u8def\uff0c\u4f46\u4e0d\u80fd\u5f80\u56de\u9000\u7740\u8d70\u3002<\/p>\n<p><img decoding=\"async\" src=\".\/wp-content\/uploads\/hibaidu\/0b24e5c5b74543a9b740cb7f1e178a82b80114e3.jpg\" alt=\"\" \/><\/p>\n<p>Start at 2011. By moving through the maze and doing any arithmetic operations you encounter, exit the maze with a result of 2012. You may pass through an operation several times, but not twice in a row.<\/p>\n<p>\u539f\u9898\u548c\u7b54\u6848\u5728\u8fd9\u91cc\uff1a<a href=\"http:\/\/www2.stetson.edu\/%7Eefriedma\/holiday\/2011\/index.html\">http:\/\/www2.stetson.edu\/~efriedma\/holiday\/2011\/index.html<\/a><\/p>\n<p>\u5728\u7f51\u4e0a\u627e\u4e86\u4e24\u4e2apython\u7a0b\u5e8f\u3002<\/p>\n<p>\u7a0b\u5e8f\u4e00\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nplus7,div2,mul3,min5=(\u2018+ 7\u2032,\u2019\/ 2\u2032,\u2019* 3\u2032,\u2019- 5\u2032)\r\nleft,middle,right=range(3)\r\n\r\nclass node:\r\n    pre_node=None\r\n    pre_opr=None\r\n    pre_pos=None\r\n    this_node=None\r\n    def __init__(self,value,opr,pos):\r\n        self.value=value\r\n        self.opr=opr\r\n        self.pos=pos\r\n    def hash(self):\r\n        return self.value*157+ord(self.opr&#x5B;0])*3+self.pos\r\n    def next(self):\r\n        list=self._next()\r\n        for child in list:\r\n            child.pre_node=self.this_node\r\n            child.pre_opr=self.opr\r\n            child.pre_pos=self.pos\r\n        return list\r\n    def _next(self):\r\n        if self.opr==plus7:\r\n            child_var=self.value+7\r\n            if self.pos==left:\r\n                return node(child_var,div2,middle),node(child_var,mul3,middle),node(child_var,min5,middle)\r\n            elif self.pos==middle:\r\n                return node(child_var,div2,left),\r\n        elif self.opr==div2:\r\n            if self.value&amp;1:\r\n                return tuple()\r\n            child_var=int(self.value)\/\/2\r\n            if self.pos==left:\r\n                return node(child_var,plus7,middle),node(child_var,mul3,middle),node(child_var,min5,middle)\r\n            elif self.pos==middle:\r\n                return node(child_var,plus7,left),\r\n        elif self.opr==mul3:\r\n            child_var=int(self.value*3)\r\n            if self.pos==middle:\r\n                return node(child_var,min5,right),\r\n            elif self.pos==right:\r\n                return node(child_var,plus7,middle),node(child_var,div2,middle),node(child_var,min5,middle)\r\n        elif self.opr==min5:\r\n            child_var=self.value-5\r\n            if self.pos==middle:\r\n                return node(child_var,mul3,right),\r\n            elif self.pos==right:\r\n                return node(child_var,plus7,middle),node(child_var,div2,middle),node(child_var,mul3,middle)\r\n\r\nnode1=node(2011,plus7,left)\r\ntree=&#x5B;node1,]\r\nhashlist=set(&#x5B;node1.hash(),])\r\ni=0\r\nwhile not (tree&#x5B;i].pos==right and tree&#x5B;i].value==2012):\r\n    tree&#x5B;i].this_node=i\r\n    result=tree&#x5B;i].next()\r\n    for item in result:\r\n        hashitem=item.hash()\r\n        if hashitem in hashlist:\r\n            continue\r\n        else:\r\n            tree.append(item)\r\n            hashlist.add(hashitem)\r\n    i+=1\r\n    #if i%100==0:\r\n    #    print(i,tree&#x5B;i].pos,tree&#x5B;i].value)\r\nprint(\u2018Tried\u2019,i,\u2019times!\u2019)\r\nlist=&#x5B;]\r\nwhile tree&#x5B;i].pre_node:\r\n    list.append(i)\r\n    i=tree&#x5B;i].pre_node\r\nlist.append(0)\r\nlist.reverse()\r\nfor i in range(len(list)-1):\r\n\r\n    print(tree&#x5B;list&#x5B;i]].value,tree&#x5B;list&#x5B;i]].opr,\u2019=',tree&#x5B;list&#x5B;i+1]].value)\r\n<\/pre>\n<p>\u7a0b\u5e8f\u4e8c\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\nimport sys\r\n\r\ntodo=&#x5B;(2011,1,'+7',&#x5B;])]\r\nstate=0\r\n\r\nwhile(todo):\r\n    newtodo=&#x5B;]\r\n    for num, state, cmd, history in todo:\r\n#        print num, state, cmd, history\r\n        history=list(history)\r\n        if cmd==\u2019+7\u2032:\r\n            history.append(\u2018+7\u2032)\r\n            num+=7\r\n        elif cmd==\u2019\/2\u2032:\r\n            history.append(\u2018\/2\u2032)\r\n            num\/=2\r\n        elif cmd==\u2019-5\u2032:\r\n            history.append(\u2018-5\u2032)\r\n            num-=5\r\n        else:\r\n            history.append(\u2018*3\u2032)\r\n            num*=3\r\n        if num==2012:\r\n            print history\r\n            sys.exit(0)\r\n        if state==0:\r\n            if cmd==\u2019+7\u2032:\r\n                if(num%2==0):\r\n                    newtodo.append((num,1,\u2019\/2\u2032,history))\r\n            else:\r\n                newtodo.append((num,1,\u2019+7\u2032, history))\r\n        elif state==1:\r\n            if cmd==\u2019+7\u2032:\r\n                if(num%2==0):\r\n                    newtodo.append((num,0,\u2019\/2\u2032,history))\r\n                newtodo.append((num,2,\u2019*3\u2032,history))\r\n                newtodo.append((num,2,\u2019-5\u2032,history))\r\n            elif cmd==\u2019\/2\u2032:\r\n                newtodo.append((num,0,\u2019+7\u2032,history))\r\n                newtodo.append((num,2,\u2019*3\u2032,history))\r\n                newtodo.append((num,2,\u2019-5\u2032,history))\r\n            elif cmd==\u2019*3\u2032:\r\n                newtodo.append((num,0,\u2019+7\u2032,history))\r\n                if(num%2==0):\r\n                    newtodo.append((num,0,\u2019\/2\u2032,history))\r\n                newtodo.append((num,2,\u2019-5\u2032,history))\r\n            else:\r\n                newtodo.append((num,0,\u2019+7\u2032,history))\r\n                if(num%2==0):\r\n                    newtodo.append((num,0,\u2019\/2\u2032,history))\r\n                newtodo.append((num,2,\u2019*3\u2032,history))\r\n        elif state==2:\r\n            if cmd==\u2019*3\u2032:\r\n                newtodo.append((num,1,\u2019-5\u2032,history))\r\n            else:\r\n                newtodo.append((num,1,\u2019*3\u2032,history))\r\n    todo=newtodo\r\n<\/pre>\n<p>\u6211\u81ea\u5df1\u4e5f\u5199\u4e86\u4e2aPython\u7248\u672c\uff0c\u7528\u9012\u5f52\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#encoding=utf-8\r\ntable = &#x5B;'+', '*', '\/', '-']\r\nhasmethod = 0\r\ndef calc(method, cursum, curmethods, leftright):\r\n    global hasmethod\r\n\r\n    desmethod = &#x5B;]\r\n    if hasmethod:\r\n        return\r\n    if method == \u2018+\u2019:\r\n        cursum += 7\r\n        curmethods += \u2018+\u2019\r\n        if cursum % 2 == 0:\r\n            desmethod.append(\u2018\/\u2019)\r\n        if leftright == \u2018r\u2019:\r\n            desmethod.append(\u2018*\u2019)\r\n            desmethod.append(\u2018-\u2019)\r\n    elif method == \u2018*\u2019:\r\n        cursum *= 3\r\n        curmethods += \u2018*\u2019\r\n        if leftright == \u2018l\u2019:\r\n            if cursum % 2 == 0:\r\n                desmethod.append(\u2018\/\u2019)\r\n            desmethod.append(\u2018+\u2019)\r\n        desmethod.append(\u2018-\u2019)\r\n    elif method == \u2018-\u2019:\r\n        cursum -= 5\r\n        curmethods += \u2018-\u2019\r\n        if leftright == \u2018l\u2019:\r\n            if cursum % 2 == 0:\r\n                desmethod.append(\u2018\/\u2019)\r\n            desmethod.append(\u2018+\u2019)\r\n        desmethod.append(\u2018*\u2019)\r\n    elif method == \u2018\/\u2019:\r\n        cursum \/= 2\r\n        curmethods += \u2018\/\u2019\r\n        desmethod.append(\u2018+\u2019)\r\n        if leftright == \u2018r\u2019:\r\n            desmethod.append(\u2018*\u2019)\r\n            desmethod.append(\u2018-\u2019)\r\n    if cursum == 2012:\r\n        ensuremethod(curmethods)\r\n        curmethods = curmethods.replace(\u2018+\u2019, \u2018+7 \u2018)\r\n        curmethods = curmethods.replace(\u2018-\u2019, \u2018-5 \u2018)\r\n        curmethods = curmethods.replace(\u2018*\u2019, \u2018*3 \u2018)\r\n        curmethods = curmethods.replace(\u2018\/\u2019, \u2018\/2 \u2018)\r\n        print curmethods\r\n        hasmethod = 1\r\n    elif len(curmethods) &lt; 30:  #\u9632\u6b62\u9012\u5f52\u8c03\u7528\u5c42\u6b21\u8fc7\u6df1\r\n        for m in desmethod:\r\n            curleftright = leftright\r\n            #\u63a7\u5236\u65b9\u5411\uff0c\u4ece\u5de6\u8fdb\u8fd8\u662f\u4ece\u53f3\u8fdb\r\n            if (m in &#x5B;'+', '\/'] and method in &#x5B;'+', '\/']) or (m in &#x5B;'*', '-'] and method in &#x5B;'*', '-']):\r\n                curleftright = \u2018r\u2019 if leftright == \u2018l\u2019 else \u2018l\u2019\r\n            calc(m, cursum, curmethods, curleftright)\r\n\r\ndef ensuremethod(methodstr):\r\n    print methodstr\r\n    print len(methodstr)\r\n    s = 2011\r\n    str0 = \u201d\r\n    for i in methodstr:\r\n        strs = str(s)\r\n        if i == \u2018+\u2019:\r\n            s += 7\r\n            str0 = \u2018+7\u2032\r\n        elif i == \u2018-\u2019:\r\n            s -= 5\r\n            str0 = \u2018-5\u2032\r\n        elif i == \u2018*\u2019:\r\n            s *= 3\r\n            str0 = \u2018*3\u2032\r\n        elif i == \u2018\/\u2019:\r\n            s \/= 2\r\n            str0 = \u2018\/2\u2032\r\n        print \u2018%s%s=%d\u2019 % (strs, str0, s)\r\n    print s\r\n\r\nhasmethod = 0\r\ncalc(\u2018+\u2019, 2011, \u201d, \u2018r\u2019)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u5927\u5bb6\u5c06\u4f1a\u770b\u5230\u7684\u662f\u4e00\u4e2a\u6781\u5176\u7b80\u5355\u800c\u53c8\u6781\u5176\u590d\u6742\u7684\u201c\u8ff7\u5bab\u201d\uff0c\u8fd9\u65e0\u7591\u662f\u6211\u5728\u672c\u5e74\u5ea6\u89c1\u5230\u7684\u6700\u53d8\u6001\u7684\u8c1c\u9898\uff1a\u4ece\u5de6\u8fb9\u5165\u53e3\u5904\u7684  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1802","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=1802"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1802\/revisions"}],"predecessor-version":[{"id":5150,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1802\/revisions\/5150"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}