Once Around the Lock
Time limit1sMemory limit128 MB
Given a circular dial and a code, decide whether a sequence of clockwise and counterclockwise turns unlocks the lock.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
A circular combination lock has a dial with the numbers through printed on it in clockwise order. Turning the dial brings one of the numbers to the top. If is at the top, turning the dial counterclockwise by brings to the top, and turning it clockwise by brings to the top.
Each lock has a three-number code and can be opened only by performing the following steps in order:
- Spin the dial clockwise through at least one full rotation, ending with at the top, with no counterclockwise turn in between (this may take several consecutive clockwise turns).
- Turn the dial counterclockwise until reaches the top for the second time, with no clockwise turn in between (this may take several consecutive counterclockwise turns).
- Turn the dial clockwise until reaches the top, without going more than one full rotation and with no counterclockwise turn in between (this may take several consecutive clockwise turns).
Any turn made after step 3 closes the lock again.
You are given a lock and a sequence of turns. Before the first turn the lock is closed and the dial has been set with at the top. Determine whether the lock is open after the last turn.
Input
The input contains several test cases.
The first line of each test case has four integers : the number of digits on the dial and the three-number code. The values , , and are all different, and .
The following line or lines list the turns, each written as d s, where d is C for clockwise or CC for counterclockwise, and is how many numbers pass the top during that turn. For example, with and currently at the top, CC 6 brings to the top while C 6 brings to the top. The list of turns may span several lines and ends with a ?.
A line containing a single 0 follows the last test case.
Output
For each test case, print one line: Case k: Open if the lock is open after the last turn, or Case k: Closed otherwise, where is the test case number starting from .