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)

const INF: usize = 1 << 50;

const B7: [[char; 7]; 7] = [
    ['a', 'b', 'a', '.', '.', '.', '.'],
    ['a', 'b', 'a', '.', '.', '.', '.'],
    ['b', 'a', 'c', 'c', '.', '.', '.'],
    ['b', 'a', '.', 'z', 'z', '.', '.'],
    ['a', '.', '.', '.', 'y', 'j', 'j'],
    ['a', '.', '.', '.', 'y', 'k', 'k'],
    ['.', 'a', 'a', 'x', 'x', 't', 't'],
];

const B6: [[char; 6]; 6] = [
    ['a', 'b', 'c', '.', '.', '.'],
    ['a', 'b', 'c', '.', '.', '.'],
    ['d', '.', 'f', 'f', 'g', 'g'],
    ['d', 'e', 'h', 'h', '.', '.'],
    ['.', 'e', '.', 'j', 'k', 'k'],
    ['i', 'i', '.', 'j', 'l', 'l'],
];

const B5: [[char; 5]; 5] = [
    ['a', 'b', 'c', '.', '.'],
    ['a', 'b', 'c', '.', '.'],
    ['d', 'e', 'e', 'f', 'f'],
    ['d', '.', 'i', 'g', 'g'],
    ['h', 'h', 'i', 'j', 'j'],
];

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n = sc.read();

    let b4: Vec<Vec<_>> = vec![
        "abcc", //
        "abdd", //
        "eefh", //
        "xxfh",
    ]
    .into_iter()
    .map(|s| s.chars().collect())
    .collect();

    if n <= 2 {
        println!("-1");
        return;
    }
    if n == 3 {
        println!("abb");
        println!("a.c");
        println!("bbc");
        return;
    }

    let k = n / 4 - 1;
    let r = n % 4 + 4;
    let mut ans = vec![vec!['.'; n]; n];
    for x in 0..k {
        for i in 0..4 {
            for j in 0..4 {
                ans[i + 4 * x][j + 4 * x] = b4[i][j];
            }
        }
    }
    for i in 0..r {
        for j in 0..r {
            ans[i + 4 * k][j + 4 * k] = match r {
                4 => b4[i][j],
                5 => B5[i][j],
                6 => B6[i][j],
                7 => B7[i][j],
                _ => unreachable!(),
            };
        }
    }

    for i in 0..n {
        for j in 0..n {
            sc.write(ans[i][j]);
        }
        sc.write("\n");
    }
}

fn brute_force(n: usize) {
    let mut board = vec![vec![INF; n]; n];
    let is_ok = rec(&mut board, 0, 3);
    if is_ok {
        println!("{:?}", board);
    }
}

fn rec(board: &mut Vec<Vec<usize>>, pos: usize, limit: u32) -> bool {
    let n = board.len();
    if pos == n * n {
        for i in 0..n {
            let mut set = 0;
            for j in 0..n {
                if board[i][j] == INF {
                    continue;
                }
                set |= 1 << board[i][j];
            }
            let count = (set as usize).count_ones();
            if count != limit {
                return false;
            }
        }
        for j in 0..n {
            let mut set = 0;
            for i in 0..n {
                if board[i][j] == INF {
                    continue;
                }
                set |= 1 << board[i][j];
            }
            let count = (set as usize).count_ones();
            if count != limit {
                return false;
            }
        }
        return true;
    }

    let (i, j) = (pos / n, pos % n);
    if j == n - 1 {
        let count = board[i]
            .iter()
            .filter(|&&j| j != INF)
            .fold(0, |sum, &j| sum | (1 << j));
        let count = (count as i32).count_ones();
        if count != limit {
            return false;
        }
    }
    if board[i][j] != INF {
        return rec(board, pos + 1, limit);
    }
    if i + 1 < n && board[i + 1][j] == INF {
        board[i][j] = pos;
        board[i + 1][j] = pos;
        if rec(board, pos + 1, limit) {
            return true;
        }
        board[i][j] = INF;
        board[i + 1][j] = INF;
    }
    if j + 1 < n && board[i][j + 1] == INF {
        board[i][j] = pos;
        board[i][j + 1] = pos;
        if rec(board, pos + 1, limit) {
            return true;
        }
        board[i][j] = INF;
        board[i][j + 1] = INF;
    }
    return rec(board, pos + 1, limit);
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}