gpt4 book ai didi

Python递归 - 矩阵中子序列的最大长度未更新

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:13 26 4
gpt4 key购买 nike

我试图在矩阵中找到最长的字符序列。我真的是 python 的新手,我认为问题在于 python 的递归方法与 C/C++/Java 等不同。这是我的代码,现在......你知道另一种方法吗,如果递归是不是 Python 的东西,或者你能修复我的代码以在 pythonic recusion 中工作吗? (问题是长度和访问矩阵在递归期间没有更新)。

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()

最佳答案

我觉得可以

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()

关于Python递归 - 矩阵中子序列的最大长度未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50718514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com