我正在写一个模拟基本火星漫游车的程序。矩阵是"mars“,其中前两个数字是x和y坐标,第三个是安全分数,它决定给定的坐标是否可导航(0-不好,4-好)。“月球车”只能直接向右或向下行驶(开始-0,0和结束-3,3)。我的程序能够告诉我什么坐标是安全的,但我正在努力寻找一种方法来比较它们,以找到一条安全的路径。我需要找到一种方法来比较显示的输出,以便x或y坐标只更改一个(向右或向下)来确定路径。
# This is mars
matrix = [[0, 0, 1], [0, 1, 4], [0, 2, 1], [0, 3, 0],
[1, 0, 1], [1, 1, 1], [1, 2, 0], [1, 3, 1],
[2, 0, 0], [2, 1, 1], [2, 2, 1], [2, 3, 2],
[3, 0, 0], [3, 1, 0], [3, 2, 1], [3, 3, 1]]
# This decides if a given coordinate is safe
def is_safe(sublist):
if sublist[2::3] == [0]:
return False
else:
return True
# As of now this just stores safe coordinates in a list
def find_paths():
for sublist in matrix:
if is_safe(sublist):
safepath.append(sublist)
safepath = []
find_paths()
print(safepath)输出:
[[0, 0, 1], [0, 1, 4], [0, 2, 1], [1, 0, 1], [1, 1, 1], [1, 3, 1], [2, 1, 1],
[2, 2, 1], [2, 3, 2], [3, 2, 1], [3, 3, 1]]发布于 2021-02-11 09:00:04
以Bamar的评论为例:
m = {}
for x,y,z in matrix:
entry = m.get(x, {})
entry[y] = z
m[x] = entry
m[0][0] # outputs 1; i.e. m now contains matrix in normalized form现在,您的问题是在传统的2d矩阵中移动,而不是以一种奇怪的方式解析列表。
https://stackoverflow.com/questions/66147100
复制相似问题