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 in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include "bits/stdc++.h"
using namespace std;
#define f first
#define s second
#define pb push_back
#define reset(a,b) memset(a,b,sizeof a);
#define ALL(x) x.begin(),x.end()
 
template <typename T>
void cetak(T t){cout << t << ')' << endl;}
template <typename T, typename... V>
void cetak(T t, V... v) {cout << t; if (sizeof...(v)) cout << ", "; cetak(v...);}
 
#define debug(x...) cout << '(' << #x << ") = ("; cetak(x) 
#define sz(x) (int)(x.size())
#define lt(x) (int)(x.length())


const int MOD = 1e9 + 7;

int add(int x, int y){
    int ret = x + y;
    if(ret >= MOD) ret -= MOD;
    return ret;
}
int sub(int x, int y){
    int ret = x - y;
    if(ret < 0) ret += MOD;
    return ret;
}
int mul(int x, int y){
    return (long long)x * y % MOD;
}


const int mx = 1e5 + 10;


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    if(n == 2){
        cout << -1 << endl;
        return 0;
    }
    // assert(n != 5);
    vector<vector<char>> ans(n, vector<char>(n, '.'));
    if(n % 4 == 0){
        for(int i = 0; i < n; i += 4){
            ans[i][i] = 'a';
            ans[i][i + 1] = 'a';
            ans[i + 1][i] = 'b';
            ans[i + 1][i + 1] = 'b';
            ans[i + 2][i + 2] = 'c';
            ans[i + 2][i + 1 + 2] = 'c';
            ans[i + 1 + 2][i + 2] = 'd';
            ans[i + 1 + 2][i + 1 + 2] = 'd';
            ans[i + 2][i] = 'e';
            ans[i + 3][i] = 'e';
            ans[i + 2][i + 1] = 'f';
            ans[i + 3][i + 1] = 'f';
            ans[i][i + 2] = 'g';
            ans[i + 1][i + 2] = 'g';
            ans[i][i + 3] = 'h';
            ans[i + 1][i + 3] = 'h';
        }
    }
    else if(n % 3 == 0){
        for(int i = 0; i < n; i += 3){
            ans[i][i] = 'a';
            ans[i][i + 1] = 'a';
            ans[i][i + 2] = 'b';
            ans[i + 1][i + 2] = 'b';
            ans[i + 2][i + 1] = 'c';
            ans[i + 2][i + 2] = 'c';
            ans[i + 1][i] = 'd';
            ans[i + 2][i] = 'd';
        }
    }
    else if(n % 2 == 0){
        for(int i = 0; i < 6; i += 6){
            ans[i][i] = 'a';
            ans[i][i + 1] = 'a';
            ans[i][i + 2] = 'b';
            ans[i + 1][i + 2] = 'b';
            ans[i + 2][i + 1] = 'c';
            ans[i + 2][i + 2] = 'c';
            ans[i + 1][i] = 'd';
            ans[i + 2][i] = 'd';
            ans[3 + i][i] = 'e';
            ans[3 + i][i + 1] = 'e';
            ans[3 + i + 1][i + 2] = 'f';
            ans[3 + i + 2][i + 2] = 'f';
            ans[i][i + 3] = 'e';
            ans[i][i + 1 + 3] = 'e';
            ans[i + 1][i + 2 + 3] = 'f';
            ans[i + 2][i + 2 + 3] = 'f';
            ans[3 + i][i + 3] = 'a';
            ans[3 + i][i + 1 + 3] = 'a';
            ans[3 + i][i + 2 + 3] = 'b';
            ans[3 + i + 1][i + 2 + 3] = 'b';
            ans[3 + i + 2][i + 1 + 3] = 'c';
            ans[3 + i + 2][i + 2 + 3] = 'c';
            ans[3 + i + 1][i + 3] = 'd';
            ans[3 + i + 2][i + 3] = 'd';
        }
        for(int i = 6; i < n; i += 4){
            ans[i][i] = 'a';
            ans[i][i + 1] = 'a';
            ans[i + 1][i] = 'b';
            ans[i + 1][i + 1] = 'b';
            ans[i + 2][i + 2] = 'c';
            ans[i + 2][i + 1 + 2] = 'c';
            ans[i + 1 + 2][i + 2] = 'd';
            ans[i + 1 + 2][i + 1 + 2] = 'd';
            ans[i + 2][i] = 'e';
            ans[i + 3][i] = 'e';
            ans[i + 2][i + 1] = 'f';
            ans[i + 3][i + 1] = 'f';
            ans[i][i + 2] = 'g';
            ans[i + 1][i + 2] = 'g';
            ans[i][i + 3] = 'h';
            ans[i + 1][i + 3] = 'h';
        }
    }
    else if(n % 2 == 1){
        int i = 0, j = 0;
        int vi = 0, vj = 1;
        int apa = 0;
        char tadi;
        while(1){
            if(i == n / 2 && j == n / 2) break;
            // nyari huruf
            set<char> ini;
            for(int xx = -2; xx <= 2; xx++){
                for(int yy = -2; yy <= 2; yy++){
                    int I = i + xx;
                    int J = j + yy;
                    if(0 <= I && I < n && 0 <= J && J < n) ini.insert(ans[I][J]);
                }
            }
            if(apa){
                apa = 0;
                ans[i][j] = tadi;
            }
            else{
                char jadi = 'a';
                while(ini.count(jadi)) jadi += 1;
                ans[i][j] = jadi;
                apa = 1;
                tadi = jadi;
            }
            
            if(i + vi >= n || i + vi < 0 || j + vj >= n || j + vj < 0 || ans[i + vi][j + vj] != '.'){
                if(vj == 1){
                    vj = 0;
                    vi = 1;
                }
                else if(vj == -1){
                    vi = -1;
                    vj = 0;
                }
                else if(vi == 1){
                    vj = -1;
                    vi = 0;
                }
                else{
                    vj = 1;
                    vi = 0;
                }
            }
            i += vi;
            j += vj;
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++) cout << ans[i][j];
        cout << '\n';
    }
}