A courtyard hangs an n x n grid of paper lanterns for a lantern festival, where each lantern is either lit (1) or unlit (0). Every night the caretakers perform a "mirror ritual" on the grid, one row at a time: for each row, they first physically swap the lanterns left-to-right (reversing the order of that row), and then they flip the lit state of every lantern in that reversed row (a lit lantern becomes unlit, and an unlit lantern becomes lit). Given the grid's state before the ritual, determine the grid's state immediately after the ritual has been applied to every row.
n, the size of the square grid.n lines each contain n space-separated integers, each 0 or 1, giving one row of the grid before the ritual (top row first).Print n lines, each containing n space-separated integers (0 or 1), giving the grid after the ritual has been applied to every row.
1 <= n <= 200 or 1.Example 1
Input
3 1 1 0 1 0 1 0 0 0
Expected
1 0 0 0 1 0 1 1 1
Explanation
Row [1,1,0] reversed is [0,1,1]; inverting each entry gives [1,0,0]. Row [1,0,1] reversed is [1,0,1] (a palindrome); inverting gives [0,1,0]. Row [0,0,0] reversed is [0,0,0]; inverting gives [1,1,1]. So the ritual turns the grid into the three rows 1 0 0 / 0 1 0 / 1 1 1.
Example 2
Input
1 0
Expected
1
Explanation
The single-lantern row [0] reversed is still [0]; inverting its one entry flips 0 to 1, giving a single output row containing 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →