# 剑指Offer(Python多种思路实现):矩阵中的路径

### 解题思路一：回溯法，使用递归。

``````class Solution:
def hasPath(self, matrix, rows, cols, path):
# write code here
if not matrix or rows<0 or cols<0 or path==None:
return False
markmatrix=[0]*(rows*cols)
pathIndex=0

for row in range(rows):
for col in range(cols):
if self.hasPathCore(matrix,rows,cols,row,col,path,pathIndex,markmatrix):
return True
return False

def hasPathCore(self,matrix,rows,cols,row,col,path,pathIndex,markmatrix):
if pathIndex==len(path):
return True
hasPath=False
if row>=0 and row<rows and col>=0 and col<cols and matrix[row*cols+col]==path[pathIndex] and not markmatrix[row*cols+col]:
pathIndex+=1
markmatrix[row*cols+col]=True
hasPath=self.hasPathCore(matrix,rows,cols,row+1,col,path,pathIndex,markmatrix) or \
self.hasPathCore(matrix, rows, cols, row-1, col,path, pathIndex, markmatrix) or \
self.hasPathCore(matrix, rows, cols, row, col+1, path, pathIndex, markmatrix) or \
self.hasPathCore(matrix, rows, cols, row, col-1, path, pathIndex, markmatrix)
if not hasPath:
pathIndex -=1
markmatrix[row*cols+col]=False
return hasPath``````

### 解题思路二：

``````class Solution:
def hasPath(matrix: str, rows: int, cols: int, path: str) -> 'bool':
for i in range(rows):   # 双重循环找矩阵元素
for j in range(cols):
if matrix[i*cols + j] == path[0]:
if spread(list(matrix), rows, cols, path[1:], i, j):
return True
return False

def spread(matrix: str, rows: int, cols: int, path: str, i: int, j: int) -> 'bool': # 这里的matrix是复制矩阵
if not path:
return True
matrix[i*cols + j] = '-'    # 已访问，覆盖