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
思路:实现两个操作:矩阵旋转90°及水平镜像反转。依次实现90°-180°-270°-镜像-镜像+90°-镜像+180°-镜像+270°,比较,然后输出。
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class transform { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("transform.in")); FileWriter fout = new FileWriter("transform.out"); int size = Integer.parseInt(br.readLine()); char[][] source = new char[size][size]; char[][] target = new char[size][size]; for(int i =0;i<size;i++) { source[i] = br.readLine().toCharArray(); } for(int i =0;i<size;i++) { target[i] = br.readLine().toCharArray(); } fout.write(check(source,target)+"\n"); fout.flush(); fout.close(); br.close(); System.exit(0); } public static int check(char[][] s,char[][] t) { //rotate 90 t=rotate(t); if(compare(s,t)) { return 1; } //rotate 180 t=rotate(t); if(compare(s,t)) { return 2; } //rotate 270 t=rotate(t); if(compare(s,t)) { return 3; } t=rotate(t); //mirror mirror(t); if(compare(s,t)) { return 4; } //mirror and rotate 90 t=rotate(t); if(compare(s,t)) { return 5; } //mirror and rotate 180 t=rotate(t); if(compare(s,t)) { return 5; } //mirror and rotate 270 t=rotate(t); if(compare(s,t)) { return 5; } t=rotate(t); mirror(t); if(compare(s,t)) { return 6; } return 7; } private static boolean compare(char[][] s,char[][] t) { int size = s.length; for(int i = 0;i<size;i++) { for(int j = 0;j<size;j++) { if(s[i][j]!=(t[i][j])) { return false; } } } return true; } //rotate counter-clockwise 90 private static char[][] rotate(char[][] source) { int size = source.length; char[][] tmp = new char[size][size]; for(int i =0;i<size;i++) { for(int j =0;j<size;j++) { tmp[size-1-j][i] = source[i][j]; } } return tmp; } //mirror the pattern horizontally private static void mirror(char[][] source) { int size = source.length; for(int i =0;i<size;i++) { for(int j = 0;j<size/2;j++) { horiz_swap(source,i,j,size); } } } private static void horiz_swap(char[][] c,int a ,int b,int size) { char tmp = c[a][b]; c[a][b] = c[a][size-1-b]; c[a][size-1-b] = tmp; } }