close

 

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

line-16.gif 

/*
    ID: sHinYU
    PROB: transform
    LANG: C
*/


#include <stdio.h>
#include <string.h>


int beg[10][10], mir[10][10], end[10][10], N;


inline int min(int a, int b)
{
    return (a < b ? a : b);
}


void INPUT(int a[][10])
{
    int i, j;
    for(i = 0; i < N; i++)
        for(j = 0; j < N; )
        {
            char ch;
            scanf("%c", &ch);
            if(ch == '-')
            {
                a[i][j] = 0;
                j++;
            }
            else if(ch == '@')
            {
                a[i][j] = 1;
                j++;
            }
        }
}  


int check(int a[][10], int b[][10])
{
    int i, j;
    for(i = 0; i < N; i++)
        for(j = 0; j < N; j++)
            if(a[i][j] != b[i][j])
                return 0;
    return 1;
}


void turn(int x[][10])
{
    int i, j, temp[10][10];
    for(i = 0; i < N; i++)
        for(j = 0; j < N; j++)
            temp[j][N-i-1] = x[i][j];


    for(i = 0; i < N; i++)
        for(j = 0; j < N; j++)
            x[i][j] = temp[i][j];
}


void mirror()
{
    int i, j;
    for(i = 0; i < N; i++)
        for(j = 0; j < N; j++)
            mir[i][N-j-1] = beg[i][j];
}


int main()
{
    memset(beg, 0, sizeof(beg));
    memset(mir, 0, sizeof(mir));
    memset(end, 0, sizeof(end));
    
    freopen("transform.in", "r", stdin);
    scanf("%d", &N);
    INPUT(beg);
    INPUT(end);  
    fclose(stdin);
    
    freopen("transform.out", "w", stdout);
    int count = 0, ans = 7;
    mirror();
    
    if(check(beg, end))
        ans = min(ans, 6);
    
    if(check(mir, end))
        ans = min(ans, 4);
    do
    {
        turn(mir);
        turn(beg);
        count++;
        if(check(beg, end))
        {
            ans = min(ans, count);
            break;
        }
        if(check(mir, end))
        {
            ans = min(ans, 5);
            break;
        }
    }while(count != 3);
    
    printf("%d\n", ans);
    fclose(stdout);
    
    return 0;
}
arrow
arrow
    全站熱搜

    sHihYU 發表在 痞客邦 留言(1) 人氣()