[[0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0],
[0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 2]] 给定类似上面个一个 m by n matrix。其中0代表可以走的路,1代表不能走的墙。任意给出起点坐标(i, j)判段是否能从起点走到用2标出来的目标点?
 grid = [[0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0],
[0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 2]] def dfsSearch(x, y): if grid[x][y] == 2:
print('found at %d,%d' %(x, y))
return True
elif grid[x][y] == 1:
print('Wall at %d,%d' %(x, y))
elif grid[x][y] == 3:
print('Visited before: %d, %d' %(x, y))
return False print('Visiting %d,%d' %(x, y)) # mark as visited
grid[x][y] = 3 # explore neighbors clockwise starting by the one on the right
if ((x < len(grid) - 1) and dfsSearch(x + 1, y)) or \
(y > 0 and dfsSearch(x, y - 1)) or \
(x > 0 and dfsSearch(x - 1, y)) or \
((y < len(grid) - 1) and dfsSearch(x, y + 1)):
return True return False

这个算法只是检查能不能找到符合要求的path,但是不保证找到的那一条path是最短的。注意把已经访问过的点设置成3,这样访问过的点就不会被重复访问了。

如果想要找出shortest path的长度,可以用如下的方法:

 class findShortestPath(object):
def __init__(self):
self.minLen = [0x7FFFFFFF] def shortPath(self, x, y):
minLen = [999999]
self.shortPathHelper(x, y, 0)
return min(minLen) def shortPathHelper(self, x, y, step):
nextStep = [[0, 1], [1, 0], [0, -1], [-1, 0]]
if grid[x][y] == 2:
print('found at %d,%d' % (x, y))
self.minLen.append(step)
return True
elif grid[x][y] == 1:
print('Wall at %d,%d' % (x, y))
elif grid[x][y] == 3:
print('Visited before: %d, %d' % (x, y))
return False grid[x][y] = 3 for k in range(4):
tx = x + nextStep[k][0]
ty = y + nextStep[k][1] if (tx < 0 or tx > len(grid)-1 or ty < 0 or ty > len(grid)-1):
continue
self.shortPathHelper(tx, ty, step + 1)

DFS经典题,reachable or not in a 2D maze的更多相关文章

  1. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  2. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  5. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

  6. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  7. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  8. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  9. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. Mysql数据库之Binlog日志使用总结

    binlog二进制日志对于mysql数据库的重要性有多大,在此就不多说了.下面根据本人的日常操作经历,并结合网上参考资料,对binlog日志使用做一梳理: 一.binlog日志介绍1)什么是binlo ...

  2. myeclipse 8.5离线安装python插件

    看到网上很多教程扯了一大堆,好麻烦的样子. 1)下载pyhon开发插件,PyDev 2.2.0.zip 我在百度137708820网盘里已分享,链接http://pan.baidu.com/s/1mg ...

  3. IO调度器

    由于对blktrace的好奇,来到了block层.通过阅读block层的代码,自己的几个错误认知被纠正,比如 一) 同步操作时,进程是在驱动中睡觉真实情况是:进程在文件系统睡觉 二) 对同一个数据块的 ...

  4. SQL Server对Xml字段的操作

    T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和 ...

  5. TinyFrame升级之七:重构Repository和Unit Of Work

    首先,重构的想法来源于以下文章:Correct use of Repository and Unit Of Work patterns in ASP.NET MVC,因为我发现在我的框架中,对Unit ...

  6. TinyFrame升级之十:WCF Rest Service注入IOC的心

    由于在实际开发中,Silverlight需要调用WebService完成数据的获取,由于之前我们一直采用古老的ASMX方式,生成的代理类不仅难以维护,而且自身没有提供APM模式的调用方式,导致在Sin ...

  7. 基于SignalR的小型IM系统

    这个IM系统真是太轻量级了,提供的功能如下: 1.聊天内容美化 2.用户上下线提示 3.心跳包检测机制 4.加入用户可群聊 下面来一步一步的讲解具体的制作方法. 开篇准备工作 首先,巧妇难为无米之炊, ...

  8. express:webpack dev-server开发中如何调用后端服务器的接口?

    开发环境:     前端:webpack + vue + vue-resource,基于如下模板创建的开发环境: https://github.com/vuejs-templates/webpack  ...

  9. Install Visual Studio For Mac Preview

    在Hack News上看到Visual Studio For Mac Preview的链接,上面有许多评论,纪录下尝鲜安装过程. 第一次尝试 VisualStudioforMacPreviewInst ...

  10. 802.11 对于multicast 和 broadcast的处理

    ethernet内部会有broadcast 和 multicast.这两种包都是一个STA向多个STA发包. 当没有wifi存在的时候,LAN口之间的broadcast 和 multicast是可靠转 ...