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 S4: [&'static str; 4] = ["aabc", "ddbc", "efgg", "efhh"];
const S5: [&'static str; 5] = ["aabbc", "dde.c", "ffegg", "..hij", "..hij"];
const S6: [&'static str; 6] = ["aabbcc", "ddeeff", "ghi...", "ghi...", "...jkl", "...jkl"];
const S7: [&'static str; 7] = [
    "aabbcc.", "ddeef..", "gg..fhh", "..ij..k", "..ij..k", "....lmn", "....lmn",
];

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

    let n: usize = sc.read();
    if n == 2 {
        println!("-1");
        return;
    }
    if n == 3 {
        println!("aab");
        println!("c.b");
        println!("cdd");
        return;
    }
    let mut board = vec![vec!['.'; n]; n];
    let r = n % 4 + 4;
    let repeats = (n - r) / 4;
    for k in 0..repeats {
        for i in 0..4 {
            let s: Vec<_> = S4[i].chars().collect();
            for j in 0..4 {
                board[k * 4 + i][k * 4 + j] = s[j];
            }
        }
    }

    for i in 0..r {
        let s: Vec<_> = match r {
            4 => S4[i].chars().collect(),
            5 => S5[i].chars().collect(),
            6 => S6[i].chars().collect(),
            7 => S7[i].chars().collect(),
            _ => unreachable!(),
        };
        for j in 0..r {
            board[repeats * 4 + i][repeats * 4 + j] = s[j];
        }
    }

    for i in 0..n {
        for j in 0..n {
            print!("{}", board[i][j]);
        }
        println!();
    }
}

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

    let next_pos = if j + 1 == n { (i + 1, 0) } else { (i, j + 1) };

    if j + 1 < n && board[i][j] == 0 && board[i][j + 1] == 0 {
        board[i][j] = depth;
        board[i][j + 1] = depth;

        if dfs(k, board, next_pos, depth + 1) {
            return true;
        }
        board[i][j] = 0;
        board[i][j + 1] = 0;
    }
    if i + 1 < n && board[i][j] == 0 && board[i + 1][j] == 0 {
        board[i][j] = depth;
        board[i + 1][j] = depth;
        if dfs(k, board, next_pos, depth + 1) {
            return true;
        }
        board[i][j] = 0;
        board[i + 1][j] = 0;
    }
    if dfs(k, board, next_pos, depth) {
        true
    } else {
        false
    }
}

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) -> Self {
        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()
    }
}