Question 1164: rotation matrix, 1164: rotation matrix
-
Description:
-
Any input of two matrices lower than 9 must determine whether the second is the first rotation matrix. If yes, the output rotation angle (0, 90, 180, 270). If not, output-1.
You must first enter the matrix order, and then enter two matrices. Two numbers in each row can be separated by any space. Rows are separated by carriage return, and two matrices are separated by any carriage return.
-
Input:
-
Multiple groups of data are input.
Enter n (1 <= n <= 9) in the first row of each group of data, and enter two n-level matrices from the second row.
-
Output:
-
Determine whether the second is the first rotation matrix. If yes, the output rotation angle (0, 90, 180, 270). If not, the output is-1.
If multiple rotation angles are returned, the smallest one is output.
-
Sample input:
-
31 2 34 5 67 8 97 4 18 5 29 6 3
-
Sample output:
-
90
-
C ++ code:
#include<iostream>using namespace std; bool f0();bool f180();bool f90();bool f270(); int a[9][9],b[9][9]; int i,j; int n;int main(){ while(cin>>n) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; }//for }//for for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>b[i][j]; }//for }//for if(f0()) cout<<0<<endl; else if(f90()) cout<<90<<endl; else if(f180()) cout<<180<<endl; else if(f270()) cout<<270<<endl; else cout<<-1<<endl; }//while return 1;}//main bool f0() { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]!=b[i][j]) return false; } } return true; }//f0 bool f90() { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]!=b[j][n-i-1]) return false; } } return true; }//f90 bool f180() { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]!=b[n-i-1][n-j-1]) return false; } } return true; }//f180 bool f270() { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]!=b[n-j-1][i]) return false; } } return true; }/************************************************************** Problem: 1164 User: Carvin Language: C++ Result: Accepted Time:0 ms Memory:1520 kb****************************************************************/
-
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.