Fun Pipe Puzzle
InterviewTime limit1sMemory limit2048 MB
Given a 2-row grid of I and L pipes, decide whether rotating them can connect a path from cell (1, 1) to cell (2, N).
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy, Implementation
- Solved
- No attempts yet
Problem
Jinheung is solving a fun pipe puzzle. The puzzle is 2 cells tall and cells wide. The cell that is -th from the top and -th from the left is written as .
Every cell except and has one pipe installed. There are two kinds of pipes: the I-shaped pipe and the L-shaped pipe. Jinheung wants to rotate these pipes in place as needed to connect a pipe path from to .

Figure 1. An I-shaped pipe can be rotated to form the two shapes above.

Figure 2. An L-shaped pipe can be rotated to form the four shapes above.
Given the type of pipe installed in each cell, determine whether Jinheung can connect a pipe path from to .

Figure 3. One way to connect a pipe path from to .
Input
The first line contains the width of the puzzle.
The second line contains a string of length that gives the pipe types in the top row.
The third line contains a string of length that gives the pipe types in the bottom row.
In each string, X is given at and . An I-shaped pipe is given as I, and an L-shaped pipe is given as L.
Output
If Jinheung can connect a pipe path from to by rotating the pipes appropriately, print YES. Otherwise, print NO.