[Java] Transformations

/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: lincans1
LANG: JAVA
TASK: transform 
*/
import java.io.*;
import java.util.*;

public class transform {
    
    

	private int N;
	
	private void swap(char[][] a, int i1, int j1, int i2, int j2) {
    
    
		char temp = a[i1][j1];
		a[i1][j1] = a[i2][j2];
		a[i2][j2] = temp;
	}

	private char[][] reflect(char[][] before) {
    
    
		for (int i = 0, n = N/2; i < N; i++) {
    
    
			for (int j = 0; j < n; j++) {
    
    
				swap(before, i, j, i, N - 1 - j);
			}
		}
		return before;
	}

	private char[][] rotate90(char[][] before) {
    
    
		for (int i = 0; i < N; i++) {
    
    
			for (int j = i + 1; j < N; j++) {
    
    
				swap(before, i, j, j, i);
			}
		}
		for (int i = 0, n = N/2; i < N; i++) {
    
    
			for (int j = 0; j < n; j++) {
    
    
				swap(before, i, j, i, N - 1 - j);
			}
		}
		return before;
	}

	private boolean isSame(char[][] a, char[][] b) {
    
    
		boolean ans = true;
		OutSide:
		for (int i = 0; i < N; i++) {
    
    
			for (int j = 0; j < N; j++) {
    
    
				if (a[i][j] != b[i][j]) {
    
    
					ans = false;
					break OutSide;
				}
			}
		}
		return ans;
	}
	
	private int count(int N, char[][] before, char[][] after) {
    
    
		// #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
		if (isSame(rotate90(before), after)) return 1;
		
		// #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
		if (isSame(rotate90(before), after)) return 2;

		// #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
		if (isSame(rotate90(before), after)) return 3;

		// #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).
		if (isSame(reflect(rotate90(before)), after)) return 4;
		
		// #5: Combination: The pattern was reflected horizontally and
		// then subjected to one of the rotations (#1-#3).
		if (isSame(rotate90(before), after) || 
			isSame(rotate90(before), after) ||
			isSame(rotate90(before), after)) {
    
    
			return 5;
		}
		// #6: No Change: The original pattern was not changed.
		if (isSame(reflect(rotate90(before)), after)) return 6;

		// #7: Invalid Transformation: The new pattern was not obtained by
		// any of the above methods.
		return 7;
	}
	
	public transform() throws IOException {
    
    
		// Use BufferedReader rather than RandomAccessFile; it's much faster
		BufferedReader f = new BufferedReader(new FileReader("transform.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("transform.out")));
		
		this.N = Integer.parseInt(f.readLine());
		// this is the square before transformation
		char[][] before = new char[N][];
		for (int i = 0; i < N; i++) {
    
    
			before[i] = f.readLine().toCharArray();
		}
		// this is the square after transformation
		char[][] after  = new char[N][];
		for (int i = 0; i < N; i++) {
    
    
			after[i] = f.readLine().toCharArray();
		}
		int ans = count(N, before, after);
		out.println(ans);
		out.close();
	}
	
	public static void main (String [] args) throws IOException {
    
    
		new transform();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41714373/article/details/111971898