Problem D: 矩阵相似
Time Limit: 1 Sec Memory Limit: 128 MB
Description
给出2个大小相同的矩阵方阵A,B,方阵中的元素为0或1。若A和B相似,满足下面条件:
(1)A=B 相等,则输出0
(2)A经过顺时针旋转90°成为B,则输出1
(3)A经过顺时针旋转180°成为B,则输出2
(4)A经过顺时针旋转270°成为B,则输出3
若A,B不相似,则输出-1;
例如
则输出1,因为A顺时针旋转90°成为B。
Input
第一行为一个整数n(1≤n≤20),
接下来的n行表示A矩阵的01方阵;
再接下来的n行表示B矩阵的01方阵。
Output
一个整数(0,或1,或2, 或3,或-1)即A,B相似的结果。
Sample Input
4
0 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0
Sample Output
1
HINT
题目如上,一开始觉得很难,做不出来,想用2个矩阵做,结果半天无果。最后利用一定的规律想出来了。
最后还是上网查了一下,借助大佬的思想,做了出来。废话不多说,上代码。
#include <cstdio>
using namespace std;
int n,a,b,c,d;
int AB[21][21];
int main()
{
scanf("%d",&n);
for (int i=1;i<=2*n;i++)
for (int j=1;j<=n;j++)
scanf("%d",&AB[i][j]);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
if (AB[i][j]==AB[n+i][j]) a++;
if (AB[i][j]==AB[n+j][n-i+1]) b++;
if (AB[i][j]==AB[2*n-i+1][n-j+1]) c++;
if (AB[i][j]==AB[2*n-j+1][i]) d++;
} //这里纯公式,后期上推理过程
if (a==n*n) printf("0");
else if (b==n*n) printf("1");
else if (c==n*n) printf("2");
else if (d==n*n) printf("3");
else printf("-1");
return 0;
}