[英]What's wrong with my Python code about this algorithm?
有一场比赛。 每个人有一个10*10的方格板。 游戏开始前,他们需要在棋盘上放置三架“飞机”。 问题是:飞机放置的可能性有多少?
这是一架飞机。
这是放置飞机的两种合法方式。 平面的放置不能重叠,但可以插入间隙。
我的代码如下:
class Plane():
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
@property
def body(self):
x = self.x
y = self.y
if self.direction == "up":
return[(x-2, y-1), (x-1, y-1), (x, y-1),
(x+1, y-1), (x+2, y-1), (x, y-2),
(x-1, y-3), (x, y-3), (x+1, y-3)]
elif self.direction == "down":
return[(x-2, y+1), (x-1, y+1), (x, y+1),
(x+1, y+1), (x+2, y+1), (x, y+2),
(x-1, y+3), (x, y+3), (x+1, y+3)]
elif self.direction == "left":
return[(x+1, y+2), (x+1, y+1), (x+1, y),
(x+1, y-1), (x+1, y-2), (x+2, y),
(x+3, y+1), (x+3, y), (x+3, y-1)]
elif self.direction == "right":
return[(x-1, y+2), (x-1, y+1), (x-1, y),
(x-1, y-1), (x-1, y-2), (x-2, y),
(x-3, y+1), (x-3, y), (x-3, y-1)]
@property
def check(self):
global chart
for x in self.body:
if x[0]<0 or x[0]>9 or x[1]<0 or x[1]>9 or chart[x[0]][x[1]] != 0:
return False
return True
def recursion(plane):
global chart, plane_list
if plane.check:
x = plane.x
y = plane.y
plane_list.append(plane)
chart[x][y] = 2
for j in plane.body:
chart[j[0]][j[1]] = 1
if x!= 9:
find_plane(x+1, y)
else:
find_plane(0, y+1)
plane_list.remove(plane)
chart[x][y] = 0
for j in plane.body:
chart[j[0]][j[1]] = 0
def find_plane(startx, starty):
global method_list, chart, plane_list
if len(plane_list) == 3:
method_list.append(plane_list)
return
if startx == 9 and starty == 9:
return
for x in range(10):
for y in range(10):
if (x > startx or y > starty) and (chart[x][y] == 0):
recursion(Plane(x, y, "up"))
recursion(Plane(x, y, "down"))
recursion(Plane(x, y, "left"))
recursion(Plane(x, y, "right"))
if __name__ == "__main__":
method_list = []
chart = [[0]*10 for i in [0]*10]
plane_list = []
find_plane(0, 0)
print(method_list)
我有问题:
这里的截图并不完整。 实际上method_list由174631个[]组成。 这让我很困惑,因为我的代码逻辑是,只有当平面列表的长度为3时,才会将平面列表添加到方法列表中。 我不明白为什么 method_list 是由一堆空列表组成的。
翻译:经过DFS,我们发现9*9飞机轰炸游戏中有8744个放置方案。 如果棋盘大小为 10 × 10,则方案总数为 66816。
但是我的答案是66816的好几次,算法想了很久,还是不知道自己错在哪里。 希望得到答复。
我花了一些时间,得到66816。我会尽量回答:
1. 整个method_list
是对一个列表的引用列表 - plane_list
。 当plane_list
为空时,则method_list
所有元素都为空……而plane_list
为空,因为您删除了所有元素。 您应该用method_list.append(plane_list.copy())
替换method_list.append(plane_list)
method_list.append(plane_list.copy())
。
2. 这段代码背后的逻辑真的很糟糕:
if x!= 9:
find_plane(x+1, y)
else:
find_plane(0, y+1)
我明白你在这里想做什么,但你做错了。 所以……别这样。 不要试图在plane_list 中设置某种顺序。 就这样做:
find_plane(0, 0)
那么你将在method_list
有重复method_list
:平面可以按以下顺序method_list
:(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)。 同一个位置有6个副本。 所以...你应该将 len(method_list) 除以 6。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.