Matrix Rotation-(c)

Source: Internet
Author: User
There are four cases of clockwise rotation of the matrix rotation matrix of order n:
  • 0 degree
  • 90 degrees
  • 180 degrees
  • 270 degrees, equivalent to 90 degrees of reverse

The calculation of the algorithm IDEA matrix mainly involves the transformation between subscripts. First, we will upload a graph I analyzed on paper (PS: Big, no compression). Take AIJ as an example. I, J is counting 90 degrees of rotation from 1:

  • Change column number to row number
  • (N-row number + 1) into column number
  • Rule: A [I] [J] = B [J] [n-I + 1]

180 degree rotation:

  • (N-row number + 1) to row number
  • (N-column number + 1) is changed to column number
  • Rule: A [I] [J] = B [n-I + 1] [n-J + 1]

270 degrees of rotation (equivalent to 90 degrees of clockwise rotation ):

  • The row number changes to the column number.
  • (N-column number + 1) becomes the row number
  • Rule: A [I] [J] = B [n-J + 1] [I]

ACM questions

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
AC code (implemented in C)
# Include <stdio. h> # include <stdlib. h> # define Len 10int switchmatrix (INT (* A) [Len], INT (* B) [Len], int N); int main () {int I, j, n, angle; int A [Len] [Len], B [Len] [Len]; while (scanf ("% d", & N )! = EOF) {// receives the first Matrix for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {scanf ("% d", * (a + I) + J) ;}// receives the second array for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {scanf ("% d", * (B + I) + J );}} // matrix comparison angle = switchmatrix (A, B, n); printf ("% d \ n", angle) ;}return 0 ;}int switchmatrix (INT (*) [Len], INT (* B) [Len], int N) {int angle, I, j; For (angle = 0, I = 0; I <N; I ++) {for (j = 0; j <n; j ++) {If (angle = 0) {if (a [I] [J] = B [I] [J]) {continue;} else {angle = 90 ;}} if (angle = 90) {if (a [I] [J] = B [J] [n-I-1]) {continue;} else {angle = 180 ;}} if (angle = 180) {if (a [I] [J] = B [n-I-1] [n-J-1]) {continue ;} else {angle = 270 ;}}if (angle = 270) {if (a [I] [J] = B [n-J-1] [I]) {continue;} else {angle =-1 ;}} if (angle =-1) {break ;}} if (angle =-1) {break ;}} return angle ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.