Problem:
 Score : 900 points    Problem Statement Let us consider a grid of squares with N rows and N columns. You want to put some domino pieces on this grid.
Each domino piece covers two squares that have a common side. Each square can be covered by at most one piece.  For each row of the grid, let's define its quality as the number of domino pieces that cover at least one square in this row.
We define the quality of each column similarly.  Find a way to put at least one domino piece on the grid so that the quality of every row is equal to the quality of every column,
or determine that such a placement doesn't exist.      Constraints  2 \le N \le 1000         Input Input is given from Standard Input in the following format:  N       Output If the required domino placement doesn't exist, print a single integer -1 .  Otherwise, output your placement as N strings of N characters each.
If a square is not covered, the corresponding character must be . (a dot).
Otherwise, it must contain a lowercase English letter.
Squares covered by the same domino piece must contain the same letter.
If two squares have a common side but belong to different pieces, they must contain different letters.        Sample Input 1 6      Sample Output 1 aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b  The quality of every row and every column is 2.       Sample Input 2 2      Sample Output 2 -1

Solution (Python):
import numpy as np


def solve(n):
    if n in [1, 2]:
        return np.array([])
    res = np.array([["."] * n for _ in range(n)])
    block_5 = np.array([
        ["a", "a", "b", "b", "c"],
        ["d", "e", "e", ".", "c"],
        ["d", ".", ".", "f", "g"],
        ["h", ".", ".", "f", "g"],
        ["h", "i", "i", "j", "j"]
    ])
    if n == 5:
        return block_5
    if n == 10:
        res[0:5, 0:5] = block_5
        res[5:10, 5:10] = block_5
        return res
    block_3 = np.array([
        ["a", "a", "."],
        [".", ".", "a"],
        [".", ".", "a"]
    ])
    if n % 3 == 0:
        for i in range(0, n, 3):
            res[i:i + 3, i:i + 3] = block_3
        return res
    res_4 = 0
    res_7 = 0
    res_9 = 0
    if n % 4 == 0:
        res_4 = n // 4
    elif (n - 7) % 4 == 0 and n >= 7:
        res_4 = (n - 7) // 4
        res_7 = 1
    elif (n - 14) % 4 == 0 and n >= 14:
        res_4 = (n - 14) // 4
        res_7 = 2
    elif (n - 21) % 4 == 0 and n >= 21:
        res_4 = (n - 21) // 4
        res_7 = 3
    elif n == 13:
        res_4 = 1
        res_9 = 1
    elif n == 17:
        res_4 = 2
        res_9 = 1
    else:
        print(Error)

    block_4 = np.array([
        ["a", "b", "c", "c"],
        ["a", "b", "d", "d"],
        ["e", "e", "f", "g"],
        ["h", "h", "f", "g"],
    ])

    block_7 = np.array([
        [".", ".", "a", "b", "c", ".", "."],
        [".", ".", "a", "b", "c", ".", "."],
        ["d", "d", "e", "e", ".", "f", "f"],
        ["g", "g", ".", ".", "h", "i", "i"],
        ["j", "j", ".", ".", "h", "k", "k"],
        [".", ".", "l", "m", "n", ".", "."],
        [".", ".", "l", "m", "n", ".", "."]
    ])

    block_9 = np.array([
        ["a", "a", "b", "b", "c", "c", ".", ".", "."],
        ["d", "d", "e", "e", "f", "f", ".", ".", "."],
        ["g", "g", "h", "h", "i", "i", ".", ".", "."],
        [".", ".", ".", ".", ".", ".", "j", "k", "l"],
        [".", ".", ".", ".", ".", ".", "j", "k", "l"],
        [".", ".", ".", ".", ".", ".", "m", "n", "o"],
        [".", ".", ".", ".", ".", ".", "m", "n", "o"],
        [".", ".", ".", ".", ".", ".", "p", "q", "r"],
        [".", ".", ".", ".", ".", ".", "p", "q", "r"]
    ])

    base = 0
    for i in range(res_4):
        res[base:base + 4, base:base + 4] = block_4
        base += 4
    for i in range(res_7):
        res[base:base + 7, base:base + 7] = block_7
        base += 7
    for i in range(res_9):
        res[base:base + 9, base:base + 9] = block_9
        base += 9

    return res


def main():
    n = int(input())
    res = solve(n)
    if res.shape[0] == 0:
        print(-1)
    else:
        for i in range(res.shape[0]):
            print("".join(list(res[i, :])))


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())

if n % 3 == 0:
    m = n // 3
    for i in range(m):
        print('.' * (3 * i) + 'abb' + '.' * (n - 3 - 3 * i))
        print('.' * (3 * i) + 'a.a' + '.' * (n - 3 - 3 * i))
        print('.' * (3 * i) + 'bba' + '.' * (n - 3 - 3 * i))
else:
    if n % 3 == 1:
        if n >= 10:
            print('abaa' + '.' * (n - 4))
            print('abcc' + '.' * (n - 4))
            print('ccba' + '.' * (n - 4))
            print('aaba' + '.' * (n - 4))

            m = (n - 4) // 3
            for i in range(m):
                if i == m - 1:
                    print('.' * 4 + 'dd.' + '.' * (n - 10) + 'abb')
                    print('.' * 4 + '..d' + '.' * (n - 10) + 'a.a')
                    print('.' * 4 + '..d' + '.' * (n - 10) + 'bba')
                else:
                    print('.' * (4 + 3 * i) + 'abbdd.' + '.' * (n - 10 - 3 * i))
                    print('.' * (4 + 3 * i) + 'a.a..d' + '.' * (n - 10 - 3 * i))
                    print('.' * (4 + 3 * i) + 'bba..d' + '.' * (n - 10 - 3 * i))
        elif n == 7:
            print('a.aa..a')
            print('ab....a')
            print('.b..aab')
            print('aabb..b')
            print('.b.aacc')
            print('aba....')
            print('a.a.aa.')
        else: 
            print('abaa')
            print('abcc')
            print('ccba')
            print('aaba')
    elif n % 3 == 2:
        if n >= 14:
            m = (n - 8) // 3

            print('abaa' + '.' * (n - 4))
            print('abcc' + '.' * (n - 4))
            print('ccba' + '.' * (n - 4))
            print('aaba' + '.' * (n - 4))
            print('....abaa' + '.' * (n - 8))
            print('....abcc' + '.' * (n - 8))
            print('....ccba' + '.' * (n - 8))
            print('....aaba' + '.' * (n - 8))

            m = (n - 8) // 3
            for i in range(m):
                if i == m - 1:
                    print('.' * 8 + 'dd.' + '.' * (n - 14) + 'abb')
                    print('.' * 8 + '..d' + '.' * (n - 14) + 'a.a')
                    print('.' * 8 + '..d' + '.' * (n - 14) + 'bba')
                else:
                    print('.' * (8 + 3 * i) + 'abbdd.' + '.' * (n - 14 - 3 * i))
                    print('.' * (8 + 3 * i) + 'a.a..d' + '.' * (n - 14 - 3 * i))
                    print('.' * (8 + 3 * i) + 'bba..d' + '.' * (n - 14 - 3 * i))
        elif n == 11:
            print('a.aa..a' + '....')
            print('ab....a' + '....')
            print('.b..aab' + '....')
            print('aabb..b' + '....')
            print('.b.aacc' + '....')
            print('aba....' + '....')
            print('a.a.aa.' + '....')
            print('.' * 7 + 'abaa')
            print('.' * 7 + 'abcc')
            print('.' * 7 + 'ccba')
            print('.' * 7 + 'aaba')
        elif n == 8:
            print('abaa....')
            print('abcc....')
            print('ccba....')
            print('aaba....')
            print('....abaa')
            print('....abcc')
            print('....ccba')
            print('....aaba')
        elif n == 5:
            print('abbaa')
            print('a..bc')
            print('bb.bc')
            print('a.aab')
            print('abb.b')
        else:
            print(-1)

Solution (Python):
n=int(input())
if n==2:
  print(-1)
  exit()
if n==3:
  print("abb")
  print("a.c")
  print("ddc")
  exit()
if n==5:
  print("aabbc")
  print("hi..c")
  print("hi..d")
  print("g.jjd")
  print("gffee")
  exit()
if n==7:
  print("..abc..")
  print("..abc..")
  print("aax..aa")
  print("bbx..bb")
  print("cc.yycc")
  print("..abc..")
  print("..abc..")
  exit()
a=[n*["."]for _ in range(n)]
if n%2:
  a[-5][-5],a[-5][-4],a[-5][-3],a[-5][-2],a[-5][-1]=list("aabbc")
  a[-4][-5],a[-4][-4],a[-4][-3],a[-4][-2],a[-4][-1]=list("hi..c")
  a[-3][-5],a[-3][-4],a[-3][-3],a[-3][-2],a[-3][-1]=list("hi..d")
  a[-2][-5],a[-2][-4],a[-2][-3],a[-2][-2],a[-2][-1]=list("g.jjd")
  a[-1][-5],a[-1][-4],a[-1][-3],a[-1][-2],a[-1][-1]=list("gffee")
  n-=5
for i in range(0,n,2):
  a[i][i],a[i][i+1]="a","a"
  a[i+1][i],a[i+1][i+1]="b","b"
if n%4==0:
  for i in range(0,n,2):
    a[n-i-2][i],a[n-i-2][i+1]="c","d"
    a[n-i-1][i],a[n-i-1][i+1]="c","d"
else:
  for i in range(0,n-2,2):
    a[i][i+2],a[i][i+3]="c","d"
    a[i+1][i+2],a[i+1][i+3]="c","d"
  a[n-2][0],a[n-2][1]="c","d"
  a[n-1][0],a[n-1][1]="c","d"
for i in a:print("".join(i))

Solution (Python):
n=int(input())
if n%3==0:
    for i in range(n//3):
        print("a.."*(n//3))
        print("a.."*(n//3))
        print(".aa"*(n//3))
elif n%2==0 and n>=4:
    x="aacd"+"."*(n-4)
    y="bbcd"+"."*(n-4)
    for i in range(n//2):
        print(x)
        print(y)
        x=x[2:]+x[:2]
        y=y[2:]+y[:2]
elif n>=13:
    x="aacd"+"."*(n-13)
    y="bbcd"+"."*(n-13)
    for i in range((n-9)//2):
        print(x+"."*9)
        print(y+"."*9)
        x=x[2:]+x[:2]
        y=y[2:]+y[:2]
    for i in range(3):
        print("."*(n-9)+"a..a..a..")
        print("."*(n-9)+"a..a..a..")
        print("."*(n-9)+".aa.aa.aa")   
elif n==5:
    print("aabba")
    print("bc..a")
    print("bc..b")
    print("a.ddb")
    print("abbaa")
elif n==7:
    print("aabbcc.")
    print("dd.dd.a")
    print("..e..ea")
    print("..e..eb")
    print("dd.dd.b")
    print("..e..ec")
    print("..e..ec")
elif n==11:
    print("aabbcc.....")
    print("dd.dd.a....")
    print("..e..ea....")
    print("..e..eb....")
    print("dd.dd.b....")
    print("..e..ec....")
    print("..e..ec....")
    print(".......aacd")
    print(".......bbcd")
    print(".......cdaa")
    print(".......cdbb")
else:
    print(-1)

Solution (Python):
list3=['aab', 'd.b', 'dcc']
list4=['aacd', 'bbcd', 'efgg', 'efhh']
list5=['abc..', 'abc..', 'ddehh', 'g.eii', 'gffjj']
list7=['.aabbcc', 'dghh...', 'dg.j...', 'eiij...', 'e...kll', 'f...k.n', 'f...mmn']

n=int(input())
if n<=2:
    print(-1)
elif n%3==0:
    lists=[]
    for i in range(n):
        row='.'*((i//3)*3)+list3[i%3]+'.'*(n-3-(i//3)*3)
        lists.append(row)
    for l in lists:
        print(l)
else:
    kotae=[
            [],
            [],
            [],
            [],
            [1,0,0],
            
            [0,1,0],
            [],
            [0,0,1],
            [2,0,0],
            [1,1,0],
            
            [0,2,0],
            [1,0,1],
            [3,0,0],
            [2,1,0],
            [1,2,0],
            
            [0,3,0],
            [4,0,0],
            [3,1,0],
            [2,2,0],
            [1,3,0],
            
            [0,4,0],
            [4,1,0],
            [3,2,0],
            [2,3,0],
            [6,0,0],
            
            [0,5,0],
            [4,2,0]
        ]
    if n<=25:
        loop=kotae[n]
    else:
        mazu=n%20
        if mazu<7:
            mazu+=20
        loop=kotae[mazu]
        loop[0]+=((n-mazu)//20)*5
    
    lists=[]
    now=0
    for i in range(loop[0]*4):
        row='.'*(i//4)*4+list4[i%4]+'.'*(n-4-(i//4)*4)
        lists.append(row)
    now+=loop[0]*4
    
    for i in range(loop[1]*5):
        row='.'*(now+(i//5)*5)+list5[i%5]+'.'*(n-5-now-(i//5)*5)
        lists.append(row)
    now+=loop[1]*5
    
    for i in range(loop[2]*7):
        row='.'*(now+(i//7)*7)+list7[i%7]+'.'*(n-7-now-(i//7)*7)
        lists.append(row)
    now+=loop[1]*7
    
    for l in lists:
        print(l)

Solution (Python):
N = int(input())
if N==2:
    print(-1)
    exit()
if N==3:
    print("""aa.
..b
..b""")
    exit()
P4 = """abcc
abdd
ccab
ddab"""
P5 = """abbaa
a..db
b..db
bcc.a
aabba"""
P6 = """aa.ccd
..bd.d
..bdcc
ccdaa.
d.d..b
dcc..b"""
P7 = """.ccbbaa
c...ccd
c...d.d
b...dcc
bccd...
ad.d...
adcc..."""
P = [P4, P5, P6, P7]
def construct(L):
    P_split = [p.split() for p in P]
    ans = []
    offset = 0
    for l in L:
        r = N-offset-l
        for p in P_split[l-4]:
            ans.append("."*offset + p + "."*r)
        offset += l
    return "\n".join(ans)
def sep(n):  
    if n==6:
        return [6]
    elif n==7:
        return [7]
    elif n==11:
        return [5, 6]
    res = [4] * (n//4)
    for i in range(n%4):
        res[i] += 1
    return res
print(construct(sep(N)))

Solution (Python):
import sys
N=int(input())
if N==2:
    print(-1)
    sys.exit()
ans=[["."]*N for i in range(N)]
if N%4==0:
    k=N//4
    for i in range(k):
        num=4*i
        for j in range(2):
            ans[j+num][0+num]="a"
            ans[j+num][1+num]="b"
        for j in range(2,4):
            ans[j+num][2+num]="a"
            ans[j+num][3+num]="b"
        for j in range(2):
            ans[2+num][j+num]="c"
            ans[3+num][j+num]="d"
        for j in range(2,4):
            ans[0+num][j+num]="c"
            ans[1+num][j+num]="d"
    for u in ans:
        print("".join(u))
    sys.exit()
n=N
while n%2==0:
    n//=2
k=N//n
for i in range(k):
    num=n*i
    for j in range(2):
        ans[j+num][num]="a"
        ans[j+num][(n-2)//2+num]="b"
        ans[j+num][n-3+num]="c"
    for j in range(n-2,n):
        ans[2+num][j+num]="a"
        ans[(n+1)//2+num][j+num]="b"
        ans[n-1+num][j+num]="c"
    lsls=["d","e"]
    for j in range(2,(n+1)//2):
        ans[j+num][j-2+num]=lsls[j%2]
        ans[j+num][j-1+num]=lsls[j%2]
    for j in range((n+3)//2,n):
        ans[j+num][j-2+num]=lsls[j%2]
        ans[j+num][j-3+num]=lsls[j%2]
    lsls=["f","g"]
    for j in range(2,(n+1)//2):
        ans[j+num][n-1-j+num]=lsls[j%2]
        ans[j+1+num][n-1-j+num]=lsls[j%2]
    for j in range((n+3)//2,n):
        ans[j+num][n-1-j+num]=lsls[j%2]
        ans[j-1+num][n-1-j+num]=lsls[j%2]
for u in ans:
    print("".join(u))

Solution (Python):
import sys
readline = sys.stdin.readline

imp = set([1, 2])

N = int(readline())
Five = set([5, 10, 14, 19, 23])
dot = ord('.')
if N in imp:
    print(-1)
else:
    G = [[dot]*N for _ in range(N)]
    if N == 7:
        G[0][0] = 97
        G[0][1] = 97
        G[0][2] = 98
        G[0][3] = 98
        G[1][1] = 99
        G[1][2] = 99
        G[2][2] = 100
        G[2][3] = 100
        G[0][6] = 101
        G[1][6] = 101
        G[1][5] = 102
        G[2][5] = 102
        G[2][6] = 103
        G[3][6] = 103
        G[3][4] = 104
        G[4][4] = 104
        G[3][0] = 105
        G[4][0] = 105
        G[5][0] = 106
        G[6][0] = 106
        G[4][1] = 107
        G[5][1] = 107
        G[5][4] = 108
        G[5][5] = 108
        G[6][3] = 109
        G[6][4] = 109
        G[6][5] = 110
        G[6][6] = 110
    elif N == 11:
        G[0][0] = 97
        G[0][1] = 97
        G[0][2] = 98
        G[0][3] = 98
        G[1][1] = 99
        G[1][2] = 99
        G[2][2] = 100
        G[2][3] = 100
        G[0][6] = 101
        G[1][6] = 101
        G[1][5] = 102
        G[2][5] = 102
        G[2][6] = 103
        G[3][6] = 103
        G[3][4] = 104
        G[4][4] = 104
        G[3][0] = 105
        G[4][0] = 105
        G[5][0] = 106
        G[6][0] = 106
        G[4][1] = 107
        G[5][1] = 107
        G[5][4] = 108
        G[5][5] = 108
        G[6][3] = 109
        G[6][4] = 109
        G[6][5] = 110
        G[6][6] = 110
        st = 7
        G[st][st] = 97
        G[st][st+1] = 97
        G[st+1][st] = 98
        G[st+1][st+1] = 98
        G[st+2][st+2] = 99
        G[st+2][st+3] = 99
        G[st+3][st+2] = 100
        G[st+3][st+3] = 100
        G[st+2][st] = 101
        G[st+3][st] = 101
        G[st+2][st+1] = 102
        G[st+3][st+1] = 102
        G[st][st+2] = 103
        G[st+1][st+2] = 103
        G[st][st+3] = 104
        G[st+1][st+3] = 104

    elif N in Five:
        for five in range(N):
            if (N - 5*five) % 9 == 0:
                nine = (N - 5*five) // 9
                break
        for i in range(five):
            st = i*5
            for j in range(3):
                G[st+j][st] = 97+j
                G[st+j][st+1] = 97+j
            G[st][st+2] = 100
            G[st][st+3] = 100
            G[st][st+4] = 101
            G[st+1][st+4] = 101
            G[st+2][st+4] = 102
            G[st+2][st+3] = 102
            G[st+2][st+2] = 103
            G[st+1][st+2] = 103
            for j in range(3):
                G[st+3][st+2+j] = 104+j
                G[st+4][st+2+j] = 104+j
        geta = 5*five
        for i in range(nine):
            st = geta + 9*i
            for j in range(3):
                for k in range(3):
                    G[st+3*j][st+3*k] = 97
                    G[st+3*j][st+3*k+1] = 97
                    G[st+3*j+1][st+3*k+2] = 97
                    G[st+3*j+2][st+3*k+2] = 97
    elif N % 3 == 0:
        for i in range(0, N, 3):
            G[i][i] = 97
            G[i][i+1] = 97
            G[i+1][i+2] = 97
            G[i+2][i+2] = 97
    else:
        for nine in range(N):
            if (N-9*nine) % 4 == 0:
                four = (N-9*nine) // 4
                break
        for i in range(nine):
            st = i*9
            for j in range(3):
                for k in range(3):
                    G[st+3*j][st+3*k] = 97
                    G[st+3*j][st+3*k+1] = 97
                    G[st+3*j+1][st+3*k+2] = 97
                    G[st+3*j+2][st+3*k+2] = 97
        geta = 9*nine
        for i in range(four):
            st = geta + i*4
            G[st][st] = 97
            G[st][st+1] = 97
            G[st+1][st] = 98
            G[st+1][st+1] = 98
            G[st+2][st+2] = 99
            G[st+2][st+3] = 99
            G[st+3][st+2] = 100
            G[st+3][st+3] = 100
            G[st+2][st] = 101
            G[st+3][st] = 101
            G[st+2][st+1] = 102
            G[st+3][st+1] = 102
            G[st][st+2] = 103
            G[st+1][st+2] = 103
            G[st][st+3] = 104
            G[st+1][st+3] = 104
    print('\n'.join([''.join(map(chr, g)) for g in G]))

Solution (Python):
n = int(input())
if n == 2:
    print(-1)
elif n == 3:
    print("aab")
    print("c.b")
    print("cdd")
elif n == 4:
    print("aabc")
    print("ddbc")
    print("efgg")
    print("efhh")
elif n == 5:
    print("abbcc")
    print("a..da")
    print("b..da")
    print("bqq.b")
    print("ccaab")
else:
    if n % 2 == 0:
        r = n // 2
        for i in range(r - 1):
            if i == 0:
                print("a" + "." * (r - 2) + "zz" + "." * (r - 3) + "aa")
            else:
                if i % 2 == 1:
                    print(
                        "." * (i - 1) + "ab" + "." * (n - i * 2 - 3) + "bb" + "." * (i)
                    )
                else:
                    print(
                        "." * (i - 1) + "ba" + "." * (n - i * 2 - 3) + "aa" + "." * (i)
                    )
        if r % 2 == 0:
            print("z" + "." * (r - 3) + "a" + "." * (n - r) + "z")
            print("z" + "." * (n - r) + "a" + "." * (r - 3) + "z")
        else:
            print("z" + "." * (r - 3) + "b" + "." * (n - r) + "z")
            print("z" + "." * (n - r) + "b" + "." * (r - 3) + "z")
        for i in range(r - 2, -1, -1):
            if i == 0:
                print("aa" + "." * (r - 3) + "zz" + "." * (r - 2) + "a")
            else:
                if i % 2 == 1:
                    print(
                        "." * (i) + "bb" + "." * (n - i * 2 - 3) + "ba" + "." * (i - 1)
                    )
                else:
                    print(
                        "." * (i) + "aa" + "." * (n - i * 2 - 3) + "ab" + "." * (i - 1)
                    )
    else:
        r = n // 2
        for i in range(r - 1):
            if i == 0:
                print("a" + "." * (r - 2) + "zz" + "." * (r - 2) + "aa")
            else:
                if i % 2 == 1:
                    print(
                        "." * (i - 1) + "ab" + "." * (n - i * 2 - 3) + "bb" + "." * (i)
                    )
                else:
                    print(
                        "." * (i - 1) + "ba" + "." * (n - i * 2 - 3) + "aa" + "." * (i)
                    )
        if r % 2 == 0:
            print("." * (r - 2) + "a..p" + "." * (r - 2) + "z")
            print("z" + "." * (r - 2) + "..p" + "." * (r - 2) + "z")
            print("z" + "." * (r - 2) + "qq.a" + "." * (r - 2))
        else:
            print("." * (r - 2) + "b..p" + "." * (r - 2) + "z")
            print("z" + "." * (r - 2) + "..p" + "." * (r - 2) + "z")
            print("z" + "." * (r - 2) + "qq.b" + "." * (r - 2))

        for i in range(r - 2, -1, -1):
            if i == 0:
                print("aa" + "." * (r - 2) + "zz" + "." * (r - 2) + "a")
            else:
                if i % 2 == 1:
                    print(
                        "." * (i) + "bb" + "." * (n - i * 2 - 3) + "ba" + "." * (i - 1)
                    )
                else:
                    print(
                        "." * (i) + "aa" + "." * (n - i * 2 - 3) + "ab" + "." * (i - 1)
                    )

Solution (Python):
def GCD(x,y):
    if y == 0:
        return x
    else:
        return GCD(y,x%y)


N = int(input())
if N == 2:
    print(-1)
    exit()
if N == 7:
    ans = [".aabbcc",
           "c..ddee",
           "c..ffgg",
           "hij....",
           "hij....",
           "klm....",
           "klm...."]
    for i in ans:
        print("".join(i))
    exit()
if N == 3:
    print(".aa")
    print("a..")
    print("a..")
    exit()

four = ["aacd",
        "bbcd",
        "efgg",
        "efhh"]
five = ["aabbc",
        "h.iic",
        "hj..d",
        "gj..d",
        "gffee"]
six = ["aacd..",
       "bbcd..",
       "ef..gg",
       "ef..hh",
       "..iikl",
       "..jjkl"]
ans = [list("."*N) for i in range(N)]
a,b,c = [[0,N//5,0],[0,N//5-1,1],[0,N//5-2,2],[2,N//5-1,0],[1,N//5,0]][N%5]
p = 0
for i in range(a):
    for x in range(4):
        for y in range(4):
            ans[p+x][p+y] = four[x][y]
    p += 4

for i in range(b):
    for x in range(5):
        for y in range(5):
            ans[p+x][p+y] = five[x][y]
    p += 5

for i in range(c):
    for x in range(6):
        for y in range(6):
            ans[p+x][p+y] = six[x][y]
    p += 6
for i in ans:
    print("".join(i))

Solution (Python):
import sys
N = int(input())
ans = [['.']*N for _ in range(N)]
def make4(a):
    for j in range(4):
        ans[a][a] = ans[a+1][a] = 'a'
        ans[a][a+1] = ans[a+1][a+1] = 'b'
        ans[a][a+2] = ans[a][a+3] = 'c'
        ans[a+1][a+2] = ans[a+1][a+3] = 'd'

        ans[a+2][a] = ans[a+2][a+1] = 'e'
        ans[a+3][a] = ans[a+3][a+1] = 'f'
        ans[a+2][a+2] = ans[a+3][a+2] = 'g'
        ans[a+2][a+3] = ans[a+3][a+3] = 'h'
def make6(a):
    ans[a+3][a] = ans[a+4][a] = 'a'
    ans[a][a+3] = ans[a][a+4] = 'a'
    ans[a+5][a+1] = ans[a+5][a+2] = 'b'
    ans[a+1][a+5] = ans[a+2][a+5] = 'b'

    ans[a][a] = ans[a+1][a] = 'c'
    ans[a][a+1] = ans[a][a+2] = 'd'
    ans[a+1][a+2] = ans[a+2][a+2] ='e'
    ans[a+2][a] = ans[a+2][a+1]  = 'f'

    ans[a+3][a+3] = ans[a+4][a+3] = 'c'
    ans[a+3][a+4] = ans[a+3][a+5] = 'd'
    ans[a+4][a+5] = ans[a+5][a+5] ='e'
    ans[a+5][a+3] = ans[a+5][a+4]  = 'f'
def make5(a):
    ans[a+2][a+1] = ans[a+3][a+1] = 'a'
    ans[a+1][a+2] = ans[a+1][a+3] = 'a'

    ans[a][a] = ans[a+1][a] = 'b'
    ans[a+2][a] = ans[a+3][a] = 'c'

    ans[a+4][a] = ans[a+4][a+1] = 'd'
    ans[a+4][a+2] = ans[a+4][a+3] = 'e'

    ans[a+4][a+4] = ans[a+3][a+4] = 'f'
    ans[a+2][a+4] = ans[a+1][a+4] = 'g'

    ans[a][a+1] = ans[a][a+2] = 'h'
    ans[a][a+3] = ans[a][a+4] = 'i'




def answer():
    for i in range(N):
        print(*ans[i], sep = '')      

if N == 1 or N == 2:
    print(-1)
elif N == 3:
    a = 0
    ans[a][a] = ans[a+1][a] = 'c'
    ans[a][a+1] = ans[a][a+2] = 'd'
    ans[a+1][a+2] = ans[a+2][a+2] ='e'
    ans[a+2][a] = ans[a+2][a+1]  = 'f'
    answer()
elif N ==7:
    print(".aabbcc",
"e.hhii.",
"ek....p",
"fk....p",
"fl....q",
"gl....q",
"g.mmnn.", sep = '\n')


elif N%2 == 0:
    if N%4 == 0:
        for i in range(0, N, 4):
            make4(i)
        answer()
    else:
        make6(0)
        for i in range(6, N, 4):
            make4(i)
        answer()
else:
    make5(0)
    if (N-5)%4 == 0:
        for i in range(5, N, 4):
            make4(i)
    else:
        make6(5)
        for i in range(5+6, N, 4):
            make4(i)
    answer()

Solution (Python):
def GCD(x,y):
    if y == 0:
        return x
    else:
        return GCD(y,x%y)


N = int(input())
if N == 2:
    print(-1)
    exit()
if N == 7:
    ans = [".aaggcc",
           "c..ddee",
           "c..ffgg",
           "hij....",
           "hij....",
           "klm....",
           "klm...."]
    for i in ans:
        print("".join(i))
    exit()
if N == 3:
    print(".aa")
    print("a..")
    print("a..")
    exit()

four = ["aacd",
        "bbcd",
        "efgg",
        "efhh"]
five = ["aabbc",
        "h.iic",
        "hj..d",
        "gj..d",
        "gffee"]
six = ["aacd..",
       "bbcd..",
       "ef..gg",
       "ef..hh",
       "..iikl",
       "..jjkl"]
ans = [list("."*N) for i in range(N)]
a,b,c = [[0,N//5,0],[0,N//5-1,1],[0,N//5-2,2],[2,N//5-1,0],[1,N//5,0]][N%5]
p = 0
for i in range(a):
    for x in range(4):
        for y in range(4):
            ans[p+x][p+y] = four[x][y]
    p += 4

for i in range(b):
    for x in range(5):
        for y in range(5):
            ans[p+x][p+y] = five[x][y]
    p += 5

for i in range(c):
    for x in range(6):
        for y in range(6):
            ans[p+x][p+y] = six[x][y]
    p += 6
for i in ans:
    print("".join(i))

Solution (Python):
n=int(input())

three_tmp = ["aac","b.c","bdd"]
four_tmp  = ["abcc","abdd","eegh","ffgh"]
five_tmp  = ["aabee","c.bff","cddgg","hij..","hij.."]
seven_tmp = ["aa..dee",".bb.d.f","..ccggf","h.iil..","h...lm.","...k.mn","jj.k..n"]

if n==2:
    print(-1)
elif n==3:
    for x in three_tmp:
        print(x)
elif n==4:
    for x in four_tmp:
        print(x)
elif n==5:
    for x in five_tmp:
        print(x)
elif n==6:
    for x in three_tmp:
        print(x+"."*3)
    for x in three_tmp:
        print("."*3+x)
elif n==7:
    for x in seven_tmp:
        print(x)
elif n==8:
    for x in four_tmp:
        print(x+"."*4)
    for x in four_tmp:
        print("."*4+x)
elif n==11:
    for x in four_tmp:
        print(x+"."*7)
    for x in seven_tmp:
        print("."*4+x)
else:
    res=n
    while res != 0:
        if res % 4 != 0:
            for x in five_tmp:
                print("."*(n-res) + x + "."*(res-5))
            res-=5
        else:
            for x in four_tmp:
                print("."*(n-res) + x + "."*(res-4))
            res-=4

Solution (Python):
n = int(input())

if n % 3 == 0:
    m = n // 3
    for i in range(m):
        print('.' * (3 * i) + 'abb' + '.' * (n - 3 - 3 * i))
        print('.' * (3 * i) + 'a.a' + '.' * (n - 3 - 3 * i))
        print('.' * (3 * i) + 'bba' + '.' * (n - 3 - 3 * i))
elif n % 4 == 0:
    m = n // 4
    for i in range(m):
        print('.' * (4 * i) + 'abaa' + '.' * (n - 4 - 4 * i))
        print('.' * (4 * i) + 'abcc' + '.' * (n - 4 - 4 * i))
        print('.' * (4 * i) + 'ccba' + '.' * (n - 4 - 4 * i))
        print('.' * (4 * i) + 'aaba' + '.' * (n - 4 - 4 * i))
else:
    if n % 3 == 1:
        if n >= 10:
            print('abaa' + '.' * (n - 4))
            print('abcc' + '.' * (n - 4))
            print('ccba' + '.' * (n - 4))
            print('aaba' + '.' * (n - 4))

            m = (n - 4) // 3
            for i in range(m):
                if i == m - 1:
                    print('.' * 4 + 'dd.' + '.' * (n - 10) + 'abb')
                    print('.' * 4 + '..d' + '.' * (n - 10) + 'a.a')
                    print('.' * 4 + '..d' + '.' * (n - 10) + 'bba')
                else:
                    print('.' * (4 + 3 * i) + 'abbdd.' + '.' * (n - 10 - 3 * i))
                    print('.' * (4 + 3 * i) + 'a.a..d' + '.' * (n - 10 - 3 * i))
                    print('.' * (4 + 3 * i) + 'bba..d' + '.' * (n - 10 - 3 * i))
        elif n == 7:
            print('a.aa..a')
            print('ab....a')
            print('.b..aab')
            print('aabb..b')
            print('.b.aacc')
            print('aba....')
            print('a.a.aa.')
        else: 
            print('abaa')
            print('abcc')
            print('ccba')
            print('aaba')

    elif n % 3 == 2:
        if n >= 14:
            m = (n - 8) // 3

            print('abaa' + '.' * (n - 4))
            print('abcc' + '.' * (n - 4))
            print('ccba' + '.' * (n - 4))
            print('aaba' + '.' * (n - 4))
            print('....abaa' + '.' * (n - 8))
            print('....abcc' + '.' * (n - 8))
            print('....ccba' + '.' * (n - 8))
            print('....aaba' + '.' * (n - 8))

            m = (n - 8) // 3
            for i in range(m):
                if i == m - 1:
                    print('.' * 8 + 'dd.' + '.' * (n - 14) + 'abb')
                    print('.' * 8 + '..d' + '.' * (n - 14) + 'a.a')
                    print('.' * 8 + '..d' + '.' * (n - 14) + 'bba')
                else:
                    print('.' * (8 + 3 * i) + 'abbdd.' + '.' * (n - 14 - 3 * i))
                    print('.' * (8 + 3 * i) + 'a.a..d' + '.' * (n - 14 - 3 * i))
                    print('.' * (8 + 3 * i) + 'bba..d' + '.' * (n - 14 - 3 * i))
        elif n == 11:
            print('a.aa..a' + '....')
            print('ab....a' + '....')
            print('.b..aab' + '....')
            print('aabb..b' + '....')
            print('.b.aacc' + '....')
            print('aba....' + '....')
            print('a.a.aa.' + '....')
            print('.' * 7 + 'abaa')
            print('.' * 7 + 'abcc')
            print('.' * 7 + 'ccba')
            print('.' * 7 + 'aaba')
        elif n == 8:
            print('abaa....')
            print('abcc....')
            print('ccba....')
            print('aaba....')
            print('....abaa')
            print('....abcc')
            print('....ccba')
            print('....aaba')
        elif n == 5:
            print('abbaa')
            print('a..bc')
            print('bb.bc')
            print('a.aab')
            print('abb.b')
        else:
            print(-1)