Transformation

USACO 1.2 - Transformations
A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another square pattern. Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations:


 * #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.


 * #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.


 * #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.


 * #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).


 * #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).


 * #6: No Change: The original pattern was not changed.


 * #7: Invalid Transformation: The new pattern was not obtained by any of the above methods.

In the case that more than one transform could have been used, choose the one with the minimum number above.

PROGRAM NAME: transform

INPUT FORMAT

Line 1: A single integer, N

Line 2..N+1: N lines of N characters (each either `@' or `-'); this is the square before transformation

Line N+2..2*N+1: N lines of N characters (each either `@' or `-'); this is the square after transformation

SAMPLE INPUT (file transform.in)

3

@-@

---

@@-

@-@

@--

--@

OUTPUT FORMAT

A single line containing the the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation.

SAMPLE OUTPUT (file transform.out)

1 Source Code code format="java" /* ID: wjh3331 LANG: JAVA TASK: transform */

import java.io.*;

public class transform { // 행렬 변환 상태 public static final int DEGREE_ROTATION_90 = 1; public static final int DEGREE_ROTATION_180 = 2; public static final int DEGREE_ROTATION_270 = 3; public static final int REFLECTION = 4; public static final int COMBINATION = 5; public static final int NO_CHANGE = 6; public static final int INVALID_TRANSFORMATION = 7;

public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("transform.in")); PrintWriter out = new PrintWriter(new FileWriter("transform.out"));

int n = Integer.parseInt(in.readLine); // 행렬의 크기

boolean[][] beforeSquare = new boolean[n][n]; // 변환전 행렬 boolean[][] afterSquare = new boolean[n][n]; // 변환후 예상 행렬 boolean[][] updateSquare = new boolean[n][n]; // 변환시 중간값 저장 boolean[][] resultSquare = new boolean[n][n]; // 변환 결과

// 변환 전 행렬 초기화 for(int i = 0; i < n; i++) { String line = in.readLine; for(int j = 0; j < n; j++) { char ch = line.charAt(j);

if(ch == '@') { beforeSquare[i][j] = true; }     }    }

// 변환후 예상 행렬 초기화 for(int i = 0; i < n; i++) { String line = in.readLine; for(int j = 0; j < n; j++) { char ch = line.charAt(j);

if(ch == '@') { afterSquare[i][j] = true; }     }    }

// '\'을 축으로 반사 reflectSlash(n, beforeSquare, updateSquare);

if(equals(n, updateSquare, afterSquare)) { out.println(COMBINATION);

out.close; System.exit(0); }

// '|'을 축으로 반사 reflectHorizontally(n, updateSquare, resultSquare);

if(equals(n, resultSquare, afterSquare)) { out.println(DEGREE_ROTATION_90);

out.close; System.exit(0); }

// '/'을 축으로 반사 reflectBackslash(n, updateSquare, resultSquare);

if(equals(n, resultSquare, afterSquare)) { out.println(DEGREE_ROTATION_180);

out.close; System.exit(0); }

// '|'을 축으로 반사 reflectHorizontally(n, resultSquare, updateSquare);

if(equals(n, updateSquare, afterSquare)) { out.println(COMBINATION);

out.close; System.exit(0); }

// '/'을 축으로 반사 reflectBackslash(n, beforeSquare, updateSquare);

if(equals(n, updateSquare, afterSquare)) { out.println(COMBINATION);

out.close; System.exit(0); }

// '|'을 축으로 반사 reflectHorizontally(n, updateSquare, resultSquare);

if(equals(n, resultSquare, afterSquare)) { out.println(DEGREE_ROTATION_270);

out.close; System.exit(0); }

// '|'을 축으로 반사 reflectHorizontally(n, beforeSquare, updateSquare);

if(equals(n, updateSquare, afterSquare)) { out.println(REFLECTION);

out.close; System.exit(0); }

// 변화가 없을 경우 if(equals(n, beforeSquare, afterSquare)) { out.println(NO_CHANGE);

out.close; System.exit(0); }

// 예상 결과와 계산 결과가 다를 경우 out.println(INVALID_TRANSFORMATION);

out.close; System.exit(0); }

// 두 행렬을 비교 public static boolean equals(int n, boolean[][] before, boolean[][] after) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(before[i][j] != after[i][j]) { return false; }     }    }    return true; }

// '|'을 축으로 반사 public static void reflectHorizontally(int n, boolean[][] before, boolean[][] after) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { after[i][j] = before[i][n-1-j]; }   }  }

// '\'을 축으로 반사 public static void reflectSlash(int n, boolean[][] before, boolean[][] after) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { after[i][j] = before[j][i]; }   }  }

// '\'을 축으로 반사 public static void reflectBackslash(int n, boolean[][] before, boolean[][] after) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { after[i][j] = before[n-1-j][n-1-i]; }   }  } } code

Comment
회원분들의 진도를 따라가려고 열심히 하고 있습니다. 언제쯤되면 그정도 수준이 될까요;; 쩝... 두 번 반사되면 원래의 결과가 나오는 성질을 이용해보았습니다. 처음 생각에는 행렬변환 연산 횟수가 적어지지 않을까 생각했는데 결과적으론 똑같은듯;; 글재주가 없어서 그림으로 대신할게요...