【问题标题】:FInd a path through a maze using recursion使用递归找到穿过迷宫的路径
【发布时间】:2015-02-22 00:33:28
【问题描述】:

我只是有一个关于递归方法的问题。我需要编写这种方法来找到穿过迷宫的路径。但是,它不接受任何参数。所以我想知道一个不带参数的方法可以递归吗?

【问题讨论】:

    标签: recursion path maze


    【解决方案1】:

    是的。在面向对象的语言中,您可以递归对象并使用它来保持和维护状态。

    这是一个 Ruby 的简单示例。

    require 'ostruct'
    
    class Demo < OpenStruct
        def recurse
            return 'finished' if self.value == 10
            puts 'recursing'
            self.value += 1
            self.recurse
        end
    end
    
    Demo.new(value: 1).recurse
    

    在其他情况下,您可以改为引用全局变量。

    【讨论】:

    • 我现在明白了,谢谢。
    • 如果我的回答有帮助,请随时投票并设置为答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    相关资源
    最近更新 更多