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: Accepted)

#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>

using namespace std;

typedef long long ll;

#ifdef iq
  mt19937 rnd(228);
#else
  mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

bool vis[1000][1000];
int id[1000][1000];

int sz = 0;

int n;

int real_n;

char ans[1000][1000];

void func(int i, int j) {
  {
    set <int> t;
    for (int x = 0; x < i; x++) {
      set <int> q;
      for (int y = 0; y < n; y++) {
        if (vis[x][y]) {
          q.insert(id[x][y]);
        }
      }
      if (q.size() > 3) return;
      t.insert(q.size());
    }
    if (t.size() > 1) return;
  }
  if (j == n) {
    i++;
    j = 0;
  }
  if (i == n) {
    set <int> t;
    for (int x = 0; x < n; x++) {
      set <int> q;
      for (int y = 0; y < n; y++) {
        if (vis[x][y]) {
          q.insert(id[x][y]);
        }
      }
      t.insert(q.size());
    }
    for (int y = 0; y < n; y++) {
      set <int> q;
      for (int x = 0; x < n; x++) {
        if (vis[x][y]) {
          q.insert(id[x][y]);
        }
      }
      t.insert(q.size());
    }
    if (!*t.begin()) return;
    if (t.size() == 1) {
      if (*t.begin() == 3) {
        map <int, char> t;
        char cur = 'a';
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            if (vis[i][j]) {
              if (!t.count(id[i][j])) {
                t[id[i][j]] = cur++;
              }
              ans[i][j] = t[id[i][j]];
            } else {
              ans[i][j] = '.';
            }
          }
        }
        while (n < real_n) {
          ans[n][n] = 'a';
          ans[n + 1][n] = 'a';
          ans[n][n + 1] = 'b';
          ans[n + 1][n + 1] = 'b';
          ans[n][n + 2] = 'c';
          ans[n][n + 3] = 'c';
          ans[n + 1][n + 2] = 'd';
          ans[n + 1][n + 3] = 'd';
          ans[n + 2][n + 2] = 'e';
          ans[n + 3][n + 2] = 'e';
          ans[n + 2][n + 3] = 'f';
          ans[n + 3][n + 3] = 'f';
          ans[n + 2][n + 0] = 'g';
          ans[n + 2][n + 1] = 'g';
          ans[n + 3][n + 0] = 'h';
          ans[n + 3][n + 1] = 'h';
          n += 4;
        }
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            cout << ans[i][j];
          }
          cout << '\n';
        }
        exit(0);
      }
    }
    return;
  } else {
    if (i + 1 < n && !vis[i + 1][j] && !vis[i][j]) {
      sz++;
      vis[i][j] = 1;
      vis[i + 1][j] = 1;
      id[i][j] = sz;
      id[i + 1][j] = sz;
      func(i, j + 1);
      vis[i][j] = 0;
      vis[i + 1][j] = 0;
    }
    if (j + 1 < n && !vis[i][j] && !vis[i][j + 1]) {
      sz++;
      vis[i][j] = 1;
      vis[i][j + 1] = 1;
      id[i][j] = sz;
      id[i][j + 1] = sz;
      func(i, j + 1);
      vis[i][j] = 0;
      vis[i][j + 1] = 0;
    }
    func(i, j + 1);
  }
}

int main() {
#ifdef iq
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans[i][j] = '.';
  real_n = n;
  if (n == 2) {
    cout << -1 << endl;
    return 0;
  }
  if (n == 3) {
    cout << "aab\n";
    cout << "c.b\n";
    cout << "cdd\n";
    return 0;
  }
  int its = 0;
  while (n - 4 >= 4) {
    n -= 4;
    its++;
  }
  func(0, 0);
}