📌  相关文章
📜  到达目的地的最小跳块数

📅  最后修改于: 2021-04-27 09:13:40             🧑  作者: Mango

在二维空间中给定N条线以及一个起点和终点。这N条线将空间分成几个块。我们需要打印从起始点到目标点的最小跳转次数。仅当它们共享一侧时,我们才能从一个块跳到另一个块。
例子:

输入:线= [x = 0,y = 0,x + y – 2 = 0]起点= [1,1],终点= [-2,-1]输出:2我们需要跳2次( B4-> B3然后B3-> B5或B4-> B6然后B6-> B5)从下图所示的起点到达目标点。在图中为每个块i赋予了一个ID Bi。 空格键

我们可以使用线和点的属性来解决此问题,该属性表示为:如果将两个点放在线方程中,则它们将具有相同的符号,即,如果两个点位于同一侧,则求值的正-负或负-负如果符号不同(即正负),它们将位于行的另一侧。
现在我们可以使用上述属性来解决此问题,对于每一行,我们将检查起点和终点是否位于同一侧。如果它们位于线的另一侧,则必须跳越该线以使其更近。如上图所示,起点和终点在x + y – 2 = 0线的同一侧,因此不必跳过该线,而其余两行则需要跳过,因为两个点都位于相反的一侧。
最后,我们将检查每条线的评估点的符号,并在发现相反符号时增加跳数。此问题的总时间复杂度将是线性的。

C++
// C++ program to find minimum jumps to reach
// a given destination from a given source
#include 
using namespace std;
  
// To represent point in 2D space
struct point
{
    int x, y;
    point(int x, int y) : x(x), y(y)
    {}
};
  
// To represent line of (ax + by + c)format
struct line
{
    int a, b, c;
    line(int a, int b, int c) : a(a), b(b), c(c)
    {}
    line()
    {}
};
  
// Returns 1 if evaluation is greater > 0,
// else returns -1
int evalPointOnLine(point p, line curLine)
{
    int eval = curLine.a* p.x +
               curLine.b * p.y +
               curLine.c;
    if (eval > 0)
        return 1;
    return -1;
}
  
//  Returns minimum jumps to reach
//  dest point from start point
int minJumpToReachDestination(point start,
              point dest, line lines[], int N)
{
    int jumps = 0;
    for (int i = 0; i < N; i++)
    {
        // get sign of evaluation from point
        // co-ordinate and line equation
        int signStart = evalPointOnLine(start, lines[i]);
        int signDest = evalPointOnLine(dest, lines[i]);
  
        // if both evaluation are of opposite sign,
        // increase jump by 1
        if (signStart * signDest < 0)
            jumps++;
    }
  
    return jumps;
}
  
// Driver code to test above methods
int main()
{
    point start(1, 1);
    point dest(-2, -1);
  
    line lines[3];
    lines[0] = line(1, 0, 0);
    lines[1] = line(0, 1, 0);
    lines[2] = line(1, 1, -2);
  
    cout << minJumpToReachDestination(start, dest, lines, 3);
  
    return 0;
}


Java
// Java program to find minimum jumps to reach
// a given destination from a given source
class GFG
{
  
// To represent point in 2D space
static class point
{
    int x, y;
  
    public point(int x, int y) 
    {
        super();
        this.x = x;
        this.y = y;
    }
      
};
  
// To represent line of (ax + by + c)format
static class line
{
    public line(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
  
    int a, b, c;
      
    line()
    {}
};
  
// Returns 1 if evaluation is greater > 0,
// else returns -1
static int evalPointOnLine(point p, line curLine)
{
    int eval = curLine.a* p.x +
            curLine.b * p.y +
            curLine.c;
    if (eval > 0)
        return 1;
    return -1;
}
  
// Returns minimum jumps to reach
// dest point from start point
static int minJumpToReachDestination(point start,
            point dest, line lines[], int N)
{
    int jumps = 0;
    for (int i = 0; i < N; i++)
    {
        // get sign of evaluation from point
        // co-ordinate and line equation
        int signStart = evalPointOnLine(start, lines[i]);
        int signDest = evalPointOnLine(dest, lines[i]);
  
        // if both evaluation are of opposite sign,
        // increase jump by 1
        if (signStart * signDest < 0)
            jumps++;
    }
  
    return jumps;
}
  
// Driver code 
public static void main(String[] args)
{
    point start = new point(1, 1);
    point dest = new point(-2, -1);
  
    line []lines = new line[3];
    lines[0] = new line(1, 0, 0);
    lines[1] = new line(0, 1, 0);
    lines[2] = new line(1, 1, -2);
  
    System.out.print(minJumpToReachDestination(start, dest, lines, 3));
}
}
  
// This code is contributed by Rajput-Ji


C#
// C# program to find minimum jumps to reach
// a given destination from a given source
using System;
  
class GFG
{
  
// To represent point in 2D space
class point
{
    public int x, y;
  
    public point(int x, int y) 
    {
        this.x = x;
        this.y = y;
    }
      
};
  
// To represent line of (ax + by + c)format
class line
{
    public int a, b, c; 
    line()
    {}
    public line(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
};
  
// Returns 1 if evaluation is greater > 0,
// else returns -1
static int evalPointOnLine(point p, line curLine)
{
    int eval = curLine.a* p.x +
            curLine.b * p.y +
            curLine.c;
    if (eval > 0)
        return 1;
    return -1;
}
  
// Returns minimum jumps to reach
// dest point from start point
static int minJumpToReachDestination(point start,
            point dest, line []lines, int N)
{
    int jumps = 0;
    for (int i = 0; i < N; i++)
    {
        // get sign of evaluation from point
        // co-ordinate and line equation
        int signStart = evalPointOnLine(start, lines[i]);
        int signDest = evalPointOnLine(dest, lines[i]);
  
        // if both evaluation are of opposite sign,
        // increase jump by 1
        if (signStart * signDest < 0)
            jumps++;
    }
  
    return jumps;
}
  
// Driver code 
public static void Main(String[] args)
{
    point start = new point(1, 1);
    point dest = new point(-2, -1);
  
    line []lines = new line[3];
    lines[0] = new line(1, 0, 0);
    lines[1] = new line(0, 1, 0);
    lines[2] = new line(1, 1, -2);
  
    Console.Write(minJumpToReachDestination(start, dest, lines, 3));
}
}
  
// This code is contributed by Rajput-Ji


输出:

2