USACO 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. 
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  

=====================================================================================================

由于比较简单,就不贴中文翻译了,代码:

/*
ID: jun41821
PROG: transform
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;

char tran[10][10];
char q[10][10];
char res[10][10];

long N,i,j;

void trans(char tran[10][10])//实现将数组旋转的函数,得到一个新的数组
{
    for(i=0;i<N;i++)
     for(j=0;j<N;j++)
        {
            q[j][N-i-1]=tran[i][j];
        }
    for(i=0;i<N;i++)
     for(j=0;j<N;j++)
        {
            tran[i][j]=q[i][j];
        }
}
void swap(char tran[10][10])//实现将数组对调
{
    for(i=0;i<N;i++)
     for(j=0;j<N;j++)
     {
         q[i][N-1-j]=tran[i][j];
     }
    for(i=0;i<N;i++)
     for(j=0;j<N;j++)
        {
            tran[i][j]=q[i][j];
        }
}
bool check(char q[10][10],char p[10][10])//实现判断是否成功转换
{
    int k=1;
    for(i=0;i<N;i++)
     for(j=0;j<N;j++)
        if(q[i][j]!=p[i][j])
            k=0;
    if(k)
    return  true;
    else
    return false;
}
int main()
{
    ofstream fout ("transform.out");
    ifstream fin ("transform.in");
    int x=0;
    //程序入口
    fin>>N;                         //输入数组大小
    for(i=0;i<N;i++)                    //输入第一个矩阵
     for(j=0;j<N;j++)
     {
         fin>>tran[i][j];
     }
    for(i=0;i<N;i++)                //输入第二个矩阵
     for(j=0;j<N;j++)
     {
         fin>>res[i][j];
     }
    if(check(tran,res))             //判断是否两个矩阵一样
    {
        x=1;
    }
    //按照题目
    trans(tran);            //旋转90

    if(check(tran,res))//tran与res是否相同
    {
        fout<<1<<endl;
        return 0;
    }
    trans(tran);            //旋转180
     if(check(tran,res))//tran与res是否相同
    {
        fout<<2<<endl;
        return 0;
    }
    trans(tran);            //旋转270
     if(check(tran,res))//tran与res是否相同
    {
        fout<<3<<endl;
        return 0;
    }
    trans(tran);            //旋转360
    swap(tran);             //中间对称
     if(check(tran,res))//tran与res是否相同
    {
        fout<<4<<endl;
        return 0;
    }
    trans(tran);            //中间对称旋转90度
    if(check(tran,res))//tran与res是否相同
    {
        fout<<5<<endl;
        return 0;
    }
    trans(tran);            //中间对称旋转180度
     if(check(tran,res))//tran与res是否相同
    {
        fout<<5<<endl;
        return 0;
    }
    trans(tran);            //中间对称旋转270度
    if(check(tran,res))//tran与res是否相同
    {
        fout<<5<<endl;
        return 0;
    }
    if(x)
    {
        fout<<6<<endl;
        return 0;
    }
    fout<<7<<endl;              //不能由以上旋转获得
    return 0;
}

posted @ 2013-04-01 20:48  amourjun  阅读(239)  评论(0编辑  收藏  举报