#abc298b. B - Coloring Matrix

B - Coloring Matrix

Score : 200200 points

问题描述

给定两个 NN×NN 的矩阵 AABB,其中每个元素为0或1。

Ai,jA_{i,j}Bi,jB_{i,j} 分别表示矩阵 AABB 中第 ii 行和第 jj 列的元素。

确定是否存在一种方式旋转矩阵 AA,使得对于所有满足 Ai,j=1A_{i,j} = 1 的整数对 (i,j)(i, j),都有 Bi,j=1B_{i,j} = 1

这里,旋转矩阵 AA 意味着执行零次或多次以下操作:

  • 对于所有满足 1i,jN1 \leq i, j \leq N 的整数对 (i,j)(i, j),同时将 Ai,jA_{i,j} 替换为 AN+1j,iA_{N + 1 - j,i}

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given NN-by-NN matrices AA and BB where each element is 00 or 11.
Let Ai,jA_{i,j} and Bi,jB_{i,j} denote the element at the ii-th row and jj-th column of AA and BB, respectively.
Determine whether it is possible to rotate AA so that Bi,j=1B_{i,j} = 1 for every pair of integers (i,j)(i, j) such that Ai,j=1A_{i,j} = 1.
Here, to rotate AA is to perform the following operation zero or more times:

  • for every pair of integers (i,j)(i, j) such that 1i,jN1 \leq i, j \leq N, simultaneously replace Ai,jA_{i,j} with AN+1j,iA_{N + 1 - j,i}.

Constraints

  • 1N1001 \leq N \leq 100
  • Each element of AA and BB is 00 or 11.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A1,1A_{1,1} A1,2A_{1,2} \ldots A1,NA_{1,N}

\vdots

AN,1A_{N,1} AN,2A_{N,2} \ldots AN,NA_{N,N}

B1,1B_{1,1} B1,2B_{1,2} \ldots B1,NB_{1,N}

\vdots

BN,1B_{N,1} BN,2B_{N,2} \ldots BN,NB_{N,N}

Output

If it is possible to rotate AA so that Bi,j=1B_{i,j} = 1 for every pair of integers (i,j)(i, j) such that Ai,j=1A_{i,j} = 1, print Yes; otherwise, print No.

Sample Input 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

Sample Output 1

Yes

Initially, AA is :

0 1 1
1 0 0
0 1 0

After performing the operation once, AA is :

0 1 0
1 0 1 
0 0 1

After performing the operation once again, AA is :

0 1 0
0 0 1
1 1 0

Here, Bi,j=1B_{i,j} = 1 for every pair of integers (i,j)(i, j) such that Ai,j=1A_{i,j} = 1, so you should print Yes.

Sample Input 2

2
0 0
0 0
1 1
1 1

Sample Output 2

Yes

Sample Input 3

5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0

Sample Output 3

No

update @ 2024/3/10 12:21:40