#K86117. Turtle Visits Targets
Turtle Visits Targets
Turtle Visits Targets
You are given n target points on a grid and an integer k representing the length of a command sequence. A turtle starts at the first target point and follows a command sequence consisting of the four characters 'N', 'E', 'S', and 'W', which represent moves of one unit north, east, south, and west respectively. After executing the command sequence once, the turtle continues to repeatedly apply the same sequence, effectively moving by a net displacement each cycle. For each target point provided, the turtle will simulate moves by repeatedly adding the net displacement vector to that target. If the union of all such positions (without duplicates) is exactly n in number, then the starting point is considered valid and should be output. Otherwise, output -1
.
Note: The net displacement of one command cycle is calculated from the starting point of that cycle. With each target point, you simulate moves until a previously visited position is encountered. If at any point, the number of unique visited positions exceeds n, the simulation is halted and the answer should be -1
.
It is guaranteed that the command sequence is non-empty.
inputFormat
The input is given from standard input (stdin
) in the following format:
n k x1 y1 x2 y2 ... xn yn command_sequence
Where:
n
is the number of target points.k
is the length of the command sequence.- Each of the next
n
lines contains two integersxi
andyi
representing the coordinates of the target points. command_sequence
is a string composed of the characters 'N', 'E', 'S', 'W'.
outputFormat
If it is possible for the turtle to visit exactly all n
target points, output the starting point as two space-separated integers. Otherwise, output -1
. The result should be printed to standard output (stdout
).
3 4
1 1
2 2
3 3
NESW
1 1
</p>