📌  相关文章
📜  到达目的地的最少步骤

📅  最后修改于: 2021-09-17 07:09:53             🧑  作者: Mango

给定从 -infinity 到 +infinity 的数轴。您从 0 开始,可以向左或向右移动。条件是,在第 i 个动作中,您走 i 个步骤。
a) 查找是否可以达到给定的数字 x
b) 如果我们确实可以达到,找到达到给定数字 x 的最佳方式。例如,3 可以通过 2 个步骤达到,(0, 1) (1, 3) 和 4 可以通过 3 个步骤 (0, -1), (-1, 1) (1, 4) 达到。
来源:Flipkart 面试题

需要注意的重要一点是我们可以到达任何目的地,因为总是可以进行长度为 1 的移动。在任何步骤 i,我们可以向前移动 i,然后向后移动 i + 1。
下面是 Arpit Thapar 在这里提出的递归解决方案。
1) 由于+ 5 和- 5 到0 的距离相同,因此我们找到目的地绝对值的答案。

2)我们使用一个递归函数作为参数:
i) 源顶点
ii) 采取的最后一步的价值
iii) 目的地

3)如果在任何一点源顶点=目标;返回步数。

4)否则我们可以在两个可能的方向上进行。在这两种情况下都采取最少的步骤。
从任何顶点我们可以去:
(当前源+最后一步+1)和
(当前来源 – 最后一步 -1)

5)如果在任何时候,我们位置的绝对值超过了我们目的地的绝对值,那么从这里出发的最短路径是不可能的,这是直观的。因此,我们将steps的值设为INT_MAX,这样当我取两种可能性中的最小值时,就会消除这个。

如果我们不使用这最后一步,程序将进入无限递归并给出运行时间错误。

下面是上述想法的实现。请注意,该解决方案仅计算步数。

C++
// C++ program to count number of
// steps to reach a point
#include
using namespace std;
 
// Function to count number of steps
// required to reach a destination
 
// source -> source vertex
// step -> value of last step taken
// dest -> destination vertex
int steps(int source, int step, int dest)
{
    // base cases
    if (abs(source) > (dest))
         return INT_MAX;
    if (source == dest) return step;
 
    // at each point we can go either way
 
    // if we go on positive side
    int pos = steps(source + step + 1,
                      step + 1, dest);
 
    // if we go on negative side
    int neg = steps(source - step - 1,
                      step + 1, dest);
 
    // minimum of both cases
    return min(pos, neg);
}
 
// Driver code
int main()
{
    int dest = 11;
    cout << "No. of steps required to reach "
                            << dest << " is "
                        << steps(0, 0, dest);
    return 0;
}


Java
// Java program to count number of
// steps to reach a point
import java.io.*;
 
class GFG
{
 
    // Function to count number of steps
    // required to reach a destination
     
    // source -> source vertex
    // step -> value of last step taken
    // dest -> destination vertex
    static int steps(int source, int step,
                                int dest)
    {
        // base cases
        if (Math.abs(source) > (dest))
            return Integer.MAX_VALUE;
     
        if (source == dest)
            return step;
 
        // at each point we can go either way
 
        // if we go on positive side
        int pos = steps(source + step + 1,
                        step + 1, dest);
 
        // if we go on negative side
        int neg = steps(source - step - 1,
                        step + 1, dest);
 
        // minimum of both cases
        return Math.min(pos, neg);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int dest = 11;
        System.out.println("No. of steps required"+
                                " to reach " + dest +
                       " is " + steps(0, 0, dest));
    }
}
 
// This code is contributed by Prerna Saini


Python3
# python program to count number of
# steps to reach a point
import sys
 
# Function to count number of steps
# required to reach a destination
     
# source -> source vertex
# step -> value of last step taken
# dest -> destination vertex
def steps(source, step, dest):
     
    #base cases
    if (abs(source) > (dest)) :
        return sys.maxsize
     
    if (source == dest):
        return step
 
    # at each point we can go
    # either way
 
    # if we go on positive side
    pos = steps(source + step + 1,
                    step + 1, dest)
 
    # if we go on negative side
    neg = steps(source - step - 1,
                    step + 1, dest)
 
    # minimum of both cases
    return min(pos, neg)
     
 
# Driver Code
dest = 11;
print("No. of steps required",
               " to reach " ,dest ,
        " is " , steps(0, 0, dest));
     
 
# This code is contributed by Sam007.


C#
// C# program to count number of
// steps to reach a point
using System;
 
class GFG
{
    // Function to count number of steps
    // required to reach a destination
     
    // source -> source vertex
    // step -> value of last step taken
    // dest -> destination vertex
    static int steps(int source, int step,
                                int dest)
    {
        // base cases
        if (Math.Abs(source) > (dest))
            return int.MaxValue;
     
        if (source == dest)    
            return step;
 
        // at each point we can go either way
 
        // if we go on positive side
        int pos = steps(source + step + 1,
                        step + 1, dest);
 
        // if we go on negative side
        int neg = steps(source - step - 1,
                        step + 1, dest);
 
        // minimum of both cases
        return Math.Min(pos, neg);
    }
 
    // Driver Code
    public static void Main()
    {
        int dest = 11;
        Console.WriteLine("No. of steps required"+
                             " to reach " + dest +
                      " is " + steps(0, 0, dest));
    }
}
 
// This code is contributed by Sam007


PHP
 source vertex
// step -> value of last step taken
// dest -> destination vertex
function steps($source, $step, $dest)
{
    // base cases
    if (abs($source) > ($dest))
        return PHP_INT_MAX;
    if ($source == $dest)
        return $step;
 
    // at each point we
    // can go either way
 
    // if we go on positive side
    $pos = steps($source + $step + 1,
                   $step + 1, $dest);
 
    // if we go on negative side
    $neg = steps($source - $step - 1,
                   $step + 1, $dest);
 
    // minimum of both cases
    return min($pos, $neg);
}
 
// Driver code
$dest = 11;
echo "No. of steps required to reach ",
     $dest, " is ", steps(0, 0, $dest);
 
// This code is contributed by aj_36
?>


Javascript


输出 :

No. of steps required to reach 11 is 5

感谢 Arpit Thapar 提供上述算法和实现。
优化解决方案:在无限线上找到达到目标的最小移动

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程