USCAO-Section1.2 Transformations

来源:互联网 发布:重庆seo俱乐部 编辑:程序博客网 时间:2024/05/16 05:27

原题:
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.

题意:给定两个n*n(1<=n<=10)的矩阵,通过1-7的操作是否能使两个矩阵相同,输出序号小的操作。
1、顺时针旋转90度
2、顺时针旋转180度
3、顺时针旋转270度
4、轴对称
5、轴对称后进行一次1-3的操作
6、不做操作
7、无法通过上面的操作得到

题解:
说是有七个操作,其实只有旋转九十度和轴对称两个操作,180和270分别是两次和三次操作1.

代码:

/*ID:newyear111PROG: transformLANG: C++*/#include <iostream>#include <fstream>#include <string>#include<algorithm>using namespace std;const int N=15;char a[N][N];char b[N][N];int n; //判定两个矩阵是否相等 bool isOK(){    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;}//旋转90°的操作 void f1(){    int t[N][N];    for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            t[i][j]=a[n-j-1][i];        }    }       for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            a[i][j]=t[i][j];        }    }//  for(int i=0;i<n;i++){//      for(int j=0;j<n;j++){//          cout<<a[i][j]<<" ";//      }//      cout<<endl;//  }       }//轴对称操作 void f2(){    for(int i=0;i<n;i++){        for(int j=0;j<n/2;j++)        {            swap(a[i][j],a[i][n-j-1]);        }    }//  for(int i=0;i<n;i++){//      for(int j=0;j<n;j++){//          cout<<a[i][j]<<" ";//      }//      cout<<endl;//  }   } int main(){    ifstream fin("transform.in");    ofstream fout("transform.out");    fin>>n;    for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            fin>>a[i][j];        }    }//  for(int i=0;i<n;i++){//      for(int j=0;j<n;j++){//          fout<<a[i][j]<<" ";//      }//      fout<<endl;//  }    for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            fin>>b[i][j];        }    }//  for(int i=0;i<n;i++){//      for(int j=0;j<n;j++){//          fout<<b[i][j]<<" ";//      }//      fout<<endl;//  }    //判定是否原矩阵就相等     bool flag=false;    if(isOK()){        flag=true;    }    //判定操作1-3是否可以完成要求     for(int i=0;i<3;i++){        f1();        if(isOK()){            fout<<i+1<<endl;            return 0;        }    }    //转360°还原矩阵     f1();    //进行操作4     f2();    if(isOK()){        fout<<4<<endl;        return 0;    }    //在操作四的基础上看是否满足5的要求     for(int i=0;i<3;i++){        f1();        if(isOK()){            fout<<5<<endl;            return 0;        }    }    //根据先前的判断看是否不操作就相等     if(flag){        fout<<6<<endl;        return 0;    }    //无法得到相等的矩阵     fout<<7<<endl;    fin.close();    fout.close();    return 0;}
原创粉丝点击