【问题标题】:Stack implementation in recursive function递归函数中的堆栈实现
【发布时间】:2020-08-27 06:51:32
【问题描述】:

我正在尝试使用深度优先搜索来实现递归回溯功能,但我陷入了需要知道我在矩阵中的先前位置的点。

这个想法是这样的:我有一个二维数组的矩阵,这是我的函数:

标记当前点,如果该点是我正在寻找的,我将矩阵中的点设置为解决方案的一部分,并将所有先前标记的点设置为解决方案的一部分。 否则我将函数调用到一个有效的相邻点。

问题是第三种情况:如果没有有效的相邻点,那么我需要将该点标记为错误并将函数调用到我之前的位置。为此,我认为我需要一个堆栈来跟踪我以前的动作,但我很难弄清楚如何在 f# 中这样做。

let rec  solve (x,y) =

         mark (x,y)

         if (x,y) = pointimlookingfor then
           for x in 0.. array width-1 do
               for y in 0..array height-1 do
                   if Myarray.[x,y]=markedpoint then
                      Myarray.[x,y]<-partofsolution

         else if (List.isEmpty(adjacentslist) then
              Myarray.[x,y]<-wrong point
              solve (the previous visited point)

         else 
              for (adjacentpoint) in adjacentslist do
                   solve(adjacentpoint)

有什么想法吗?

【问题讨论】:

    标签: recursion f# stack depth-first-search backtracking


    【解决方案1】:

    在大多数函数式语言中,默认列表类型是不可变的链表,由于它的构造,您可以将其用作简单的堆栈。

    cons 被压入堆栈,head 被从堆栈中弹出。 这样,我们就可以编写一个简单的堆栈模块了。

    module Stack =
        let empty = []
        let push item stack = item::stack
        let pop = function
        | [] -> failwith "No items in stack"
        | x::xs -> xs
        let peek stack = stack |> List.tryHead
    

    所以,

    Stack.empty |> Stack.push 1 |> Stack.push 2 |> Stack.pop |> Stack.pop = Stack.empty //true
    

    在实际实践中,最简单的方法是在递归/折叠时随身携带的某个累加器上使用模式匹配,而不是显式使用上述函数。

    例如,让我们为堆栈重新创建一个经典用例 - balancing parenthesis。 每次遇到左大括号,就压入堆栈,遇到右大括号时,从堆栈中弹出,看看它是否与您压入的最后一个匹配。如果不匹配,则不平衡。

    let rec isBalanced stack = function
    | '(' | '{' | '[' as opened -> opened::stack //push into stack
    | ')' | '}' | ']' as closed -> 
        match stack with
        | opened::rest as all -> //pop from stack
            match opened, closed with
            | '(', ')' 
            | '{', '}' 
            | '[', ']' -> rest
            | _ -> failwith "Mismatched braces"
        | [] -> failwith "Closing before open"
    | _ -> stack
    
    "abc() { [ 1; 2; 3] }" |> Seq.fold (isBalanced) [] 
    

    有更简洁的写法,但这说明了如何模拟具有不可变结构的经典堆栈。

    在您的情况下,您可以将 (x,y) 元组压入堆栈,并通过解构它让算法回溯:(x,y)::tail

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 2014-03-14
      • 2017-09-12
      • 2015-12-17
      • 2016-11-14
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多