Tiling the Shower Floor (Small)

Time limit2sMemory limit512 MB

Summary
Cover a 2^K by 2^K grid with L-shaped trominoes leaving one drain cell open, choosing the lexicographically smallest numbering.
Level

Medium7 of 10

Topics
Divide and conquer, Recursion, Implementation
Solved
No attempts yet

Problem

On his first day at the training camp, Mingyu is called in and told to lay the shower floor again. The person who laid it before covered the drain along with every other cell.

The shower floor is a square whose side length is a power of two. That worker used only 2×22 \times 2 square tiles, and such tiles cannot leave a single cell open for the drain. Mingyu decides to use L shaped tiles that cover three cells instead of the four cell square tiles. An L shaped tile is a 2×22 \times 2 square with one cell removed, and it may be rotated by 90 degrees into any of the four orientations.

You are given the size of the floor and the position of the drain. Cover every cell except the drain with L shaped tiles so that no two tiles overlap and no cell is left uncovered. A tile may not stick out of the floor.

Input

The first line contains a natural number KK (1≤K≤21 \le K \le 2). The side length of the floor is 2K2^K.

The second line contains two natural numbers xx and yy, separated by a space, giving the position of the drain (1≤x,y≤2K1 \le x, y \le 2^K). The bottom left cell is (1,1)(1, 1) and the top right cell is (2K,2K)(2^K, 2^K).

Output

Print 2K2^K lines. Line ii holds the row with y=2K+1−iy = 2^K + 1 - i, from x=1x = 1 to x=2Kx = 2^K, with one space between values, so the top row is printed first. Print -1 for the drain cell, and for every other cell print the number of the tile that covers it.

Several layouts can be valid, so one answer is fixed by the following rule. Scan the cells in printing order, top line first and left to right inside a line, and give each tile you meet for the first time the next unused number starting from 11. There are (4K−1)/3(4^K - 1) / 3 tiles, so the numbers run from 11 to (4K−1)/3(4^K - 1) / 3. Among all valid layouts numbered this way, print the one whose printed numbers, read in printing order, form the lexicographically smallest sequence.

If no valid covering exists, print -1 on a single line.

Examples3

  1. Example 1

    Input
    1
    2 2
    
    Expected output
    1 -1
    1 1
    
  2. Example 2

    Input
    2
    1 1
    
    Expected output
    1 1 2 2
    1 3 3 2
    4 4 3 5
    -1 4 5 5
    
  3. Example 3

    Input
    2
    3 2
    
    Expected output
    1 1 2 2
    1 3 3 2
    4 3 -1 5
    4 4 5 5