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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimize steps defined by a string required to reach the destination from a given source
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Check if it is possible to reach vector B by rotating vector A and adding vector C to it
- Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
- 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
- Minimum Cost Polygon Triangulation
- Paper Cut into Minimum Number of Squares
- Minimum lines to cover all points
- Minimum revolutions to move center of a circle to a target
- Minimum Perimeter of n blocks
- Find minimum radius such that atleast k point lie inside the circle
- Maximum and Minimum value of a quadratic function
- Minimum height of a triangle with given base and area
- Minimum number of points to be removed to get remaining points on one side of axis
- Find maximum and minimum distance between magnets
- Puzzle | Minimum distance for Lizard
- Minimum number of square tiles required to fill the rectangular floor
- Minimum squares to cover a rectangle
- Minimum length of the shortest path of a triangle
Improved By : Rajput-Ji