Hide

Problem I
Sheeesh

Johnny is playing the new VR game Lots of Walls1 with his friend Paige. He wants to visit her in the game and tell her just how fantastic and wall-filled the game is. Unfortunately, it seems to be bugged because neither he nor Paige can currently move. Fortunately, he has another way of telling her just how great he thinks the game is. He decides to send a sheeesh in her direction to let her know he thinks this game is impressive. The game does not yet have a great $3D$ sound system yet, so sound works very simply. It travels linearly and follows the law of reflection, such that the angle of incidence is equal to the angle of reflection. To simplify the game engine, sound only bounces off of a wall a maximum of one time. Using the debug screen, Johnny can see both his and Paige’s positions as well as the positions of all of the walls in the game. Is there a wall such that Johnny can yell sheeesh where Paige can hear?

Input

The input is as follows. The first line contains integers $x_1$, $y_1$, $x_2$, $y_2$ which are the coordinates of Johnny and Paige respectively. The next line $n$ specifies the number of walls in the game. Each of the following walls is specified by $4$ integer points, $px_1$, $py_1$, $px_2$, $py_2$, which define the line segment representing a wall. It is guaranteed that Johnny and Paige will never intersect with a wall. Walls may intersect with each other. The input is constrained as follows:
$1 \leq n \leq 10\, 000$
$-100\, 000 \leq x_1, y_1, x_2, y_2 \leq 100\, 000$
$-200\, 000 \leq px_1, py_1, px_2, py_2 \leq 200\, 000$

Output

Output $0$ to stdout if there is a direct path for Johnny’s sheeesh to reach Paige. If there is a wall segment that the sheeesh may be bounced off of to successfully reach Paige, output the index ($1$-indexed) of the wall segment that can be used. If there are multiple walls that may be used, output the lowest possible index. If there is no path for the sheeesh within $1$ bounce, output $-1$.

The figures below provide a visualization of the sample problems:

\includegraphics[width=1\textwidth ]{sample_problems.png}
Sample Input 1 Sample Output 1
2 2 6 1
2
0 0 10 0
3 1 3 4
-1
Sample Input 2 Sample Output 2
2 2 6 1
1
0 0 10 0
0
Sample Input 3 Sample Output 3
2 2 6 2
2
0 0 10 0
4 1 4 4
1

Footnotes

  1. Lots of Walls not © anyone, we just made this one up.

Please log in to submit a solution to this problem

Log in