Python递归-矩阵中子序列的最大长度未更新
我试图找到矩阵中字符的最长序列。我对python真的很陌生,我认为问题在于python的递归方法与C/C++/Java等不同。这是我的代码,现在。。。如果在Python中不使用递归,您知道另一种方法吗?或者您可以修复我的代码以在pythonic递归中工作吗?(问题是长度和访问矩阵在递归过程中没有更新) 我想这个就行了Python递归-矩阵中子序列的最大长度未更新,python,algorithm,recursion,matrix,Python,Algorithm,Recursion,Matrix,我试图找到矩阵中字符的最长序列。我对python真的很陌生,我认为问题在于python的递归方法与C/C++/Java等不同。这是我的代码,现在。。。如果在Python中不使用递归,您知道另一种方法吗?或者您可以修复我的代码以在pythonic递归中工作吗?(问题是长度和访问矩阵在递归过程中没有更新) 我想这个就行了 def get_current_score(self, i, j, char, leng): # leng = 0 <- don't self.viz[i][
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()
def获取当前分数(self、i、j、char、leng):
#leng=0编辑:这不是最终代码,抱歉,实际代码使用leng=而不是返回leng+,不清楚此代码试图实现什么,您能提供更多信息吗?另外,如何定义子序列?(查看代码,似乎您没有使用通用的“子序列”定义)这很难闻:您将类属性self.viz
作为参数viz
传递,并且只使用self.viz
。对于更糟糕的长度:发送self.score1
将其设置为局部变量leng
,该变量在每次递归时设置为零。您的函数只能返回1
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()