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

📅  最后修改于: 2021-04-27 23:47:03             🧑  作者: Mango

给定一个从-infinity到+ infinity的数字线。您从0开始,可以向左或向右移动。条件是,在我采取行动时,您将采取步骤。
a)确定您是否可以达到给定的数字x
b)如果确实可以达到,则找到达到给定数x的最佳方法。例如,可以2个步骤达到3,(0,1)(1,3)和3可以达到3个步骤(0,-1),(-1,1)(1,4)。
资料来源:Flipkart面试问题

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

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

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

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

5)如果在任何时候我们位置的绝对值都超过了目的地的绝对值,那么很直觉的是从这里不可能出现最短的路径。因此,我们将步长设为INT_MAX,以便当我将两种可能性中的最小值时,将这一可能性消除。

如果我们不使用此最后一步,则程序将进入INFINITE递归并给出RUN TIME ERROR。

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

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提供上述算法和实现。
优化的解决方案:找到在无限线上达到目标的最小移动