Рекурсия Python - максимальная длина подпоследовательности в матрице не обновляется - PullRequest
0 голосов
/ 06 июня 2018

Я пытаюсь найти самую длинную последовательность символов в матрице.Я действительно новичок в Python, и я думаю, что проблема в том, что рекурсивные методы Python не совпадают с C / C ++ / Java и т. Д. Вот мой код, теперь ... Знаете ли вы другой способ сделать это, если рекурсияВ Python нет ничего, или вы можете исправить мой код, чтобы он работал в Python?(проблема в том, что длина и посещаемая матрица не обновляются во время рекурсии).

def get_current_score(self, i, j, char, viz, leng):
    leng = 0
    self.viz[i][j] = True

    if (self.out_of_span(i - 1, j) == False and self.viz[i-1][j] == False and self.matrix[i - 1][j] == char):
        self.viz[i - 1][j] = True
        return leng + self.get_current_score(i - 1, j, char, self.viz, leng)
    if (self.out_of_span(i - 1, j +1) == False and self.viz[i-1][j+1] == False and self.matrix[i - 1][j + 1] == char):
        self.viz[i - 1][j + 1] = True
        return leng + self.get_current_score(i - 1, j + 1, char, self.viz, leng)
    if (self.out_of_span(i - 1, j - 1) == False and self.viz[i-1][j-1] == False and self.matrix[i - 1][j - 1] == char):
        self.viz[i - 1][j - 1] = True
        return leng + self.get_current_score(i - 1, j - 1, char, self.viz, leng)
    if (self.out_of_span(i, j - 1) == False and self.viz[i][j-1] == False and self.matrix[i][j - 1] == char):
        self.viz[i][j - 1] = True
        return leng + self.get_current_score(i, j - 1, char, self.viz, leng)
    if ( self.out_of_span(i, j + 1) == False and self.viz[i][j+1] == False and self.matrix[i][j + 1] == char):
        self.viz[i][j + 1] = True
        return leng + self.get_current_score(i, j + 1, char, self.viz, leng)
    if ( self.out_of_span(i + 1, j) == False and self.viz[i+1][j] == False and self.matrix[i + 1][j] == char):
        self.viz[i + 1][j] = True
        return leng + self.get_current_score(i + 1, j, char, self.viz, leng)
    if (self.out_of_span(i + 1, j - 1) == False and self.viz[i+1][j-1] == False and self.matrix[i + 1][j - 1] == char):
        self.viz[i + 1][j - 1] = True
        return leng + self.get_current_score(i + 1, j - 1, char, self.viz, leng)
    if (self.out_of_span(i + 1, j + 1) == False and self.viz[i+1][j+1] == False and self.matrix[i + 1][j + 1] == char):
        self.viz[i + 1][j + 1] = True
        return leng + self.get_current_score(i + 1, j + 1, char, self.viz, leng)

    return 1 + leng

def add(self, index):
    [...]
    # scor
    print('\n --------------\n')
    for i in range(self.maxh, self.nr):
        for j in range(self.span[0], self.span[1]+1):
                if(self.player1 == False and self.matrix[i][j] == 'X'):

                    self.score1 = max(self.score1, self.get_current_score(i, j, 'X', self.viz, self.score1))
                    self.viz[i][j] = True
                    #self.score1 -= 1
                else:
                    if(self.player1 == True and self.matrix[i][j] == 'O'):
                        self.score2 = max(self.score2, self.get_current_score(i, j, 'O', self.viz, self.score1))
                        self.viz[i][j] = True
    self.reset_viz()
    self.print_matrix()

1 Ответ

0 голосов
/ 06 июня 2018

Я думаю, что это будет

def get_current_score(self, i, j, char, leng):
    # leng = 0 <- don't
    self.viz[i][j] = True  # you set it twice, let's keep that one

    def test_cell(a, b):  # keep it DRY
        return self.out_of_span(a, b) == False \
               and self.viz[a][b] == False \
               and self.matrix[a][b] == char

    cells = [(i - 1, j), (i - 1, j + 1), (i - 1, j - 1), (i, j - 1),
             (i, j + 1), (i + 1, j), (i + 1, j - 1), (i + 1, j + 1)]
    for a, b in cells:
        if test_cell(a, b): 
            # you don't need to set that cell to true since that's the 
            # first thing you do in the function
            # self.viz[a][b] = True
            return leng + self.get_current_score(a, b, char, leng)

    return 1 + leng

def add(self, index):
    [...]
    # scor
    print('\n --------------\n')
    for i in range(self.maxh, self.nr):
        for j in range(self.span[0], self.span[1]+1):
                if(self.player1 == False and self.matrix[i][j] == 'X'):
                    # no need to send self.viz here. same for score2
                    # if you need to set self.score1 to 0 do it here. not in the recursion
                    self.score1 = max(self.score1, self.get_current_score(i, j, 'X', self.score1))
                    self.viz[i][j] = True
                    #self.score1 -= 1
                else:
                    if(self.player1 == True and self.matrix[i][j] == 'O'):
                        self.score2 = max(self.score2, self.get_current_score(i, j, 'O', self.score1))
                        self.viz[i][j] = True
    self.reset_viz()
    self.print_matrix()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...