【发布时间】:2015-05-09 03:28:02
【问题描述】:
最近遇到以下面试问题?
问题:给您一个包含 M 行 N 列的二维矩阵。您最初位于 (0,0),这是数组中的左上角单元格。您可以向右或向下移动。数组用 1 和 0 填充。 1 表示您可以通过该单元格,0 表示您不能通过该单元格。返回从左上角单元格到右下角单元格的路径数。(即(0,0)到(M-1,N-1))。由于答案可能很大,因此您必须返回 ans%(10^9+7)。
谁能告诉我如何处理或任何可能有帮助的算法?
编辑:
I have an approach
1.Start with the top-left cell,initialize count=0
do
2.Check if 1 exists in adjacent right or adjacent down cell.
3.Add the tuple (i,j) in the stack and choose the right path.
4.If we reach the bottom right cell , update count and pop from stack and go to that position (i,j).
while(stack is not empty)
5.Print count
我想知道是否有人有其他方法?
【问题讨论】:
-
这是极其微不足道的,在任何时候都不涉及回溯。究竟是什么让你难过?
-
我正在寻找不同的解决方案
标签: python c algorithm matrix depth-first-search