题解 AT5694 【[AGC041C] Domino Quality】

· · 题解

这题妙啊

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

```plain e.. e.. .ee ``` 然后考虑更大的怎么办。 我们考虑构造多米诺数量为 $3$ 的矩阵: $n=4$: ```plain orzz oree eero zzro ``` $n=5$: ```plain orrzz oe..e ae..e a.eez oorrz ``` $n=6$: ```plain oorrzz .e.e.e .e.e.e e.e.e. e.e.e. oorrzz ``` $n=7$: ```plain .oorrzz oaee... oa.a... reea... r...eaa z...e.e z...aae ``` 打完这些表后,聪明的观众可能就会说:我会做了! 对于 $n \ge 8$ 的那些表格,我们只需要用上面四个凑就行了。将这些东西放在对角线上就可以了。 比如,对于 $n=9$,我们可以用一个 $n=4$ 和一个 $n=5$ 来凑: ```plain orzz..... oree..... eero..... zzro..... ....orrzz ....oe..e ....ae..e ....a.eez ....oorrz ``` 更大的也用类似的方法凑就行了。大概就先将 $n \bmod 4$ 算出最后凑上哪一个,然后再一直凑 $4$ 直到凑到 $n$ 就行了。 $\rm Code$: ```cpp 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; } ```