作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
所以,我妻子在 Steam 上玩 Hammerwatch。她遇到了一个难题,我决定尝试为其编写解决方案。
拼图的原理如下:
激活一个开关可以打开或关闭该开关,也可以切换其相邻的开关。
这是游戏中拼图的 YouTube 视频:
http://www.youtube.com/watch?v=OM1XD7IZ0cg
我想出了如何让拼图的机制正常工作。我最终意识到我有两种选择可以让计算机解决这个问题:
A)让电脑随机选择开关来解决
……或者……
B) 创建一个算法,让计算机更有效地解决难题。
作为一名新程序员(CodeAcademy 教程学习了一半,LPTHW 学习了一半,目前正在学习 MIT edX 计算机科学 Python 类(class)),我觉得我解决这个问题的能力有点有限。我是来学习的!请帮忙!
我需要帮助,要么想出一种更好的方法来随机解决这个问题,要么更好地找到一种算法,让计算机系统地解决这个难题。
我唯一能做的就是让计算机存储列表或字典中的拼图状态,通过跳过那些存储的状态来协助程序,指向程序到新的可能的解决方案
我打算让用户使用前 9 个 raw_inputs 输入拼图板的当前状态。然后它进入一个循环,随机切换拼图板的开关,直到它们全部打开。
P.S.:当我注册 StackOverflow 帐户并输入此消息时,我的计算机一直在后台运行此程序以寻找解决方案。已经大约一个小时了,仍然没有找到解决方案,它目前正在进行 ~92,000,000 次迭代。我认为它不起作用...
import random
def switcheroo(x):
"""
switches 'x' to 1 if it's a 0 and vice-versa
"""
if x == 0:
x = 1
else:
x = 0
return x
# original input variables
a1 = 0
a2 = 0
a3 = 0
b1 = 0
b2 = 0
b3 = 0
c1 = 0
c2 = 0
c3 = 0
# puzzleboard
print "\n\n"
print " 1 2 3 "
print " -------------"
print "a |",a1,"|",a2,"|",a3,"|"
print " -------------"
print "b |",b1,"|",b2,"|",b3,"|"
print " -------------"
print "c |",c1,"|",c2,"|",c3,"|"
print " -------------"
print "\n\n"
print "What's ON/OFF? (type 0 for OFF, 1 for ON)"
a1 = int(raw_input("a1: "))
a2 = int(raw_input("a2: "))
a3 = int(raw_input("a3: "))
b1 = int(raw_input("b1: "))
b2 = int(raw_input("b2: "))
b3 = int(raw_input("b3: "))
c1 = int(raw_input("c1: "))
c2 = int(raw_input("c2: "))
c3 = int(raw_input("c3: "))
# for counting the iterations within the loop
iteration = 0
# to stop loop if all switches are ON
ans = a1 and a2 and a3 and b1 and b2 and b3 and c1 and c2 and c3
while ans == False:
# randomly generates number, flipping random switches
counter = random.randint(1,9)
if counter == 1:
switch = "a1"
elif counter == 2:
switch = "a2"
elif counter == 3:
switch = "a3"
elif counter == 4:
switch = "b1"
elif counter == 5:
switch = "b2"
elif counter == 6:
switch = "b3"
elif counter == 7:
switch = "c1"
elif counter == 8:
switch = "c2"
elif counter == 9:
switch = "c9"
# PUZZLE MECHANICES #
if switch == "a1":
a1 = switcheroo(a1)
a2 = switcheroo(a2)
b1 = switcheroo(b1)
if switch == "a2":
a2 = switcheroo(a2)
a1 = switcheroo(a1)
a3 = switcheroo(a3)
b2 = switcheroo(b2)
if switch == "a3":
a3 = switcheroo(a3)
a2 = switcheroo(a2)
b3 = switcheroo(b3)
if switch == "b1":
b1 = switcheroo(b1)
b2 = switcheroo(b2)
a1 = switcheroo(a1)
c1 = switcheroo(c1)
if switch == "b2":
b2 = switcheroo(b2)
a2 = switcheroo(a2)
b1 = switcheroo(b1)
b3 = switcheroo(b3)
c2 = switcheroo(c2)
if switch == "b3":
b3 = switcheroo(b3)
b1 = switcheroo(b1)
b2 = switcheroo(b2)
c3 = switcheroo(c3)
# Edit 1
if switch == "c1":
c1 = switcheroo(c1)
c2 = switcheroo(c2)
b1 = switcheroo(b1)
if switch == "c2":
c2 = switcheroo(c2)
c1 = switcheroo(c1)
c3 = switcheroo(c3)
b2 = switcheroo(b2)
if switch == "c3":
c3 = switcheroo(c3)
c2 = switcheroo(c2)
b3 = switcheroo(b3)
if switch == "stop":
break
# prints puzzle-board state at end of loop iteration
print "\n\n"
print " 1 2 3 "
print " -------------"
print "a |",a1,"|",a2,"|",a3,"|"
print " -------------"
print "b |",b1,"|",b2,"|",b3,"|"
print " -------------"
print "c |",c1,"|",c2,"|",c3,"|"
print " -------------"
print "\n\n"
# prints which # was randomly generated
print "random #: ", counter
# tracks loop iteration
iteration += 1
print "iteration", iteration
if ans == True:
print "I figured it out!"
这个问题在这里已经有了答案: Any algorithm for "Flip all" (Light Out) game? [duplicate] (2 个答案) 关闭 8 年前。 所以,我妻子在
我是一名优秀的程序员,十分优秀!