USACO训练场 1.3Transformations
每天一道题+个人总结打卡
此博客仅用于记录学习
题目:
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
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.
大致思路:
主要通过枚举+模拟各种情况来解决。
首先注意答案可能有很多种,可能两种方法都能得到Transform之后的Square,所以枚举各种情况的时候要按顺序。然后可以通过创建两个新的数组c和d来保存当前的矩阵状态。
主要写四个函数:主函数,Rotate函数,Reflection函数和Comp函数(每Transform一次都要调用Cmp函数来和目标Square进行对比)
第1到3种情况都是在上一个基础上旋转90度,那么只需要写一个Rotate函数,反复调用就行。第4种情况要在原始Square的基础上进行轴对称变换,那么可以先再调用一下Rotate函数,旋转回原始状态(旋转360度),然后再写一个Reflection函数进行调用。第5种情况的话,因为在枚举第4种情况的时候已经调用过Reflection函数了,所以可以再循环调用Rotate函数三次,枚举一遍1-3的情况。第6种情况的话,可以先调用Rotate函数,把当前的Square旋转回经过轴对称变换的原先状态,然后再调用Reflection,轴对称变换回最原始的状态。
注意:每transform一次,无论调用的是Rotation函数还是Reflection函数,都要用d数组存储以下经过刚刚transform过后的c数组的状态,确保下一次Rotate的时候是基于最新状态来进行Rotate的。
代码:
#include <bits/stdc++.h>
#include <fstream>
#include <string>
using namespace std;
int N;
char a[11][11];
char b[11][11];
char c[11][11];
char d[11][11];
void Rotate()
{
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
c[j][N+1-i]=d[i][j];
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
d[i][j]=c[i][j];
}
void Reflection()
{
char cnt;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N/2;j++)
{
cnt=c[i][j];
c[i][j]=c[i][N+1-j];
c[i][N+1-j]=cnt;
}
}
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
d[i][j]=c[i][j];
}
int Cmp()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
if(c[i][j]!=b[i][j])
return 1;
}
}
return 0;
}
int main()
{
ofstream fout ("transform.out");
ifstream fin ("transform.in");
fin>>N;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
fin>>a[i][j];
d[i][j]=a[i][j];
c[i][j]=a[i][j];
}
}
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
fin>>b[i][j];
int cnt=1;
while(cnt<=3)
{
Rotate();
if(Cmp()==0)
{
fout<<cnt<<endl;
return 0;
}
cnt+=1;
}
Rotate();
Reflection();
if(Cmp()==0)
{
fout<<4<<endl;
return 0;
}
cnt=1;
while(cnt<=3)
{
Rotate();
if(Cmp()==0)
{
fout<<5<<endl;
return 0;
}
cnt+=1;
}
Rotate();
Reflection();
if(Cmp()==0)
{
fout<<6<<endl;
return 0;
}
fout<<7<<endl;
}