📌  相关文章
📜  从给定点到原点所需的最小步数

📅  最后修改于: 2021-04-18 02:28:16             🧑  作者: Mango

给定两个整数AB表示第一象限中一个点的坐标,任务是找到到达原点所需的最小步数。从(i,j)点开始的所有可能移动都是(i – 1,j),(i,j – 1)(i,j) (停留在同一位置)。
注意:不允许在同一方向上连续两次移动。

例子:

天真的方法:最简单的方法是递归。想法是递归地考虑从每个点开始的所有可能的移动,并针对每个移动计算出到达原点所需的最小步数。
时间复杂度: O(3 max(A,B) )
辅助空间: O(1)

高效方法:为了优化上述方法,该思想基于以下观察结果:如果x和y坐标之间绝对差为1或0 ,则到达原点所需的最小步数为(a + b) 。否则,它需要(2 * abs(a – b)– 1)移动到(k,k) ,其中ka,b的最小值。

请按照以下步骤解决问题:

  • 初始化变量ans ,该变量存储从(a,b)到达原点所需的最小步数。
  • 如果ab的绝对差为10 ,则将ans更新为(a + b)
  • 除此以外:
    • 找到a,b的最小值,并将其存储在变量k中
    • 使用公式更新ans =(2 * abs(a – b)– 1)+(2 * k)
  • 完成上述步骤后,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum moves
// required to reach origin from (a, b)
void findMinMoves(int a, int b)
{
    // Stores the minimum number of moves
    int ans = 0;
 
    // Check if the absolute
    // difference is 1 or 0
    if (a == b || abs(a - b) == 1) {
        ans = a + b;
    }
 
    else {
 
        // Store the minimum of a, b
        int k = min(a, b);
 
        // Store the maximum of a, b
        int j = max(a, b);
 
        ans = 2 * k + 2 * (j - k) - 1;
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given co-ordinates
    int a = 3, b = 5;
 
    // Function Call
    findMinMoves(a, b);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
  // Function to find the minimum moves
  // required to reach origin from (a, b)
  static void findMinMoves(int a, int b)
  {
 
    // Stores the minimum number of moves
    int ans = 0;
 
    // Check if the absolute
    // difference is 1 or 0
    if (a == b || Math.abs(a - b) == 1)
    {
      ans = a + b;
    }
 
    else
    {
 
      // Store the minimum of a, b
      int k = Math.min(a, b);
 
      // Store the maximum of a, b
      int j = Math.max(a, b);
      ans = 2 * k + 2 * (j - k) - 1;
    }
 
    // Print the answer
    System.out.print(ans);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
     
    // Given co-ordinates
    int a = 3, b = 5;
 
    // Function Call
    findMinMoves(a, b);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
 
# function to find the minimum moves
# required to reach origin from (a, b)
def findMinMoves(a, b):
   
    # Stores the minimum number of moves
    ans = 0
 
    # Check if the absolute
    # difference is 1 or 0
    if (a == b or abs(a - b) == 1):
        ans = a + b
    else:
        # Store the minimum of a, b
        k = min(a, b)
 
        # Store the maximum of a, b
        j = max(a, b)
        ans = 2 * k + 2 * (j - k) - 1
 
    # Prthe answer
    print (ans)
 
# Driver Code
if __name__ == '__main__':
   
    # Given co-ordinates
    a,b = 3, 5
 
    # Function Call
    findMinMoves(a, b)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find the minimum moves
  // required to reach origin from (a, b)
  static void findMinMoves(int a, int b)
  {
 
    // Stores the minimum number of moves
    int ans = 0;
 
    // Check if the absolute
    // difference is 1 or 0
    if (a == b || Math.Abs(a - b) == 1)
    {
      ans = a + b;
    }
 
    else
    {
 
      // Store the minimum of a, b
      int k = Math.Min(a, b);
 
      // Store the maximum of a, b
      int j = Math.Max(a, b);
      ans = 2 * k + 2 * (j - k) - 1;
    }
 
    // Print the answer
    Console.Write(ans);
  }
 
  // Driver Code
  public static void Main()
  {
    // Given co-ordinates
    int a = 3, b = 5;
 
    // Function Call
    findMinMoves(a, b);
  }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
9

时间复杂度: O(1)
辅助空间: O(1)