【发布时间】:2016-08-07 15:20:00
【问题描述】:
我最近在一次采访中被问到this question:
迷宫以 N*N 块的二进制矩阵形式给出,其中源块是 最左上角的块,即 maze[0][0] 和目标块是 最右下方的块,即迷宫[N-1][N-1]。老鼠从源头开始 并且必须到达目的地。老鼠只能向两个方向移动: 向前和向下。 (或高级问题中的 4 个)。在迷宫矩阵中,0 表示该块是死胡同,1 表示该块可以在 从源到目的地的路径。
我的答案是链接中提到的那个,它使用回溯。来自链接的高级伪代码:
If destination is reached
print the solution matrix
Else
a) Mark current cell in solution matrix as 1.
b) Move forward in horizontal direction and recursively check if this
move leads to a solution.
c) If the move chosen in the above step doesn't lead to a solution
then move down and check if this move leads to a solution.
d) If none of the above solutions work then unmark this cell as 0
(BACKTRACK) and return false.
面试官对我的回答非常惊讶,显然期待多项式时间解。
这个问题是否存在多项式时间解?
【问题讨论】:
-
花时间在此处的帖子中包含所有必要的详细信息,并避免依赖外部网站。请参阅How to Ask 页面。
-
@amit 感谢您抽出时间来编辑我的问题并给出答案。@ray 我一定会在以后的帖子中遵循您的建议和如何提问指南。
标签: algorithm time-complexity path-finding