We need to parse the problem. There's a robot that starts at (10,10). It receives a sequence of commands: direction (N,E,S,W) and a positive integer distance lj. The robot moves from its current position in that direction by that distance (presumably in a straight line). The robot collects all gems that lie on its movement path (including the destination). The path is a line segment from start point to end point. The robot collects gems that are on this segment, including endpoints. So essentially, as the robot moves along each segment, any gem that lies exactly on that segment (including start and end points) is collected. The question: after executing all commands, has the robot collected all N gems? That is, determine if every gem is on at least one of the movement segments (including possibly start point? The start point is (10,10), but the problem says "all gems are guaranteed to be on grid points other than the center." So (10,10) does not have a gem. So we only care about gems on the path of any segment. Also note that the robot moves sequentially; after each command, the robot's position changes to the new destination, and the next command starts from there. The path consists of all points visited during movement, i.e., the union of these line segments. Since the robot collects gems when it moves over them, if a gem lies on any segment, it's collected. Overlap? Could be collected multiple times but that's fine.
We need to output "Yes" if every gem lies on at least one segment, else "No".
Constraints: N up to 20, M up to 30. Coordinates are integer grid points from 0 to 20 inclusive. Robot moves within the square 0<=x,y<=20 (including boundaries). The commands guarantee not to exceed the range; i.e., after each move, robot remains inside.
We need to handle multiple datasets until N=0.
The sample input: The given includes some non-English? Actually the sample input is messy: It shows
Sample Input
2
10 11
11 12
2
N 2
E 1
2
10 11
11 12
2
N 2
W 1
3
0 15
5 10
5 15
Five
W 10
S 10
N 20
E 10
S 10
0
But "Five" probably is a typo; likely it's "5". So the third dataset: N=3, then three gem coordinates, then M=5, then five commands. So we'll parse accordingly.
We need to be careful about reading: the direction is a letter and an integer; they are on same line separated by space.
Now the main challenge: determine if a point (gem) lies on the line segment between two points (start and end of a command). Since all coordinates are integer grid points, we can check using geometry. For each gem, we need to see if there exists a command segment that contains it.
Simplify: Simulate robot position: start at (10,10). For each command with direction d and distance l, compute new position (x2,y2) based on d. For example, N: (x, y+l), S: (x, y-l), E: (x+l, y), W: (x-l, y). The segment from (x1,y1) to (x2,y2). Then for each gem not yet collected, test if it lies on that segment. Collect if yes. After processing all commands, check if all gems collected.
Edge cases: The robot might collect a gem on the starting point? Starting point is (10,10) and there is no gem guaranteed. However, a gem could lie exactly on a segment's endpoint (including the start of a later command after moving). For instance, if a gem is at (10,10) (not allowed), but other points can be at the start of a command if it was the end of previous command. That's fine because when the robot starts the command from that point, does it collect the gem? The problem says: "When the robot finds jewels on the movement path (including the destination), it collects all of them." Does the starting point count as part of the movement path? Usually the movement path includes the starting point? It says "including the destination", but not explicitly the start. However, it says "when the robot finds jewels on the movement path (including the destination)". If the gem is exactly at the starting point before moving, does it collect it? The robot is already at that point, but it didn't "move" over it. However, the problem might intend that if the robot is at a location where a gem is, it would have been collected earlier when it arrived there. But what if a gem lies exactly at the starting point of the very first command? That's (10,10) which is excluded. For other commands, the starting point is the destination of previous command. If that destination had a gem, it would have been collected when the robot arrived there (since it's included in the previous movement's destination). So it's fine. So we don't need to consider the start point separately, as it will have been collected if it was on a previous segment's destination. But just to be safe, we can include both endpoints in the segment check, which is typical: a point lies on a segment if it's between the endpoints inclusive. So we'll test inclusive of both endpoints.
Implementation: For each segment (x1,y1) to (x2,y2), we need to check if gem (xg, yg) lies on the line segment. Conditions:
-
The point must be collinear with the segment: The cross product ( (xg - x1)(y2 - y1) - (yg - y1)(x2 - x1) ) == 0.
-
It must lie within the bounding rectangle: min(x1,x2) <= xg <= max(x1,x2) and similarly for y.
Since all coordinates are integers, we can do integer arithmetic.
Because N and M are small, brute-force checking all gems against all segments is fine.
Potential pitfalls: Floating point errors avoided by using integer cross product.
Also, the robot moves within 0..20 grid, all integers, so segments are axis-aligned? Actually directions are only N, E, S, W, so the robot moves only horizontally or vertically. Wait, the commands are direction letters N/E/S/W only, which means each movement is axis-aligned. So the path consists of axis-aligned segments only (vertical or horizontal). That simplifies detection of whether a point lies on the segment: For a vertical move (N/S), x constant; a point lies on it if xg == x1 and yg between y1 and y2 inclusive. For horizontal move (E/W), y constant; point lies on if yg == y1 and xg between x1 and x2 inclusive. That is simpler. Indeed the problem says "direction of movement" is one of N, E, S, W. So robot moves only in cardinal directions. So we don't need cross product.
Thus, we can just simulate: current position (cx, cy) initially (10,10). For each command: parse direction and distance. Determine new position:
if d == 'N': nx = cx; ny = cy + l;
if d == 'S': nx = cx; ny = cy - l;
if d == 'E': nx = cx + l; ny = cy;
if d == 'W': nx = cx - l; ny = cy;
Now, for each gem not yet collected, check if it lies on the segment. For vertical: if xg == cx and min(cy, ny) <= yg <= max(cy, ny), collect. For horizontal: if yg == cy and min(cx, nx) <= xg <= max(cx, nx), collect.
After commands, if all collected, output "Yes", else "No".
Edge Cases: Could a gem be collected multiple times? Doesn't matter.
Check the sample:
Dataset1: gems at (10,11) and (11,12). Commands: N 2: from (10,10) to (10,12). This collects any gem with x=10 and y between 10 and 12 inclusive: (10,11) is collected. Then E 1: from (10,12) to (11,12). This collects any gem with y=12 and x between 10 and 11 inclusive: (11,12) is collected. So all collected => Yes.
Dataset2: same gems. Commands: N 2 (10,10)->(10,12) collects (10,11). Then W 1: from (10,12) to (9,12). This collects gems with y=12 and x between 9 and 10. (11,12) not on that segment. So (11,12) not collected => No.
Dataset3: gems: (0,15), (5,10), (5,15). Commands: W 10: from (10,10) to (0,10). This is horizontal, y=10, x from 10 down to 0. Collects any gem with y=10 and x between 0 and 10: (5,10) is collected. S 10: from (0,10) to (0,0) vertical? Wait S 10: from (0,10) move south 10 => (0,0). Collects any gem with x=0 and y between 0 and 10: (0,15) is not because y 15 >10. (5,15) not. N 20: from (0,0) to (0,20). Collects any gem with x=0 and y between 0 and 20: (0,15) is collected. E 10: from (0,20) to (10,20). Collects any gem with y=20 and x between 0 and10: none (gem at (5,15) not). S 10: from (10,20) to (10,10). Collects any gem with x=10 and y between 10 and20: none. So (5,15) remains uncollected => No. So matches.
Thus solution is straightforward.
But wait: The input sample has "Five" which likely is a typo; we should treat it as "5". So the input reading must handle that? But actual online judges will give correct numeric. So just read integers normally.
Now we need to process until N=0. The input may have extra whitespace. We'll read N, if N==0 break, else read N lines of gems, then read M, then M lines of commands.
Potential pitfalls: Input might contain blank lines. But we can use standard reading.
Implementation details: For each dataset, maintain a boolean array collected of size N (or set). Or just a set of coordinates that are uncollected; but easier: store list of (x,y) and a flag. Since N small.
We simulate commands; for each command, iterate over all uncollected gems; if condition satisfied, set collected.
After all commands, check if all flags true.
Complexities trivial.
Now we need to be careful: The robot collects jewels on the movement path including the destination. So for each segment, we include the endpoints. Our condition includes the endpoints because we use <=. That's correct.
Also, note: The robot's path includes the starting point of the first command? The robot starts at (10,10) before any movement. It might collect a gem if it's at that point? But that's excluded. However, if a gem is at (10,10), it's not allowed, but could a gem be at the starting point of later commands? Those points are destinations of previous moves, so they would have been collected when the robot arrived there. So fine.
Edge case: A gem could be exactly at the starting point of a command if it was not collected earlier? But then that means that previous segment ended at that point and didn't collect it, which would only happen if the gem was not on that segment. However, if a gem is exactly at the starting point of a command, it means that the robot's position is at that gem. Could that happen without having collected it? Only if the robot started there or it moved onto it but the gem was not on the segment? Wait, consider a gem at (10,12). The robot moves from (10,10) to (10,12) (N 2). The destination (10,12) is included, so it is collected. So any gem that lies exactly at the destination of a move will be collected. So if a gem is at the starting point of a command, it must have been collected earlier when it became the destination. The only way it could be not collected is if the robot started there (center) but no gem there. So it's safe.
Thus algorithm is correct.
Now implement.
But wait: There is subtlety: The problem says "When the robot finds jewels on the movement path (including the destination), it collects all of them." Could it be that the robot collects gems only when moving, not when stationary? But the destination is included, so the gem at the destination is collected when the robot arrives. So it's fine.
Now, is there any scenario where the robot might pass over a gem but not collect it because it's not exactly on the path? Since grid points are integer, and robot moves along axis-aligned lines, the path is a continuous line. The gems are at integer grid points. If the robot moves from (10,10) to (10,12), the path includes all points with x=10, y from 10 to 12. The gem at (10,11) is on the path. So collected.
Thus solution is simple.
Now we need to parse the input correctly. In the problem statement, after the gem coordinates, there is a line with M, then M lines with direction and integer. But the sample shows "Five" which is wrong. So we just treat as integer.
Potential pitfalls: The input may have extra spaces or line breaks. Use Python's input reading.
We'll write a Python script: read from sys.stdin. Use a loop.
Pseudo:
import sys
def main():
data = sys.stdin.read().strip().split()
# But need to parse tokens because lines may be messy? Could read line by line.
# Simpler: use iter over lines.
But careful: Using split might mix integers and letters like 'N', 'E' etc. It's okay because we can parse tokens. However, we need to differentiate between direction letters and integers. Since direction is a single uppercase letter, we can treat it as token. So we can read all tokens and parse sequentially.
But the input ends with N=0. So we can parse tokens sequentially:
Let tokens = list of strings from sys.stdin.read().split().
Then index = 0.
While True:
N = int(tokens[idx]); idx+=1
if N == 0: break
gems = []
for i in range(N):
x = int(tokens[idx]); y = int(tokens[idx+1]); idx+=2
gems.append((x,y))
M = int(tokens[idx]); idx+=1
commands = []
for j in range(M):
d = tokens[idx] # direction letter
l = int(tokens[idx+1]); idx+=2
commands.append((d, l))
# process dataset
...
But note: The input may have newlines and spaces; tokens approach works fine.
But be careful: The sample input includes "Five" which is not a number. In a real judge, it would be "5". So we assume valid.
Alternatively, we can read line by line using sys.stdin, and handle possible blank lines.
I'll use token parsing, it's robust.
Now implement processing:
collected = [False]*N
cx, cy = 10, 10
for (d, l) in commands:
if d == 'N':
nx, ny = cx, cy + l
# vertical up
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gx == cx and (cy <= gy <= ny or ny <= gy <= cy):
collected[i] = True
elif d == 'S':
nx, ny = cx, cy - l
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gx == cx and (ny <= gy <= cy or cy <= gy <= ny):
collected[i] = True
elif d == 'E':
nx, ny = cx + l, cy
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gy == cy and (cx <= gx <= nx or nx <= gx <= cx):
collected[i] = True
elif d == 'W':
nx, ny = cx - l, cy
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gy == cy and (nx <= gx <= cx or cx <= gx <= nx):
collected[i] = True
cx, cy = nx, ny
After loop, if all(collected): print("Yes") else: print("No").
But we can simplify condition: For vertical, check gx == cx and min(cy,ny) <= gy <= max(cy,ny). For horizontal, check gy == cy and min(cx,nx) <= gx <= max(cx,nx).
So implement:
if d in ('N','S'):
if gx == cx and min(cy,ny) <= gy <= max(cy,ny): collect
else: # horizontal
if gy == cy and min(cx,nx) <= gx <= max(cx,nx): collect
We need to compute nx, ny first.
So code:
if d == 'N':
nx, ny = cx, cy + l
elif d == 'S':
nx, ny = cx, cy - l
elif d == 'E':
nx, ny = cx + l, cy
elif d == 'W':
nx, ny = cx - l, cy
Then for each uncollected gem:
if d in ('N','S'):
if gx == cx and (min(cy,ny) <= gy <= max(cy,ny)):
collected[i] = True
else:
if gy == cy and (min(cx,nx) <= gx <= max(cx,nx)):
collected[i] = True
That's clean.
Now we also could consider if the robot might go out of bounds? But guaranteed not.
Edge Cases: What if l = 0? The problem says positive integer, so >=1.
Also, there might be multiple commands; robot could go over same gem multiple times; but collected flag prevents double-checking.
Now, ensure that we convert coordinates to int.
Now output for each dataset.
Now test with sample.
But wait, the sample input in the problem statement appears to have:
3
0 15
5 10
5 15
Five
W 10
S 10
N 20
E 10
S 10
0
The token "Five" would break integer parsing. In real input, it's "5". So we ignore.
Now let's test with sample:
Dataset1: N=2, gems (10,11),(11,12), M=2, commands (N,2),(E,1). Process:
start (10,10)
N: nx=10,ny=12; collect: gem0 (10,11) has gx==10 and gy between 10 and12 -> true; gem1 (11,12) has gx==10? false. So gem0 collected.
E: from (10,12) to (11,12); collect: gem1: gy==12? yes, gx between 10 and11 -> true. So all collected.
Dataset2: N=2, same gems, M=2, (N,2),(W,1):
N: collect (10,11) again.
W: from (10,12) to (9,12); collect: gem1: gy==12? yes, gx between 9 and10? gem1 gx=11 -> no. So not collected. => No.
Dataset3: as earlier.
Matches.
Thus solution is correct.
Now we need to output for each dataset.
Complexities trivial.
Now check constraints: N up to 20, M up to 30, easy.
Now think about potential corner cases:
-
A gem might be exactly at the start point of a command but not collected earlier because that command's start point may have been reached without collecting if it was not on any previous segment. But can that happen? Consider gem at (10,12). The robot moves from (10,10) to (10,13) (N 3). The path includes (10,12) so collected. So start of next command is (10,13). If there is a gem at (10,13), it would be collected as destination of the first move. So it's covered. What if the robot moves from (10,10) to (10,13) but there is a gem at (10,13) but the distance is 3; yes destination included. So it's collected.
-
Could a gem lie at (10,10)? Problem says all gems are on grid points other than the center. So not.
-
Could a gem lie on a corner of the square? It's allowed.
-
Could the robot move zero distance? No.
-
Could the robot's path be such that a gem lies exactly on the line but at a turning point? The turning point is the end of one segment and start of next. The gem would be collected during the segment that ends there. So fine.
Thus algorithm stands.
Now about reading input: There might be blank lines. Using tokens split is safe.
One nuance: In the problem statement, after the gems, there is a line with M, then M lines with direction and integer. But the direction letter and integer are on the same line separated by space. So tokens approach works: we read M as integer, then for each command we read a token (direction letter) and a token (integer). So the token list will contain e.g., 'N', '2', etc. Good.
Now implement.
But we need to consider that input might have comments or extraneous? No.
Thus final code:
import sys
def solve():
data = sys.stdin.read().split()
it = iter(data)
out_lines = []
while True:
try:
N = int(next(it))
except StopIteration:
break
if N == 0:
break
gems = []
for _ in range(N):
x = int(next(it))
y = int(next(it))
gems.append((x,y))
M = int(next(it))
commands = []
for _ in range(M):
d = next(it)
l = int(next(it))
commands.append((d,l))
# simulate
collected = [False]*N
cx, cy = 10, 10
for d, l in commands:
if d == 'N':
nx, ny = cx, cy + l
elif d == 'S':
nx, ny = cx, cy - l
elif d == 'E':
nx, ny = cx + l, cy
elif d == 'W':
nx, ny = cx - l, cy
else:
# should not happen
continue
# check each uncollected gem
if d in ('N','S'):
# vertical move
if cx != nx: # Actually vertical should have cx == nx; but just in case
# but it's axis-aligned
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gx == cx and min(cy, ny) <= gy <= max(cy, ny):
collected[i] = True
else:
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gx == cx and min(cy, ny) <= gy <= max(cy, ny):
collected[i] = True
else: # horizontal
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gy == cy and min(cx, nx) <= gx <= max(cx, nx):
collected[i] = True
cx, cy = nx, ny
if all(collected):
out_lines.append("Yes")
else:
out_lines.append("No")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()
But we can simplify the loop: we already have condition that for vertical, cx == nx, for horizontal, cy == ny, but we can just use generic conditions as above.
Now check: For vertical, we used condition gx == cx and between y. That's correct even if l is 0? But l positive so ny differs.
We could also incorporate the start point? But already includes endpoints due to min/max inclusive.
Now, there is a subtle point: The robot collects jewels on the movement path including the destination. The starting point of the move is not necessarily included? Actually, if a gem is at the starting point, it would have been collected earlier when that point was the destination of a previous move. But what about the very first move? The starting point is (10,10) and there is no gem. So it's fine. But in our condition, for vertical we check between cy and ny inclusive. That includes the start point cy. So if a gem were at (cx,cy) it would be collected now. But we know there is none. So it's okay. However, consider a scenario where a gem lies at the start point of a command but hasn't been collected because it was not on previous segment (maybe the robot moved onto it via a different path? Hmm). For instance, gem at (0,10). Robot moves from (10,10) to (0,10) horizontally, that collects gem at destination (0,10). So collected. If gem at (0,10) was not collected because robot moved from (0,0) to (0,20) vertical? That would not pass through (0,10) because vertical includes (0,10) yes. So it would be collected. So it's fine. Our condition includes the start point, so even if somehow it wasn't collected earlier, we would collect it now. But it's impossible that it wasn't collected earlier if the robot is at that point: because to be at that point, the robot must have moved to that point as a destination in a previous move, which would have collected any gem at that destination. Unless the robot started there (10,10) no gem. So including start point is safe and maybe redundant but not harmful.
Now, what about gems that lie exactly on the path of a command but not at endpoints? Our condition includes them.
Thus final solution is good.
Now, test with a custom case: N=1 gem (10,12), M=1 command N 2 -> from (10,10) to (10,12) includes gem: yes. Output Yes.
Edge: gem (10,10) not allowed.
Edge: gem (10,20) M=1 command N 10 -> from (10,10) to (10,20) includes gem: yes.
Edge: gem (0,10) M=1 command W 10 -> from (10,10) to (0,10) includes gem: yes.
Edge: gem (5,10) M=1 command W 5 -> from (10,10) to (5,10) includes gem: yes.
Edge: gem (5,10) M=1 command W 4 -> from (10,10) to (6,10) does not include (5,10): no.
Now, ensure that we handle multiple datasets correctly.
Now, there is also possibility that M=0? But M is at least 1 per constraints (1 <= M <= 30). So at least one command.
Now, also note that the problem description says "The input ends when N = 0, which is not included in the dataset." So we break.
Now, implement.
But wait, the input given in the problem statement includes the line "Five" which is not a number; but we ignore.
Now, one more nuance: The robot moves only within the square. If a gem lies exactly on the boundary, it's okay.
Now, consider a case where the robot moves in a sequence that goes over the same point multiple times; but our algorithm will still mark collected the first time it's encountered.
Thus final answer.
Now, let's produce final solution.
But I need to write answer in the response: the program description and then code.
But the user said: "You are a space adventurer... Your job is to write a program..." So they want code.
Thus I'll provide a Python solution with explanation? The instruction: "Write a program that determines if the robot was able to retrieve all the gems..." So output code.
But in the chat, as ChatGPT, I need to provide a final answer, which likely includes the code. Possibly they expect a solution explanation? Usually in these tasks, we output just code. But the user said "You are a space adventurer... Your job is to write a program..." So I'll provide the program in Python.
Given the typical format on AI, I can provide the code.
But the system might require only code block. I'll include a brief explanation and then code.
Given that this is a problem from some contest, the answer is expected to be the program.
Thus final.
But wait, there might be a nuance: The robot collects gems on the movement path including the destination. But what about the case where the robot moves from A to B and then later from B to C, and a gem is exactly at B but not collected because B was not considered as part of the movement? But as argued, it's collected as destination of first move. So fine.
Now, ensure that we treat inclusive boundaries correctly.
Now, the code as above is good.
But let's test with a case where a gem lies exactly on the path of a vertical move but x matches but y exactly equals start: we included, okay.
Now, the code uses "if d in ('N','S'):" to decide vertical/horizontal. But note that 'N' and 'S' are vertical, 'E' and 'W' are horizontal. Good.
Now, we must be careful: For vertical, we used condition gx == cx. But what if the move is zero distance? Not allowed. So fine.
Now, about performance: NM at most 2030=600, trivial.
Now, consider that the robot may move in a way that crosses many gems, but we only need to check each gem once.
Now, the code.
Now, one potential improvement: we could break early if all collected, but not needed.
Now, think about input parsing: When reading tokens, if there are empty lines, split will ignore them. So fine.
Now, finalize.
But wait: The sample output shows:
Yes
No
No
Exactly.
Now, I'll produce final answer.
But I'll also consider any possible misinterpretation: The problem says "the range in which the robot collects mysterious gems is not so wide. Therefore, the range in which the robot moves can be represented by a two-dimensional plane. The range of movement of the robot is the inside of the square (including the boundary line) with (0,0) and (20,20) as the lower left corner and the upper right corner, respectively. The robot always descends to the center of the range, that is, the coordinates (10,10). Also, all gems are guaranteed to be on grid points other than the center." That indicates the robot cannot go outside this square, but commands guarantee that.
So our simulation stays within square.
Now, final code.
One more nuance: The problem says "When the robot finds jewels on the movement path (including the destination), it collects all of them." That implies that if multiple jewels are on the same path, it collects all. So our loop over gems is correct.
Now, any potential for floating points? No.
Thus solution.
Now, I will output the code.
But before final, let's do a quick sanity check with more edge cases.
Case: N=1 gem (10,9), M=1 command S 1 -> from (10,10) to (10,9). That includes gem: yes.
Case: N=1 gem (10,9), M=1 command S 0? not allowed.
Case: N=2 gems (10,11) and (10,12), M=1 command N 2 -> both on path: yes.
Case: N=2 gems (10,11) and (10,12), M=2 commands: N 1, N 1. First N1: (10,10)->(10,11) collects (10,11). Second N1: (10,11)->(10,12) collects (10,12). Both collected. Also (10,12) might be start of second command, but we include start? Our condition includes the start because min(cy,ny) <= gy <= max(cy,ny) and cy=11, ny=12, start=11, gem at 11 already collected but if not, would be collected as start? Our condition includes start: gem at (10,11) would be included again? But we check only uncollected gems, so after first command, it's collected; second command: gem at (10,11) already collected, gem at (10,12) uncollected, and condition: gx==10 and min(11,12)=11 <=12<=12 -> true, so collected. So fine.
Now, what about if a gem lies exactly on the path but not at integer coordinates? Gems are integer grid points, so fine.
Now, everything's good.
Now, writing the final answer: Python code.
I'll provide the code with a main function and reading from stdin.
One more nuance: There is a possibility that the input has trailing spaces after the last dataset. Our loop will break on N=0. But if there is any extra token after that, we break anyway because we break when N==0, before reading more. So safe.
Thus final.
But wait, the input format shows a line "Five" but that is a mistake. In real input, M is an integer. So our code expects integer.
Now, final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
it = iter(data)
out = []
while True:
try:
N = int(next(it))
except StopIteration:
break
if N == 0:
break
gems = []
for _ in range(N):
x = int(next(it))
y = int(next(it))
gems.append((x, y))
M = int(next(it))
commands = []
for _ in range(M):
d = next(it) # direction letter
l = int(next(it)) # distance
commands.append((d, l))
# simulation
collected = [False] * N
cx, cy = 10, 10 # start position
for d, l in commands:
if d == 'N':
nx, ny = cx, cy + l
elif d == 'S':
nx, ny = cx, cy - l
elif d == 'E':
nx, ny = cx + l, cy
elif d == 'W':
nx, ny = cx - l, cy
else:
# should not happen, skip
continue
# check each not yet collected gem
if d in ('N', 'S'): # vertical movement
y_min, y_max = min(cy, ny), max(cy, ny)
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gx == cx and y_min <= gy <= y_max:
collected[i] = True
else: # horizontal movement
x_min, x_max = min(cx, nx), max(cx, nx)
for i, (gx, gy) in enumerate(gems):
if not collected[i] and gy == cy and x_min <= gx <= x_max:
collected[i] = True
cx, cy = nx, ny
out.append("Yes" if all(collected) else "No")
sys.stdout.write("\n".join(out))
if name == "main":
solve()