#!/usr/bin/env python

from sys import argv
from random import choice

try:
    n = int(argv[1])
except:
    n = 3

try:
    posx, posy = int(argv[2]),int(argv[3])
except:
    (posx,posy) = (1,1)  # x = Zeile, y = Spalte

try:
    PRINT_AXES = argv[4] != "noaxes"
except:
    PRINT_AXES = True

try:
    PRINT_HTML = argv[4] == "html"
except:
    PRINT_HTML = False

side = 2**n

DEBUG = False
# PRINT_AXES = False


zeichen = '@'

if DEBUG: print(side, (posx,posy))

matrix = [["-" for x in range(side)] for y in range(side)]
matrix[posx][posy] = '!'  # x = Zeile, y = Spalte


def nextzeichen (z):
    z2 = chr(ord(z)+1)
    if z2 > "Z": z2 = "A"
    return z2


def splitquadrat (m):
    laenge = len(m); halb = laenge//2
    oben = range(halb); unten = range(halb,laenge)
    links,rechts = oben,unten
    a = [[m[x][y] for y in links]  for x in oben ]  # links oben
    b = [[m[x][y] for y in rechts] for x in oben ]  # rechts oben
    c = [[m[x][y] for y in links]  for x in unten]  # links unten
    d = [[m[x][y] for y in rechts] for x in unten]  # rechts unten
    return a,b,c,d

def joinquadrat (a,b,c,d):
    laenge = len(a)
    return [a[i]+b[i] for i in range(laenge)] \
        + [c[i]+d[i] for i in range(laenge)]

def output_html(m):
    color_dict = {}
    for i in range(26):
        color_dict[chr(65+i)] = ''.join([choice('0123456789ABCDEF') for j in range(6)])
    color_dict["!"] = "#000000"

    print ("""<html>
<body>
<table>""")
    for line in m: 
        print ("<tr>")
        for col in line: 
            farbe = color_dict[col]
            if col == "!":
                fontcolor="; color:red"
            else:
                fontcolor=""
            print (('<td width="20pt" height="20pt" align="center" '+
                'style="background-color:#%s; font-family:Courier%s">'+
                '%s</td>') % (farbe, fontcolor, col))
        print ("</tr>")
    print ("""</table>
</body>
</html>""")

def output(m):
    if PRINT_AXES:
        print ("  | ", end="")
        for i in range(len(m)): print (i % 10, end=" ")
        print ("")
        print("--+"+len(m)*"--")
        
    i = 0
    for line in m: 
        if PRINT_AXES:
            print (i % 10, end=" | "); i += 1
        for col in line: print (col, end=' ')
        print ("")

def fill(m,x,y):
    global zeichen
    laenge = len(m)
    if laenge == 2:
        zeichen = nextzeichen (zeichen)
        sicherung = m[x][y]
        m = [[zeichen for i in range(laenge)] for j in range(laenge)]
        m[x][y] = sicherung
    else:
        a,b,c,d = splitquadrat (m)
        halb = laenge//2
        # 
        # m = | a b | 
        #     | c d |
        if   x <  halb and y <  halb:  workers = "bcd"   # links oben
        elif x <  halb and y >= halb:  workers = "acd"   # rechts oben
        elif x >= halb and y <  halb:  workers = "abd"   # links unten
        elif x >= halb and y >= halb:  workers = "abc"   # rechts unten

        if   "a" not in workers:  a = fill (a,x,     y)
        elif "b" not in workers:  b = fill (b,x,     y-halb)
        elif "c" not in workers:  c = fill (c,x-halb,y)
        elif "d" not in workers:  d = fill (d,x-halb,y-halb)

        zeichen = nextzeichen (zeichen)

        if "a" in workers:        a[-1][-1] = zeichen    # unten rechts
        if "b" in workers:        b[-1][0 ] = zeichen    # unten links
        if "c" in workers:        c[0 ][-1] = zeichen    # oben rechts
        if "d" in workers:        d[0 ][0 ] = zeichen    # oben links

        if "a" in workers:        a = fill (a,halb-1,halb-1)
        if "b" in workers:        b = fill (b,halb-1,0)
        if "c" in workers:        c = fill (c,0,     halb-1)
        if "d" in workers:        d = fill (d,0,     0)

        m = joinquadrat (a,b,c,d)
    return m

def errors(m):
    e = 0
    for zeile in m:
        for elem in zeile:
            if elem == "-": e=e+1
    return e



if DEBUG: 
    output(matrix); print("")
matrix = fill (matrix,posx,posy)
if PRINT_HTML:
    output_html(matrix)
else:
    output(matrix); 

if DEBUG:
    print("")
    print ("Error count = ", errors(matrix))

#if matrix[posx][posy] != "!":
#    print ("Warning: ! gone")

# xx = [[1,1,2,2],[1,1,2,2],[3,3,4,4],[3,3,4,4]]
# a,b,c,d = splitquadrat(xx)
# output(xx)
# output(a)
# output(b)
# output(c)
# output(d)
# yy = joinquadrat(a,b,c,d)
# output(yy)
