Toy Train Tracks
Time limit1sMemory limit1024 MB
Given s straight and c curved track pieces, build the longest closed loop on a grid that does not cross itself and print its piece sequence.
- Level
Medium6 of 10
- Topics
- Math, Geometry, Implementation
- Solved
- No attempts yet
Problem
Every little child, and quite a few adults, are fascinated by toy trains. From a toddler's choo-choo train to a hobbyist's elaborate model railroad filling an entire basement, they are a profitable business. The Toy Train Tracks Construction Company (TTTCC) manufactures train tracks for all ages and skill levels.
To keep existing customers busy and perhaps attract new ones, TTTCC has recently started publishing maps that show how to connect its train tracks into elaborate layouts. Usually a designer comes up with an interesting track layout, then publishes both the layout and the number of each track segment type (say, curves and straight parts) needed to build it. But TTTCC has learned that many customers want the reverse: they already have track segments lying around (maybe found in grandma's attic) and would like to use them to build a large train course. How difficult might that be?
To study the feasibility of automating the layout-creation process, TTTCC is interested in constructing train courses from two shapes: straight line segments and 90-degree turns (see Figure 1).

Valid layouts are created by placing these shapes on a square grid, with each track piece taking up exactly one grid cell. Both types of pieces can be rotated in 90-degree increments. A "proper" train track must be connected and form a single closed loop. Given a set of straight and curved track segments, what is the longest closed loop one can construct?

Figure 2: A sample track using four straight track segments and twelve curves. This corresponds to Sample Output 1.
Input
The input is a single line containing two integers and , the number of straight and curved track segments available, respectively (, ).
Output
Output a train loop that uses at most straight segments and curved segments and has the longest possible length (in number of track segments). The loop must be closed and cannot intersect itself. If several loops have the maximal length, any one of them is accepted.
If the loop has length , print a single string of length , where each character represents a segment in the order encountered during one traversal of the loop. S stands for a straight segment, L for a curved segment that turns left, and R for a curved segment that turns right.