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
题意:问原矩阵经过哪种翻转可以得到目标矩阵。
思路,这里有一点要注意的是,由于是二维坐标,我们只要找到哪个在变哪个不变即可。。。。
这边要深入理解,两个for循环,第一个for不变,第二个for再变的道理,抓住这个特征即可ac。。。
祝各位acmer ac愉快
在此贴上代码。
/**
PROG:transform
LANG:C++
ID:zpc19951
**/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s1[22][22],s2[22][22];
int main()
{
freopen("transform.in","r",stdin);
freopen("transform.out","w",stdout);
int n,i,j,ans;
scanf("%d",&n);
for(i=1;i<=n;i++) {
scanf("%s",s1[i]+1);
}
for(i=1;i<=n;i++) {
scanf("%s",s2[i]+1);
}
ans=7;
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
if(s1[i][j]!=s2[i][j]) break;
if(j<=n) break;
}
if(i>n) ans=6;
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
if(s1[j][i]!=s2[i][j]) break;
}
if(j<=n) break;
}
if(i>n) ans=5;
for(i=n;i;i--) {
for(j=1;j<=n;j++) {
if(s1[i][j]!=s2[n+1-i][j]) break;
}
if(j<=n) break;
}
if(i==0) ans=5;
for(i=n;i;i--) {
for(j=n;j;j--) {
if(s1[j][i]!=s2[n+1-i][n+1-j]) break;
}
if(j>0) break;
}
if(i==0) ans=5;
for(i=1;i<=n;i++) {
for(j=n;n;j--) {
if(s1[i][j]!=s2[i][n+1-j]) break;
}
if(j>0) break;
}
if(i>n) ans=4;
for(i=n;i;i--) {
for(j=1;j<=n;j++) {
if(s1[j][i]!=s2[n+1-i][j]) break;
}
if(j<=n) break;
}
if(i==0) ans=3;
for(i=n;i;i--) {
for(j=n;j;j--) {
if(s1[i][j]!=s2[n+1-i][n+1-j]) break;
}
if(j>0) break;
}
if(i==0) ans=2;
for(i=1;i<=n;i++) {
for(j=n;j;j--) {
if(s1[j][i]!=s2[i][n+1-j]) break;
}
if(j>0) break;
}
if(i>n) ans=1;
printf("%d\n",ans);
return 0;
}