简体   繁体   English

到达目的地的最少步骤数?

[英]Minimum number of steps to reach destination?

I am learning data Structures from few days.Suppose, we have a Data structure that is built from layer of nodes. 我从几天开始学习数据结构,假设我们有一个从节点层构建的数据结构。 All layers start with 'start' node and end with a null value instead of last node. 所有层均以“开始”节点开始,并以空值而不是最后一个节点结束。 Each node has a 'value', 'foll' pointer to next node and 'down' pointer to the next node in the same layer. 每个节点在同一层中都有一个“值”,指向下一个节点的“ foll”指针和指向下一个节点的“向下”指针。 On each layer, values are sorted in ascending order. 在每一层上,值按升序排序。

Example: 例:

  1. S ---- 9 -------------------------------------->NULL S ---- 9 --------------------------------------> NULL

      | 
  2. S -----9---------27---------51-------------->NULL S ----- 9 --------- 27 --------- 51 --------------> NULL

      | | | 
  3. S -----9---23---27--29---51----53------->NULL S ----- 9 --- 23 --- 27--29 --- 51 ---- 53 -------> NULL

Data Structure for Node: 节点的数据结构:

   `      Class Node {
                  int value;
                   Node foll;
                      Node down;
                  }                 `

Write a function findNode that will get a starting node head and a search value value and will return the minimum number of jumps that are needed in order to either reach node with that value or to determine that it does not exist in the data structure. 编写一个函数findNode ,该函数将获取一个起始节点头和一个搜索值 ,并将返回所需的最小跳转次数,以达到具有该值的节点或确定其在数据结构中不存在。

Input : Number of layers, followed by a list of new nodes for each layer and finally the number that should be found. 输入 :层数,然后是每层新节点的列表,最后是应找到的数目。 All of the values are integers larger than zero. 所有值都是大于零的整数。

4

7

27 51

24 80

4 32 54 69 82

54

will result in data structure described above and value to be found is 54 将导致上述数据结构,并且找到的值是54

Output 产量

7

Can anyone please tell me, what should be following function in Java? 谁能告诉我,Java中的以下函数应该是什么?

static int findNode(Node start, int value) { }

The easiest way to approach this problem would be with a recursive solution. 解决此问题的最简单方法是使用递归解决方案。 Think about this problem as starting at one node, and then expanding in every direction possible until it reaches the node it's looking for. 考虑一下这个问题,它从一个节点开始,然后在所有可能的方向上扩展,直到到达要查找的节点。

The first thing you need in any recursive function is a base case, or what to do when it can't go any further. 在任何递归函数中,您需要做的第一件事是基本情况,或者在无法继续进行时应采取的措施。 In our case, if we reach the end without finding the node we want, we could break, or return 1 if we find the node we want. 在我们的例子中,如果到达末尾而没有找到所需的节点,则可能会中断,或者如果找到所需的节点,则返回1。

The next part is the recursive call. 下一部分是递归调用。 So if we have not found what we want and we have more places to go, then we can keep go to the surrounding nodes and adding 1 to our depth with every call. 因此,如果我们找不到想要的东西并且还有更多的地方可以去,那么我们可以继续前往周围的节点,并在每次调用时将深度加1。

In code, this would be something similar to this: 在代码中,这将类似于以下内容:

static int findNode(Node start, int value){
    if(start.value == value)
        return 1;
    else if(Node == null)
        break;

    return Math.min(findNode(start.foll) + 1, findNode(start.down) + 1);
}

You might have to change this a little bit and exchange break with another way of stopping, but hopefully this give you the idea. 您可能需要对此稍作更改,并以另一种停止方式来交换休息,但希望这能给您带来灵感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM