我有一个生成元组列表的函数,比如:
[(0, 0), (1, 1), (1, 2), (1,3), (2, 4), (3, 5), (4, 5)]
用于表示我正在制作的游戏中的分幅路径(行、列)。你知道吗
我用来生成这些路径的函数并不完美,因为它经常生成“循环”,如下所示:
[(2, 0), (2, 1), (1, 2), (0, 3), (0, 4), (1, 5), (2, 5), (3, 4), (3, 3),
(3, 2), (4, 1)]
上面的路径应该是这样的:
[(2, 0), (2, 1), (3, 2), (4, 1)]
这些路径可以包含任意数量的循环,这些循环可以是任意大小和形状。你知道吗
所以我的问题是,如何在python中编写一个函数来剪切循环列表,并返回一个新的、较短的、没有这些循环的列表。你知道吗
我的尝试如下:
def Cut_Out_Loops(Path):
NewList = list(Path)
Cutting = True
a = 0
for Cords in Path:
a += 1
try:
for i in range(a + 2, len(Path)):
if (Path[i][0] == Cords[0] and abs(Path[i][1] - Cords[1]) == 1:
NewList = NewList[0:a] + NewList[i:]
Path = list(NewList)
elif Path[i][1] == Cords[1] and abs(Path[i][0] - Cords[0]) == 1:
NewList = NewList[0:a] + NewList[i:]
Path = list(NewList)
elif abs(Path[i][0] - Cords[0]) == 1 and abs(Path[i][1] - Cords[1]) == 1:
NewList = NewList[0:a] + NewList[i:]
Path = list(NewList)
elif abs(Path[i][1] - Cords[1]) == 1 and abs(Path[i][0] - Cords[0]) == 1:
NewList = NewList[0:a] + NewList[i:]
Path = list(NewList)
Cutting = False
except IndexError:
Cutting = True
你的路有多长?如果它们都在1000个元素以下,即使是简单的暴力算法也可以:
尽管你对“循环”的定义不太清楚,还是试试这个
这绝对适用于你的例子:
也就是说,这只是最直接的暴力解决方案。复杂性是二次的。你知道吗
相关问题 更多 >
编程相关推荐