NOI Contest Archive

Archive / 2025 / Qualifier Round

robot-path-collision

Medium · NOI 2025 Qualifier Round

A robot is placed on an infinite 2D grid at coordinates (0,0) facing north. You are given N commands of three possible types:

  1. MOVE x — The robot moves forward x steps in the direction it is currently facing.
  2. LEFT — The robot turns left 90 degrees (counterclockwise) without moving from its spot.
  3. RIGHT — The robot turns right 90 degrees (clockwise) without moving from its spot.

Your task is to determine if the robot’s path ever crosses itself. Formally, the path crosses itself if the robot visits any grid cell more than once at any point during the entire sequence of commands.

Important Details and Examples:

Input Format

A single integer \(N\) — the number of commands.

Followed by \(N\) lines, each describing a command:

Output Format

Print YES if the robot’s path ever crosses itself, or NO otherwise.

Constraints

Sample Input 0

4
MOVE 3
LEFT
MOVE 2
RIGHT

Sample Output 0

NO