跳到正文

题解

题解 AT5694 【[AGC041C] Domino Quality】

CYJian约 4 分钟
原文
文章信息与版本

文章信息

发布
2020年2月12日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 4 分钟

版本

  1. v1当前

这题妙啊


首先不难发现,n<3n<3 是无解的。

n=3n=3 可以非常方便地构造:

代码
e..
e..
.ee

然后考虑更大的怎么办。

我们考虑构造多米诺数量为 33 的矩阵:

n=4n=4:

代码
orzz
oree
eero
zzro

n=5n=5:

代码
orrzz
oe..e
ae..e
a.eez
oorrz

n=6n=6:

代码
oorrzz
.e.e.e
.e.e.e
e.e.e.
e.e.e.
oorrzz

n=7n=7:

代码
.oorrzz
oaee...
oa.a...
reea...
r...eaa
z...e.e
z...aae

打完这些表后,聪明的观众可能就会说:我会做了!

对于 n≥8n \ge 8 的那些表格,我们只需要用上面四个凑就行了。将这些东西放在对角线上就可以了。

比如,对于 n=9n=9,我们可以用一个 n=4n=4 和一个 n=5n=5 来凑:

代码
orzz.....
oree.....
eero.....
zzro.....
....orrzz
....oe..e
....ae..e
....a.eez
....oorrz

更大的也用类似的方法凑就行了。大概就先将 n mod 4n \bmod 4 算出最后凑上哪一个,然后再一直凑 44 直到凑到 nn 就行了。

Code\rm Code:

C++
const string S4[4] = {"abcc", "abdd", "ccab", "ddab"};
const string S5[5] = {"abbcc", "ad..a", "bd..a", "b.eez", "oorrz"};
const string S6[6] = {"oorrzz", ".a.b.c", ".a.b.c", "d.e.f.", "d.e.f.", "oorrzz"};
const string S7[7] = {".oorrzz", "oabb...", "oa.c...", "rddc...", "r...abb", "z...a.c", "z...ddc"};

char s[1010][1010];

inline void solve(int&n, int x) {
	if(x == 4)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S4[i][j];
	if(x == 5)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S5[i][j];
	if(x == 6)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S6[i][j];
	if(x == 7)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S7[i][j];
	n += x;
}

int main() {
	int n = ri;
	if(n <= 2) return puts("-1"), 0;
	if(n == 3) {
		puts("a..");
		puts("a..");
		puts(".bb");
		return 0;
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			s[i][j] = '.';
	int x = n % 4, N = 1;
	if(x == 0) solve(N, 4);
	if(x == 1) solve(N, 5);
	if(x == 2) solve(N, 6);
	if(x == 3) solve(N, 7);
	while(N <= n) solve(N, 4);
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++)
			putchar(s[i][j]);
		puts("");
	}
	return 0;
}
原文
存档评论8 条抓取于