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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn set_residual(offset: usize, grid: &mut Vec<Vec<char>>) {
    let n = grid.len();
    for xx in 0..(n - offset) / 4 {
        for yy in 0..(n - offset) / 4 {
            if xx != yy {
                continue;
            }
            for y in 0..2 {
                for x in 0..2 {
                    let x0 = 4 * xx + x * 2 + offset;
                    let y0 = 4 * yy + y * 2 + offset;
                    if x % 2 == y % 2 {
                        grid[y0][x0] = 'a';
                        grid[y0 + 1][x0] = 'a';
                        grid[y0][x0 + 1] = 'b';
                        grid[y0 + 1][x0 + 1] = 'b';
                    } else {
                        grid[y0][x0] = 'c';
                        grid[y0][x0 + 1] = 'c';
                        grid[y0 + 1][x0] = 'd';
                        grid[y0 + 1][x0 + 1] = 'd';
                    }
                }
            }
        }
    }
}

fn main() {
    let n = read::<usize>();
    if n == 2 {
        println!("-1");
        return;
    }
    let mut grid = vec![vec!['.'; n]; n];
    if n % 4 == 0 {
        set_residual(0, &mut grid);
    } else if n % 4 == 2 {
        let mut rows = vec![];
        rows.push("aab..e");
        rows.push("d.b..e");
        rows.push("dccff.");
        rows.push("..eaab");
        rows.push("..ed.b");
        rows.push("ff.dcc");
        for (y, ref row) in rows.into_iter().enumerate() {
            for (x, ch) in row.chars().enumerate() {
                grid[y][x] = ch;
            }
        }
        set_residual(6, &mut grid);
    } else if n == 3 {
        grid[0] = vec!['.', '.', 'a'];
        grid[1] = vec!['.', '.', 'a'];
        grid[2] = vec!['a', 'a', '.'];
    } else if n % 4 == 1 {
        let mut rows = vec![];
        rows.push("aabbc");
        rows.push("h..ic");
        rows.push("h..id");
        rows.push("gjj.d");
        rows.push("gffee");
        for (y, ref row) in rows.into_iter().enumerate() {
            for (x, ch) in row.chars().enumerate() {
                grid[y][x] = ch;
            }
        }
        set_residual(5, &mut grid);
    } else {
        let mut rows = vec![];
        rows.push("ee..ggh");
        rows.push("p.ff..h");
        rows.push("p...bl.");
        rows.push(".o..bl.");
        rows.push(".oaa..j");
        rows.push("n..ll.j");
        rows.push("nmm..kk");
        for (y, ref row) in rows.into_iter().enumerate() {
            for (x, ch) in row.chars().enumerate() {
                grid[y][x] = ch;
            }
        }
        set_residual(7, &mut grid);
    }

    for row in grid {
        for mass in row {
            print!("{}", mass);
        }
        println!("");
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}