Skip to content

Instantly share code, notes, and snippets.

@cadl
Created November 8, 2012 11:44
Show Gist options
  • Save cadl/4038339 to your computer and use it in GitHub Desktop.
Save cadl/4038339 to your computer and use it in GitHub Desktop.
labyrinth
import Queue
queue = Queue.Queue()
valueSet = set()
valueProc = [(lambda x: x/2+7, 1), (lambda x: (x+7)/2, 0), (lambda x: x*3-5, 3),(lambda x: (x-5)*3, 2)]
valueTry = [(lambda x: (x-5), 2), (lambda x: x*3, 3)]
def check(value, state):
for f, s in valueTry:
if not state == s:
if f(value) == 2012:
return True
return False
def run(((value, state), l)):
if check(value, state) == True:
print "find it"
print "2011", "-->"
for step in l:
print step, "-->"
print "2012"
exit()
else:
for index, (f, s) in enumerate(valueProc):
if not state == s:
o = (f(value), index)
if o in valueSet:
continue
valueSet.add(o)
a = list(l)
a.append(o)
queue.put((o, a))
def main():
valueSet.add((2011+7, 0))
valueSet.add((2011/2, 1))
queue.put(((2011+7, 0), [(2018, 0)]))
queue.put(((2011/2, 1), [(1005, 1)]))
while not queue.empty():
run(queue.get())
if __name__ == "__main__":
main()
"""
结果:
find it
2011 -->
(1005, 1) -->
(506, 1) -->
(256, 1) -->
(131, 1) -->
(69, 1) -->
(202, 2) -->
(601, 2) -->
(304, 1) -->
(897, 3) -->
(2676, 3) -->
(1345, 0) -->
(4020, 3) -->
(2017, 0) -->
2012
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment