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.

Examples:

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.

`// C++ program to find minimum jumps to reach ` `// a given destination from a given source ` `#include <bits/stdc++.h> ` `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; ` `} ` |

Output:

2

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## leave a comment

## 0 Comments