我正在尝试为我的矩阵执行追踪,它应该遵循图像所示的步骤,如link所示。我的答案只是提供一个值 - 7
,而应该还有两个值作为答案 - 1,4
。
这是追溯方法:
1)如果元素小于或等于1且不等于零,则读取矩阵的最后一行;我们认为这是我们的追溯。
2)然后,我们检查该元素的相邻对角线,垂直和水平值,以及M[i,j]
处的值是否针对三个(M[i-1,j]+1,M[i,j-1]+1,M[i-1,j-1]+1)
条件中的任何一个。
3)
a)如果M[i,j] = M[i-1,j]+1
我们垂直向上到下一个元素并将其视为我们的下一个M[i,j]
b)如果M[i,j]=M[i,j-1]+1
我们横向转到下一个元素,并将其视为我们的下一个M[i,j]
c)如果M[i,j]
等于M[i,j-1]+1
和M[i-1,j]+1
,我们希望垂直向上选择下一个M[i,j]
。
4)如果i+1,j+1
属于元组列表,那么我们遵循以下条件:
a)如果M[i,j] = M[i-1,j]+1
我们垂直向上到下一个元素并将其视为我们的下一个M[i,j]
b)如果M[i,j]=M[i,j-1]+1
我们横向转到下一个元素,并将其视为下一个M[i,j]
c)如果M[i,j]=M[i-1,j-1]
,则我们沿对角线选择我们的下一个M[i,j]
d)如果有可能对角线或垂直方向,我们宁愿在这种情况下对角线,因为匹配。我们继续这样直到我们到达第1行,然后返回相应的j
位置。
def test():
M=[[0, 0, 0, 0, 0 ,0, 0, 0],[1, 1, 1, 1, 0, 1, 1, 0],[2, 2, 1, 2, 1, 0, 1, 1]]
positions = [(1, 4), (1, 7), (2, 2), (2, 5)]
list=[]
k=1
for i in range(2,-1,-1):
for j in range(7,-1,-1):
if M[2][j]<=1 and M[2][j]!=0:#checking last row for tracing back
if (i+1,j+1) not in positions:
if M[i][j]== M[i][j-1]+1 and M[i][j]!=M[i-1][j]+1 and M[i][j]!=M[i-1][j-1]+1:#move horizontally
j -= 1
if i==0:
list.append(j+1)
elif M[i][j]!= M[i][j-1]+1 and M[i][j]==M[i-1][j]+1 and M[i][j]!=M[i-1][j-1]+1:
i -= 1
if i==0:
list.append(j+1)
elif M[i][j]== M[i][j-1]+1 and M[i][j]!=M[i-1][j]+1 and M[i][j]==M[i-1][j-1]+1:#
j -= 1
if i==0:
list.append(j+1)
elif M[i][j]== M[i][j-1]+1 and M[i][j]==M[i-1][j]+1 and M[i][j]!=M[i-1][j-1]+1:
i -= 1
if i==0:
list.append(j+1)
elif M[i][j]!= M[i][j-1]+1 and M[i][j]==M[i-1][j]+1 and M[i][j]==M[i-1][j-1]+1:
i -= 1
if i==0:
list.append(j+1)
elif M[i][j]== M[i][j-1]+1 and M[i][j]!=M[i-1][j]+1 and M[i][j]==M[i-1][j-1]+1:
j -= 1
if i==0:
list.append(j+1)
else :
print "error"
elif (i+1,j+1) in positions:
if M[i][j]== M[i][j-1]+1 and M[i][j]!=M[i-1][j]+1 and M[i][j]!=M[i-1][j-1]:
j -= 1
if i==0:
list.append(j+1)
elif M[i][j]!= M[i][j-1]+1 and M[i][j]==M[i-1][j]+1 and M[i][j]!=M[i-1][j-1]:
i -= 1
if i==0:
list.append(j+1)
elif M[i][j]!= M[i][j-1]+1 and M[i][j]!=M[i-1][j]+1 and M[i][j]==M[i-1][j-1]:#
j -= 1
i -= 1
if i==0:
list.append(j+1)
elif M[i][j]== M[i][j-1]+1 and M[i][j]==M[i-1][j]+1 and M[i][j]!=M[i-1][j-1]:
i -= 1
if i==0:
list.append(j+1)
elif M[i][j]!= M[i][j-1]+1 and M[i][j]==M[i-1][j]+1 and M[i][j]==M[i-1][j-1]:
i -= 1
j -= 1
if i==0:
list.append(j+1)
elif M[i][j]== M[i][j-1]+1 and M[i][j]!=M[i-1][j]+1 and M[i][j]==M[i-1][j-1]:
j -= 1
i -= 1
if i==0:
list.append(j+1)
elif M[i][j]== M[i][j-1]+1 and M[i][j]==M[i-1][j]+1 and M[i][j]==M[i-1][j-1]:
j -= 1
i -= 1
if i==0:
list.append(j+1)
else :
print "error"
else:
pass
return list
print(test())
答案 0 :(得分:0)
我会和你直接对话,那段代码真的很难看。尝试使用此类NavMat编写方法,这样可以防止条件中的索引错误。
class NavMat():
mat = []
row = 0
col = 0
width = 0
height = 0
def __init__(self):
self.mat.append([])
# Initialize an empty 2x2 matrix
def set(self,a):
self.mat = a
self.width = len(a[0])
self.height = len(a[1])
def at(self,x,y):
self.row = x
self.col = y
return(self.mat[x][y])
# retruns element at x,y and sets row and column accordingly
def north(self):
if (self.row>0):
return(self.mat[self.row-1][self.col])
else:
return(None)
# Returns value north of current position (one row up*)
def south(self):
if (self.row<self.height):
return(self.mat[self.row+1][self.col])
else:
return(None)
# Returns value south of current position (one row down*)
def east(self):
if (self.col<self.width):
return self.mat[self.row][self.col+1]
else:
return(None)
# Returns value east of current position (one column right*)
def west(self):
if (self.col>0):
return(self.mat[self.row][self.col-1])
else:
return(None)
# Returns value west of current position (one column left*)
def northEast(self):
if(self.row>0 and self.col<self.width):
return(self.mat[self.row-1][self.col+1])
else:
return(None)
# Up and to the right
def southEast(self):
if(self.row<self.height-1 and self.col<self.width):
return(self.mat[self.row+1][self.col+1])
else:
return(None)
# down and to the right
def southWest(self):
if(self.row<self.height - 1 and self.col>0):
return(self.mat[self.row+1][self.col-1])
else:
return(None)
# Down and to the left
def northWest(self):
if(self.row>0 and self.col>0):
return(self.mat[self.row-1][self.col-1])
else:
return(None)
# Up and to the left
def prt(self):
s = ""
for k in self.mat:
s+="\n"
for k2 in k:
s+=str(k2)+"\t"
print(s)
#Prints the matrix
m = NavMat()
这样的类有点开销,但是它们使得编写剩下的代码显然不那么悲惨(特别是如果你以后必须回来读它,或者询问有关堆栈溢出的问题)。