Given N lines and one starting point and destination point in 2-dimensional space. These N lines divide the space into some blocks. We need to print the minimum number of jumps to reach destination point from starting point. We can jump from one block to other block only if they share a side.
Input : Lines = [x = 0, y = 0, x + y – 2 = 0] Start point = [1, 1], Dest point = [-2, -1] Output : 2 We need to jump 2 times (B4 -> B3 then B3 -> B5 or B4 -> B6 then B6 -> B5) to reach destination point from starting point shown in below diagram. Each block i is given an id Bi in the diagram.
We can solve this problem using a property of lines and points which is stated as, If we put two points in line equation then they will have same sign i.e. positive-positive or negative-negative of evaluated value if both points lies on the same side of line, in case of different sign i.e. positive-negative they will lie on different side of line.
Now we can use above property to solve this problem, For each line, we will check if start and destination point lie on the same side or not. If they lie to the different side of a line then that line must be jumped to come closer. As in above diagram start point and the destination point are on the same side of x + y – 2 = 0 line, so that line need not to be jumped, rest two lines need to be jumped because both points lies on opposite side.
Finally, we will check the sign of evaluation of points with respect to each line and we will increase our jump count whenever we found opposite signs. Total time complexity of this problem will be linear.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Lexicographically Kth smallest way to reach given coordinate from origin
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
- Check if it is possible to reach vector B by rotating vector A and adding vector C to it
- Minimum Perimeter of n blocks
- Puzzle | Minimum distance for Lizard
- Minimum squares to evenly cut a rectangle
- Minimum squares to cover a rectangle
- Minimum lines to cover all points
- Minimum area of a Polygon with three points given
- Paper Cut into Minimum Number of Squares
- Maximum and Minimum value of a quadratic function
- Minimum Cost Polygon Triangulation
- Minimum height of a triangle with given base and area