The Rock Game
InterviewTime limit1sMemory limit128 MB
Print the reflected binary Gray code cycle for N bits as 2^N + 1 lines, each line showing X for covered holes and O for uncovered ones.
- Level
Easy3 of 10
- Topics
- Bit manipulation, Math, Implementation, Simulation
- Solved
- No attempts yet
Problem
Before the cows head home to rest, Farmer John wants them to get some intellectual stimulation by playing a game.
The board has () identical holes in the ground, numbered through from left to right. Every hole starts uncovered. In one move a cow either covers exactly one currently uncovered hole with a rock, or uncovers exactly one currently covered hole.
The state of the game is described by which holes are covered and which are not: a string of length whose -th character is X if hole is covered and O if it is uncovered. There are possible states.
The cows want to begin from the all-uncovered state, visit every one of the states exactly once, and then return to the all-uncovered state. Because each move flips exactly one hole, such a tour changes one character at a time and closes back onto its start.
Reaching every state is not automatic. For example, with a cow can play seven moves and arrive at state XXX, only to find that uncovering any single hole leads to a state it has already visited — the tour gets stuck before it can return to OOO.
Many valid tours exist. To make the answer unique, you must output one specific canonical tour, defined below in the Output section.
Input
The single line contains one integer ().
Output
Print exactly lines. Line (for ) is the state at time of the required canonical tour, the reflected binary Gray-code cycle:
- Let and (the bitwise XOR of with shifted right by one bit).
- Write as an -bit binary number, most significant bit first. Hole is the most significant bit and hole is the least significant bit.
- Print
Xfor each bit equal to (covered) andOfor each bit equal to (uncovered).
Since at both and , the first and last lines are always all O. Consecutive lines always differ in exactly one hole, and the states on lines through are all distinct.