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)

#[test]
fn check_solve() {
    assert_eq!(solve("2"), "-1\n");
    for &n in &[3,4,5,6,7,8,9,10,11,100,123,456,789,998,999,1000] {
        let out = solve(&n.to_string());
        let sw = out.split_whitespace();
        let brd: Vec<Vec<_>> = sw.map(|r| r.chars().collect()).collect();
        let mut q_pre = 0;
        for i in 0..n {
            let mut q = 0;
            let mut prev = '.';
            for j in 0..n {
                if brd[j][i] != '.' && brd[j][i] != prev {
                    q += 1;
                }
                prev = brd[j][i];
            }
            if i != 0 {
                assert_eq!(q, q_pre);
            }
            q_pre = q;
        }
        for i in 0..n {
            let mut q = 0;
            let mut prev = '.';
            for j in 0..n {
                if brd[i][j] != '.' && brd[i][j] != prev {
                    q += 1;
                }
                prev = brd[i][j];
            }
            assert_eq!(q, q_pre);
            q_pre = q;
        }
    }
}

fn solve(src: &str) -> String {
    let mut sc = Scanner::from_str(src);

    let p32 = vec![
        "aab",
        "b.b",
        "baa"
    ];
    let p43 = vec![
        "aaca",
        "bbca",
        "acbb",
        "acaa"
    ];
    let p53 = vec![
        "aabba",
        "bcc.a",
        "b..cb",
        "a..cb",
        "abbaa"
    ];
    let p73 = vec![
        ".aabbaa",
        "a..a..b",
        "a..a..b",
        "baa.bb.",
        "b..b..a",
        "a..b..a",
        "abb.aa.",
    ];

    let n: usize = sc.read();

    let mut brd = vec![vec!['.'; n + 1]; n];
    for i in 0..n {
        brd[i][n] = '\n';
    }
    if n == 2 {
        "-1\n".to_string()
    } else {
        match n {
            3 => put(&mut brd, 0, &p32),
            6 => {
                put(&mut brd, 0, &p32);
                put(&mut brd, 3, &p32);
            },
            7 => put(&mut brd, 0, &p73),
            11 => {
                put(&mut brd, 0, &p43);
                put(&mut brd, 4, &p73);
            },
            n => {
                let mut cnt = 0;
                while (n - cnt) % 4 != 0 {
                    put(&mut brd, cnt, &p53);
                    cnt += 5;
                }
                while cnt < n {
                    put(&mut brd, cnt, &p43);
                    cnt += 4;
                }
            }
        }
        brd.iter().flat_map(|v| v.clone().into_iter()).collect()
    }
}

fn put(brd: &mut [Vec<char>], pos: usize, pat: &[&str]) {
    for (i, row) in pat.iter().enumerate() {
        for (j, c) in row.chars().enumerate() {
            brd[pos+i][pos+j] = c;
        }
    }
}

fn main() {
    let mut buf = String::new();
    use std::io;
    use std::io::{Read, Write};
    io::stdin().read_to_string(&mut buf).unwrap();
    io::stdout().write_all(solve(&buf).as_bytes()).unwrap();
}

struct Scanner<'a>(std::str::SplitWhitespace<'a>);
impl<'a> Scanner<'a> {
    fn from_str(src: &str) -> Scanner {
        Scanner(src.split_whitespace())
    }
    fn get(&mut self) -> &str {
        self.0.next().unwrap()
    }
    fn read<T: std::str::FromStr>(&mut self) -> T {
        self.get().parse().ok().unwrap()
    }
}