该算法的最佳方法应该是什么? DFS 还是 BFS?为什么?

What should be the best approach for this algorithm? DFS or BFS? and why?

在花盆里种花时,重要的是要确保 每当你给你的植物浇水时,任何不 被根部吸收,从花盆底部排出。 否则水会积在锅底, 导致你的植物腐烂。

You recently decided to plant some flowers of your own,
and decided to fill the base of the pot with gravel. You've
decided to write code to verify whether water will
successfully drain out of the pot.
Using a 2D array to represent your pot, individual pieces of
gravel are notated with a 1 and empty spaces between
gravel are notated with a 0.
Example Pot #1:

[
[0, 1, 1, 1, 1],
[0, 1, 0, 0, 0],
[0, 0, 0, 1, 0],
[1, 1, 1, 1, 0],
[1, 0, 0, 1, 0],
]

Write a function to determine whether the water can fall
from the top row to the bottom, moving through the
spaces between the gravel. Taking the example pot from
above, you can see the posible path, which is marked by
replacing the relevant 0's with asterisks (*)

[
[*, 1, 1, 1, 1],
[*, 1, *, *, *],
[*, *, *, 1, *],
[1, 1, 1, 1, *],
[1, 0, 0, 1, *],
]

注意路径包括顶部和底部 行。 允许的动作: 唯一允许的移动是向上、向下、向左和向右。 不允许使用对角线。

Here are a few pots that don't drain properly, along with
explanations.

[
[1, 1, 1],
[1, 1, 0],
[1, 0, 0],
]

Explanation: The top row has no gaps

[
[1, 1, 0],
[1, 1, 0],
[1, 1, 1],
]

Explanation: The bottom row has no gaps
[
[1, 1, 0],
[1, 1, 0],
[1, 0, 1],
]

这两种解决方案都有效,并且都可以通过就地修改矩阵,仅使用恒定数量的额外内存来实现。既然任务是修改矩阵,不妨利用一下这个能力

BFS 稍微简单一些,会找到最短路径 -- 不是必需的,但不会有什么坏处。