📜  将圆心移动到目标的最小转数

📅  最后修改于: 2021-10-23 08:32:00             🧑  作者: Mango

给定一个半径为 r 的圆,圆心在 point(x1, y1) 并给定一个点 (x2, y2)。任务是使用最少的步数将圆心从给定的中心 (x1, y1) 移动到目标 (x2, y2)。在一个步骤中,我们可以在圆的边界上的任何一点放置一个大头针,然后将圆围绕该大头针旋转任意角度,最后取下大头针。
例子 :

Input : r = 2 
        x1 = 0, y1 = 0
        x2 = 0, y2 = 4
Output :1

Input  : r = 1
         x1 = 1, y1 = 1,
         x2 = 4, y2 = 4 
Output : 3

让我们考虑两个中心之间的直线。显然,要以最大距离移动中心,我们需要将它绕线旋转 180 度。所以我们每次可以移动中心的最大距离是 2 * r。让我们每次以 2 * r 距离继续移动中心,直到两个圆相交。现在显然我们可以通过围绕两个圆的交点之一旋转中心来使中心移动到其最终位置,直到它到达最终目的地。
每次我们使圆移动 2 * r 次,除了最后一次移动,它将 < 2 * r。令两点间的初始距离为 d。该问题的解决方案将是 ceil(d/2*r)。

C++
// C++ program to find minimum number of
// revolutions to reach a target center
#include
using namespace std;
 
// Minimum revolutions to move center from
// (x1, y1) to (x2, y2)
int minRevolutions(double r, int x1, int y1,
                             int x2, int y2)
{
    double d = sqrt((x1 - x2)*(x1 - x2) +
                    (y1 - y2)*(y1 - y2));
    return ceil(d/(2*r));
}
 
// Driver code
int main()
{
    int r = 2, x1 = 0, y1 = 0, x2 = 0, y2 = 4;
    cout << minRevolutions(r, x1, y1, x2, y2);
    return 0;
}


Java
// Java program to find minimum number of
// revolutions to reach a target center
class GFG {
     
    // Minimum revolutions to move center
    // from (x1, y1) to (x2, y2)
    static double minRevolutions(double r,
         int x1, int y1, int x2, int y2)
    {
         
        double d = Math.sqrt((x1 - x2)
                   * (x1 - x2) + (y1 - y2)
                   * (y1 - y2));
                    
        return Math.ceil(d / (2 * r));
    }
     
    // Driver Program to test above function
    public static void main(String arg[]) {
         
        int r = 2, x1 = 0, y1 = 0;
        int x2 = 0, y2 = 4;
         
        System.out.print((int)minRevolutions(r,
                              x1, y1, x2, y2));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find
# minimum number of
# revolutions to reach
# a target center
import math
 
# Minimum revolutions to move center from
# (x1, y1) to (x2, y2)
def minRevolutions(r,x1,y1,x2,y2):
 
    d = math.sqrt((x1 -x2)*(x1 - x2) +
         (y1 - y2)*(y1 - y2))
    return math.ceil(d/(2*r))
 
# Driver code
 
r = 2
x1 = 0
y1 = 0
x2 = 0
y2 = 4
 
print(minRevolutions(r, x1, y1, x2, y2))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find minimum number of
// revolutions to reach a target center
using System;
 
class GFG {
     
    // Minimum revolutions to move center
    // from (x1, y1) to (x2, y2)
    static double minRevolutions(double r,
            int x1, int y1, int x2, int y2)
    {
         
        double d = Math.Sqrt((x1 - x2)
                * (x1 - x2) + (y1 - y2)
                * (y1 - y2));
                     
        return Math.Ceiling(d / (2 * r));
    }
     
    // Driver Program to test above function
    public static void Main()
    {
        int r = 2, x1 = 0, y1 = 0;
        int x2 = 0, y2 = 4;
         
        Console.Write((int)minRevolutions(r,
                            x1, y1, x2, y2));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出 :

1

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