博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指offer python版 矩阵中的路径
阅读量:4515 次
发布时间:2019-06-08

本文共 1140 字,大约阅读时间需要 3 分钟。

class Solution:    def hasPath(self, matrix, rows, cols, path):         assistMatrix = [True]*rows*cols         for i in range(rows):             for j in range(cols):                 if(self.hasPathAtAStartPoint(matrix,rows,cols, i, j, path, assistMatrix)):                     return True         return False     def hasPathAtAStartPoint(self, matrix, rows, cols, i, j, path, assistMatrix):         if not path:            return True         index = i*cols+j         if i<0 or i>=rows or j<0 or j>=cols or matrix[index]!=path[0] or assistMatrix[index]==False:             return False        assistMatrix[index] = False         if(self.hasPathAtAStartPoint(matrix,rows,cols,i+1,j,path[1:],assistMatrix) or self.hasPathAtAStartPoint(matrix,rows,cols,i-1,j,path[1:],assistMatrix) or self.hasPathAtAStartPoint(matrix,rows,cols,i,j-1,path[1:],assistMatrix) or self.hasPathAtAStartPoint(matrix,rows,cols,i,j+1,path[1:],assistMatrix)):            return True        assistMatrix[index] = True         return False    a=Solution()print(a.hasPath([1,2,3,4,5,6,7,8,9,10,11,12],3,4,[1,2,3,4,5] ))

 

转载于:https://www.cnblogs.com/xzm123/p/9848379.html

你可能感兴趣的文章
php 魔术方法 __autoload()
查看>>
js div拖动动画运行轨迹效果
查看>>
使用Struts 2框架实现文件下载
查看>>
把工程部署在tomcat的root路径下
查看>>
topcoder SRM 625 DIV2 AddMultiply
查看>>
Leetcode Climbing Stairs
查看>>
腾讯2013实习笔试题
查看>>
Recipe 1.9. Processing a String One Word at a Time
查看>>
Linux 下查看系统是32位 还是64 位的方法
查看>>
MySQL 引擎 和 InnoDB并发控制 简介
查看>>
Dave Python 练习二
查看>>
Integer类之成员变量
查看>>
菜根谭#179
查看>>
如何获取多个字符串中最长的共同子字符串?
查看>>
Android 开发笔记___textvieww__跑马灯效果
查看>>
[ JS 进阶 ] 闭包,作用域链,垃圾回收,内存泄露
查看>>
GitHub注册与Git安装
查看>>
ThinkPHP 更新数据 save方法
查看>>
Bshare自定义分享按钮
查看>>
11Qt样式表
查看>>