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<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,a[1005][1005];
char ans[2005][2005],tmp[15][15];
void fill_33(int stx,int sty,char c)
{
	ans[stx][sty]=ans[stx][sty+1]=c;
	ans[stx][sty+2]=ans[stx+1][sty+2]=c+1;
	ans[stx+1][sty]=ans[stx+2][sty]=c+2;
	ans[stx+2][sty+1]=ans[stx+2][sty+2]=c+3;
}
void fill_44(int stx,int sty,char c)
{
	ans[stx][sty]=ans[stx][sty+1]=c;
	ans[stx+1][sty]=ans[stx+1][sty+1]=c+1;
	ans[stx][sty+2]=ans[stx+1][sty+2]=c+2;
	ans[stx][sty+3]=ans[stx+1][sty+3]=c+3;
	ans[stx+2][sty]=ans[stx+3][sty]=c+4;
	ans[stx+2][sty+1]=ans[stx+3][sty+1]=c+5;
	ans[stx+2][sty+2]=ans[stx+2][sty+3]=c+6;
	ans[stx+3][sty+2]=ans[stx+3][sty+3]=c+7;
}
void fill_45(int stx,int sty,char c)
{
	ans[stx][sty]=ans[stx+1][sty]=c;
	ans[stx][sty+1]=ans[stx][sty+2]=c+1;
	ans[stx][sty+3]=ans[stx][sty+4]=c+2;
	ans[stx+1][sty+2]=ans[stx+2][sty+2]=c+3;
	ans[stx+1][sty+3]=ans[stx+1][sty+4]=c+4;
	ans[stx+2][sty]=ans[stx+2][sty+1]=c+5;
	ans[stx+3][sty]=ans[stx+3][sty+1]=c+6;
	ans[stx+3][sty+2]=ans[stx+3][sty+3]=c+7;
	ans[stx+2][sty+4]=ans[stx+3][sty+4]=c+8;
}
void fill_54(int stx,int sty,char c)
{
	fill_45(1001,1001,c);
	for(int i=0;i<5;i++)
	  for(int j=0;j<4;j++)
	    ans[stx+i][sty+j]=ans[1001+j][1001+i];
}
void fill_55(int stx,int sty,char c)
{
	ans[stx][sty]=ans[stx][sty+1]=c;
	ans[stx][sty+2]=ans[stx][sty+3]=c+1;
	ans[stx][sty+4]=ans[stx+1][sty+4]=c+2;
	ans[stx+1][sty]=ans[stx+2][sty]=c+3;
	ans[stx+1][sty+1]=ans[stx+1][sty+2]=c+4;
	ans[stx+3][sty]=ans[stx+4][sty]=c+5;
	ans[stx+4][sty+1]=ans[stx+4][sty+2]=c+6;
	ans[stx+4][sty+3]=ans[stx+4][sty+4]=c+7;
	ans[stx+2][sty+3]=ans[stx+3][sty+3]=c+8;
	ans[stx+2][sty+4]=ans[stx+3][sty+4]=c+9;
}
void print()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(ans[i][j]>='a'&&ans[i][j]<='z')putchar(ans[i][j]);
			else putchar('.');
		}
		putchar('\n');
	}
}
int s1[15],s2[15];
bool flag[15][15];
void dfs(int nowx,int nowy,int step)
{
	while(flag[nowx][nowy])
	{
		nowy++;
		if(nowy>n)nowy=1,nowx++;
	}
	if(nowx!=1&&s1[nowx-1]!=3)return;
	if(nowx>n)
	{
		for(int i=1;i<=n;i++)
		  if(s1[i]!=3||s2[i]!=3)
		    return;
		print();
		exit(0);
	}
	if(nowy<n&&!flag[nowx][nowy+1])
	{
		flag[nowx][nowy]=flag[nowx][nowy+1]=1;
		ans[nowx][nowy]=ans[nowx][nowy+1]='a'+step;
		s1[nowx]++,s2[nowy]++,s2[nowy+1]++;
		if(s1[nowx]>3||s2[nowy]>3||s2[nowy+1]>3);
		else
		{
			if(nowy==n-1)dfs(nowx+1,1,step+1);
			else dfs(nowx,nowy+2,step+1);
		}
		flag[nowx][nowy]=flag[nowx][nowy+1]=0;
		ans[nowx][nowy]=ans[nowx][nowy+1]='.';
		s1[nowx]--,s2[nowy]--,s2[nowy+1]--;
	}
	if(nowx<n&&!flag[nowx+1][nowy])
	{
		flag[nowx][nowy]=flag[nowx+1][nowy]=1;
		ans[nowx][nowy]=ans[nowx+1][nowy]='a'+step;
		s1[nowx]++,s2[nowy]++,s1[nowx+1]++;
		if(s1[nowx]>3||s2[nowy]>3||s1[nowx+1]>3);
		else
		{
			if(nowy==n)dfs(nowx+1,1,step+1);
			else dfs(nowx,nowy+1,step+1);
		}
		flag[nowx][nowy]=flag[nowx+1][nowy]=0;
		ans[nowx][nowy]=ans[nowx+1][nowy]='.';
		s1[nowx]--,s2[nowy]--,s1[nowx+1]--;
	}
	if(nowy==n)dfs(nowx+1,1,step);
	else dfs(nowx,nowy+1,step);
}
int main()
{
	scanf("%d",&n);
	if(n==2)
	{
		printf("-1\n");
		return 0;
	}
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=n;j++)
	    ans[i][j]='.';
	if(n%3==0)
	{
		for(int i=1;i<=n;i+=3)
		  for(int j=1;j<=n;j+=3)
		    fill_33(i,j,'a');
		print();
		return 0;
	}
	if(n==7||n==11)dfs(1,1,0);
	for(int x=0;x*4<=n;x++)
	{
		if((n-x*4)%5==0)
		{
			int y=(n-x*4)/5;
			for(int i=0;i<x;i++)
			  for(int j=0;j<x;j++)
			    fill_44(i*4+1,j*4+1,'a');
			for(int i=0;i<y;i++)
			  for(int j=0;j<x;j++)
			    fill_54(4*x+i*5+1,j*4+1,'a');
			for(int i=0;i<x;i++)
			  for(int j=0;j<y;j++)
			    fill_45(i*4+1,4*x+j*5+1,'a');
			for(int i=0;i<y;i++)
			  for(int j=0;j<y;j++)
			    fill_55(4*x+i*5+1,4*x+j*5+1,'a');
			print();
			return 0;
		}
	}
	return 0;
}