Transformations

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.

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

3
@-@
---
@@-
@-@
@--
--@

OUTPUT FORMAT

A single line containing 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

1

设计算法:

最初我是在想写个函数解决旋转90°的问题,那么旋转180°和270°只需要多次调用函数就行了,后面发现旋转90°其实就是求一个矩阵的转置矩阵,这个函数找找规律就可以写出来了,很简单。接着是写个函数解决反射问题,其实题目的反射我是没怎么看懂是什么意思,不过提到了镜面反射,自己写了个矩阵拿到镜子一照得到镜面反射结果,那么反射的函数也很简单就可以完成了,后面基本上就是一个一个去判断了,这个不难,也就那么几种情况,可以一个一个对着判断,也可以写成循环

C++编写:

写了三个代码都能够通过,其实都差不多,不过还是调了挺久的
最初代码:

/*
ID: your_id_here
TASK: transform
LANG: C++                 
*/
#include<iostream>
#include<cstdio>
using namespace std;

int N;
char before[12][12],after[12][12];

bool check(char a[12][12],char b[12][12])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(a[i][j] != b[i][j])
                return false;
        }
    }
    return true;
}

void rotate(char begin[12][12],char end[12][12])       /*旋转90°,其实这个就是求转置矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            end[j][N-1-i]=begin[i][j];

    }
}

void reflact(char begin[12][12],char end[12][12])      /*求反射矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            end[i][N-j-1]=begin[i][j];
        }
    }
}

void solve()
{
    char temp1[12][12],temp2[12][12],temp3[12][12],temp4[12][12],temp5[12][12],temp6[12][12],temp7[12][12];
    rotate(before,temp1);             /*旋转90° */
    if(check(temp1,after))
    {
        cout<<1<<endl;
        return;
    }

    rotate(temp1,temp2);     /*旋转180° */
    if(check(temp2,after))
    {
        cout<<2<<endl;
        return;
    }

    rotate(temp2,temp3);       /*旋转270° */
    if(check(temp3,after))     
    {
        cout<<3<<endl;
        return;
    }

    reflact(before,temp4);         /*反射 */ 
    if(check(temp4,after))
    {
        cout<<4<<endl;
        return;
    }

    rotate(temp4,temp5);           /*反射后旋转90° */
    rotate(temp5,temp6);           /*反射后旋转180° */
    rotate(temp6,temp7);           /*反射后旋转270° */
    if(check(temp5,after) || check(temp6,after) || check(temp7,after))
    {
        cout<<5<<endl;
        return;
    }

    if(check(before,after))
    {
        cout<<6<<endl;
        return;
    }
    cout<<7<<endl;
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);

    cin>>N;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>before[i][j];
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>after[i][j];
    }
    solve();
    return 0;
}

改后:

/*
ID: your_id_here
TASK: transform
LANG: C++                 
*/
#include<iostream>
#include<cstdio>
using namespace std;

int N;
char before[12][12],after[12][12];

bool check(char a[12][12],char b[12][12])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(a[i][j] != b[i][j])
                return false;
        }
    }
    return true;
}

void rotate(char begin[12][12],char end[12][12])       /*旋转90°,其实这个就是求转置矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            end[j][N-1-i]=begin[i][j];

    }
}

void reflact(char begin[12][12],char end[12][12])      /*求反射矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            end[i][N-j-1]=begin[i][j];
        }
    }
}

void solve()
{
    char temp[8][12][12];
    rotate(before,temp[0]);             /*旋转90° */
    rotate(temp[0],temp[1]);            /*旋转180° */
    rotate(temp[1],temp[2]);            /*旋转270° */
    reflact(before,temp[3]);            /*反射 */ 
    rotate(temp[3],temp[4]);            /*反射后旋转90° */
    rotate(temp[4],temp[5]);            /*反射后旋转180° */
    rotate(temp[5],temp[6]);            /*反射后旋转270° */
    rotate(temp[2],temp[7]);            /*不进行任何变化 */

    for(int i=0;i<8;i++)
    {
        if(check(temp[i],after))
        {
            if(i>=0 && i<=3)
            {
                cout<<i+1<<endl;
                return;
            }
            else if(i>=4 && i<=6)
            {
                cout<<5<<endl;
                return;
            }
            else
            {
                cout<<6<<endl;
                return;
            }
        }
    }
    cout<<7<<endl;
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);

    cin>>N;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>before[i][j];
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>after[i][j];
    }
    solve();
    return 0;
}

再次修改后:

/*
ID: your_id_here
TASK: transform
LANG: C++                 
*/
#include<iostream>
#include<cstdio>
using namespace std;

int N;
char before[12][12],after[12][12];

void rotate(char begin[12][12],char end[12][12])       /*旋转90°,其实这个就是求转置矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            end[j][N-1-i]=begin[i][j];

    }
}

void reflact(char begin[12][12],char end[12][12])      /*求反射矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            end[i][N-j-1]=begin[i][j];
        }
    }
}

void solve()
{
    char temp[8][12][12];
    rotate(before,temp[0]);             /*旋转90° */
    rotate(temp[0],temp[1]);            /*旋转180° */
    rotate(temp[1],temp[2]);            /*旋转270° */
    reflact(before,temp[3]);            /*反射 */ 
    rotate(temp[3],temp[4]);            /*反射后旋转90° */
    rotate(temp[4],temp[5]);            /*反射后旋转180° */
    rotate(temp[5],temp[6]);            /*反射后旋转270° */
    rotate(temp[2],temp[7]);            /*不进行任何变化 */

    for(int i=0;i<8;i++)
    {
        bool rec=true;
        int j,k;         /*必须在外面申明 */
        for(j=0;j<N;j++)
        {
            for(k=0;k<N;k++)
            {
                if(temp[i][j][k] != after[j][k])
                {
                    rec=false;
                    break;
                }
            }
            if(!rec)
                break;
        }
        if(j==N && k==N)
        {
            if(i>=0 && i<=3)
            {
                cout<<i+1<<endl;
                return;
            }
            else if(i>=4 && i<=6)
            {
                cout<<5<<endl;
                return;
            }
            else 
            {
                cout<<6<<endl;
                return;
            }
        }
    }
    cout<<7<<endl;
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);

    cin>>N;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>before[i][j];
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>after[i][j];
    }
    solve();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值