【问题标题】:Make Logic in Function Recursive使函数递归中的逻辑
【发布时间】:2013-01-10 07:43:01
【问题描述】:

背景:假设我有一个小机器人。我将此机器人放置在地图(图表)中的某个节点处。机器人可以调用 giveMeMapCopy() 方法来获取他所在的整个地图的副本。我想给我的小机器人一个函数,通过它他可以使用广度优先遍历来找到到 Exit 节点的最短路径.以下是此类地图的示例:

我在 YouTube 上观看了有关如何对图进行广度优先遍历的视频,因此我很清楚需要做什么。问题是,我发现很难让我的逻辑递归。这是我的代码:

public class Robot
{
    // fields required for traversal
    private Queue<ArrayList<String>> queue;
    private ArrayList<ArrayList<String>> result;
    private String workingNode;
    private ArrayList<String> routeSoFar;

    private Queue<String> knownShortestPath;

    public Robot() {
        queue = new LinkedList<ArrayList<String>>();
        result = new ArrayList<ArrayList<String>>();
        routeSoFar = new ArrayList<String>();
        knownShortestPath = new LinkedList<String>();
    }

    // Runs when Robot has reached a node.
    public void enterNodeActions() {
        knownShortestPath = determineIdealPath();
    }

    // Runs to determine where to go next
    public String chooseNextNode() {
        if(!knownShortestPath.isEmpty())
        {
            // TODO: Need to go through the 
        }
    }

    public LinkedList<String> determineIdealPath()
    {
        try {
            // Get the map
            Map m = giveMeMapCopy();

            // Get all entry nodes of map
            Set<String> entryNodes = m.getEntryNodes();

            /*
             * Loop through all Entry nodes, and find out where we are.
             * Set that as current working node.
             */
            for (String n : entryNodes) {
                if(n == getMyLocation())
                {
                    workingNode = n;
                }
            }

            // All enighbours of working node.
            Set<String> neighboursNames = getNeighboursNames(workingNode);

            /*
             * For each neighbour, construct a path from working node to the neighbour node
             * And add path to Queue and Result (if not already present).
             */
            for(String node : neighboursNames)
            {
                if(!node.equals(getMyLocation()))
                {
                    ArrayList<String> route = new ArrayList<String>();
                    route.add(getMyLocation());
                    route.add(node);
                    if(!containsRoute(result, route))
                    {
                        if(!containsRoute(queue, route))
                        {
                            queue.add(route);                           
                        }
                        result.add(route);
                    }
                }
            }       
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我希望递归发生的地方是在我遍历了入口节点 [A] 的所有邻居之后,我想移动到下一个 [B] 并为此做同样的事情,即遍历它的每个邻居(忽略 A,因为它已经存在于 Result 列表中)并将它们添加到 Queue 和 Result 列表中。

我希望问题很清楚,如果没有,请告诉我,我会尽力澄清任何不清楚的地方。

【问题讨论】:

  • 比看视频好,你还应该阅读一些递归编程的介绍
  • 如果您在制定递归时遇到问题,那么为什么不尝试使用队列的迭代版本呢?
  • 我想学习实现目标的巧妙方法。我的机器人将在它到达的每个节点上做同样的事情,因此我认为递归是最好的。
  • 1. “整洁的方式”不是最好的方式 2. 做同样的事情是迭代的目的
  • 谢谢,我认为你是对的,我错了。

标签: java recursion computer-science traversal


【解决方案1】:

广度优先搜索通常在没有递归的情况下完成,因为它基于队列(在您的情况下为部分路径)。另一方面,深度优先搜索基于堆栈,可以很自然地使用递归函数的调用堆栈来实现。

【讨论】:

    【解决方案2】:

    本质上,您想要实现Dijkstra's algorithm 或类似的东西,以在图中找到源和目标之间的最短路径。

    你可以在 Java here 中找到这样的实现。只需将所有权重设置为 1

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2015-06-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 2012-12-12
      • 2012-08-02
      • 2018-01-20
      • 1970-01-01
      相关资源
      最近更新 更多