Friday, May 27, 2016

【USACO】Transformations

Problem

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 theafter' representation.

SAMPLE OUTPUT (file transform.out)

1

Solution

    #include <iostream>
    #include <fstream>
    using namespace std;

    ifstream fin("transform.in");
    ofstream fout("transform.out");

    int main(){
        int n;
        fin >> n;
        int arr[n+1][n+1];
        int tar[n+1][n+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                char input;
                fin >> input;
                if(input == '@') arr[i][j] = 1;
                else    arr[i][j] = 0;
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                char input;
                fin >> input;
                if(input == '@') tar[i][j] = 1;
                else    tar[i][j] = 0;
            }
        }

        int count = 0;
        int ans = 99;
        //
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(arr[i][j] == tar[j][n-i+1])
                    count++;
            }
        }
        if(count == n*n && ans > 1)    ans = 1;
        count = 0;
        //
        int arr180[n+1][n+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                arr180[j][n-i+1] = arr[i][j];
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(arr180[i][j] == tar[j][n-i+1])
                    count++;
            }
        }
        if(count == n*n && ans > 2)    ans = 2;
        count = 0;
        //
        int arr270[n+1][n+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                arr270[j][n-i+1] = arr180[i][j];
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(arr270[i][j] == tar[j][n-i+1])
                    count++;
            }
        }
        if(count == n*n && ans > 3)    ans = 3;
        count = 0;
        //
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(arr[i][j] == tar[i][n-j+1]){
                    count++;
                }
            }
        }
        if(count == n*n && ans > 4)    ans = 4;
        count = 0;
        //
        int rarr[n+1][n+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                rarr[i][n-j+1] = arr[i][j];
            }
        }

        /**/
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(rarr[i][j] == tar[j][n-i+1])
                    count++;
            }
        }
        if(count == n*n && ans > 5)    ans = 5;
        count = 0;
        // /
        int rarr180[n+1][n+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                rarr180[j][n-i+1] = rarr[i][j];
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(rarr180[i][j] == tar[j][n-i+1])
                    count++;
            }
        }
        if(count == n*n && ans > 5)    ans = 5;
        count = 0;
        // /
        int rarr270[n+1][n+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                rarr270[j][n-i+1] = rarr180[i][j];
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(rarr270[i][j] == tar[j][n-i+1])
                    count++;
            }
        }
        if(count == n*n && ans > 5)    ans = 5;
        count = 0;
        //
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(arr[i][j] == tar[i][j]){
                    count++;
                }
            }
        }
        if(count == n*n && ans > 6)     ans = 6;
        count = 0;
        //
        if(ans == 99){
            fout << 7 << endl;
        }else{
            fout << ans << endl;
        }

        return 0;
    }

No comments:

Post a Comment