[[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. 编译php-5.6出错,xml2-config not found

    今天在centos上编译PHP-5.6 cd php-5.6 ./configure --prefix=/usr/local/php5./ --with-apxs2=/usr/local/apache ...

  2. php常见知识

    printf("%.nf",&f);这个n代表显示浮点数时,小数点后显示几位:0就是不显示小数点后的数,1就是显示小数点后1位:

  3. JPA, JNDI, OSGi

    JPA Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. JNDI Java Naming and Di ...

  4. Meet Python: little notes 3 - function

    Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...

  5. 表单 - Form - 无刷新提交原理

    为什么Form组件的表单提交可以做到无刷新? EasyUI在提交的时候,将表单作为一个隐藏的iframe进行的提交,并不是我们看到的那个表单进行的提交 并且那个iframe使用了绝对定位,保证页面上不 ...

  6. Div和Span标签显示与隐藏

    本实例中,学习jQuery的知识,显示与隐藏网页上的div或是span标签. 实际环境中,也许是根据某些条件进行,符合条件时,对某个或是某个div或是span标签时行显示与隐藏. 主要是学习jQuer ...

  7. [win]系统优化工具dism++

    系统优化工具, 确实能将c盘扩大个2-3g. 主要是删除日志 优化系统等功能. https://www.chuyu.me/

  8. File类

    存储在变量,数组和对象中的数据是暂时的,当程序终止时他们就会丢失.为了能够永久的保存程序中创建的数据,需要将他们存储到硬盘或光盘的文件中.这些文件可以移动,传送,亦可以被其他程序使用.由于数据存储在文 ...

  9. stm32调试记录一

    ..\..\SYSTEM\usart\usart.c(1): error:  #5: cannot open source input file "sys.h": No such ...

  10. 高性能JavaScript 重排与重绘

    先回顾下前文高性能JavaScript DOM编程,主要提了两点优化,一是尽量减少DOM的访问,而把运算放在ECMAScript这一端,二是尽量缓存局部变量,比如length等等,最后介绍了两个新的A ...