Flipping letters
InterviewTime limit1sMemory limit1024 MB
Given an N by N grid of d, b, q, and p, print the grid after flipping every letter vertically or horizontally.
- Level
Easy1 of 10
- Topics
- Implementation, String
- Solved
- No attempts yet
Statement

The lowercase letters d, b, q, and p have a special property: flipping one vertically or horizontally turns it into one of d, b, q, or p. The rules are as follows.
- Flipping
dvertically givesq, and flipping it horizontally givesb. - Flipping
bvertically givesp, and flipping it horizontally givesd. - Flipping
qvertically givesd, and flipping it horizontally givesp. - Flipping
pvertically givesb, and flipping it horizontally givesq.
Each cell of an by grid contains one of d, b, q, or p. Find the result of flipping every letter in the grid in the given direction.
Input
The first line contains two integers and , separated by a space. means flip vertically, and means flip horizontally.
The next lines each contain a string of length . The -th character of the -th line is the letter in row , column of the grid. Each cell contains one of the lowercase letters d, b, q, or p.
Output
Print the grid after flipping every letter in it in the given direction.