如何进行收集绝对最大点数的遍历?

时间:2019-01-04 17:22:26

标签: python oop matrix

我们有一个遍历,该遍历从第一行的任何元素开始,获取其能量,并且他必须从该起始位置收集最大能量,直到到达最后一行,他仅移动到(i + 1,j +1)或(i + 1,j-1)或(i + 1,j),并且他在移动时保持固定的能量。

我尝试了一种贪婪的方法,有时它会在我编写指示的行中告诉我“列表索引超出范围”,我认为有一种更好的方法可以解决,但无法弄清楚,谢谢提前

import random

class robot:
    def __init__(self, column, row = 0):
        self.row = row
        self.column = column
        self.power = a[row][column]
        self.consume = random.randint(1,5)

    def left(self):
        self.row = self.row + 1
        self.column = self.column - 1
        self.power = self.power + a[self.row][self.column] - self.consume

    def right(self):
        self.row = self.row + 1
        self.column = self.column + 1
        self.power = self.power + a[self.row][self.column] - self.consume

    def center(self):
        self.row = self.row + 1
        self.power = self.power + a[self.row][self.column] - self.consume

    def decision(self):
        row = self.row
        column = self.column
        if row < 9:
            if column < 9:
                if a[row + 1][column + 1] > a[row + 1][column] and a[row + 1][column  + 1] > a[row + 1][column - 1]:
                    self.right()
            else:
                if a[row + 1][column - 1] > a[row + 1][column]:
                    self.left()
                else: 
                    self.center
            if a[row + 1][column] > a[row + 1][column + 1] and a[row + 1][column] > a[row + 1][column - 1]:
                self.center()
            else:
                if a[row + 1][column  + 1] > a[row + 1][column - 1]:
                    self.right()
                else: 
                    self.left()
            if column > 0:
                if a[row + 1][column - 1] > a[row + 1][column] and a[row + 1][column  - 1] > a[row + 1][column + 1]:
                    self.left()
            else:
                if a[row + 1][column] > a[row + 1][column + 1]:
                    self.center()
                else:
                    self.right()

a = [[random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10)]]
for i in range (0,10):
    a.append([random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10)])
x = 0
y = random.randint(0,9)
p = robot(y)
while (x<9):
    x= x + 1
    p.decision()
print(p.power)

1 个答案:

答案 0 :(得分:1)

我使用pprint可视化您的矩阵板,但可以随时删除该部分。您的问题来自decision()方法中复杂的if / else块。在其中某个地方,当您调用一个[row + 1] [column]列时,该列的索引编制超出范围,该列的正号或负号之一。我修复了该部分,以找到机器人下方的三个值,然后,如果left为max,则向左移动;如果center为max,则向中心,如果right为max,则向右走。这三个值被初始化为-1,因此,如果在边缘上不存在left或right,则它将永远不会是这三个值中的最大值。

import random, pprint

class robot():
    # omitted other methods
    def decision(self):
        row = self.row
        column = self.column
        if row == len(a):
            return
        # initialize local variables to store left, center, and right values at -1
        values = [-1, -1, -1]

        # set left value if it exists
        if column > 0:
            values[0] = a[row+1][column-1]
        # set center value
        values[1] = a[row+1][column]
        # set right value if it exists
        if column < 9:
            values[2] = a[row+1][column+1]

        if values[0] == max(values):
            self.left()
        elif values[1] == max(values):
            self.center()
        elif values[2] == max(values):
            self.right()

a = [[random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10)]]
for i in range (0,10):
    a.append([random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10), random.randint(0,10)])

x = 0
y = random.randint(0,9)
p = robot(y)

pprint.pprint(a)
print('starting:', y)

while(p.row<9):
    p.decision()
print(p.power)

P.S。我认为这不一定一定会在最后找到最高的能量总量,因为有时跟随机器人正下方的最大值不会导致最高路径。例如,在此4x4板上,假设机器人从第0行和第0列开始: R 0 0 0 0 1 0 0 0 0 1 0 9 0 0 1

机器人最终将获得3的能量(忽略消耗的能量),但是如果他直接下降到9会更好。所以我认为您需要一个BFS来检查所有可能的底部移动行并找到这些路径的最大值。本质上是一个遍历树的问题。