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
题意:
给出两个字符串组成的图形A与B,判断1. 90°对称;2. 180°对称;3. 270°对称;4. 垂直镜面对称;5. 垂直变换后,看是否满足1,2,3中的其中之一;6. 没有变换;7. 什么都不是 满足哪一条件,若满足多项则输出最小数。
思路:
枚举所以可能发生的情况 ,然后一一对应输出就可以了,5要注意一下。
Debug and test:
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int N,i,j,sum; int way1=0,way2=0,way3=0,way4=0,way6=0,way5=0; int temp1=0,temp2=0,temp3=0,temp4=0,temp6=0; int t1=0,t2=0,t3=0; char a[15][15],b[15][15],c[15][15]; scanf("%d",&N); for(i=1;i<=N;i++) scanf("%s",a[i]); for(i=1;i<=N;i++) scanf("%s",b[i]); //这里打错为a // printf("\n"); // for(i=1;i<=N;i++) // printf("%s\n",a[i]); // printf("\n"); // for(i=1;i<=N;i++) // printf("%s\n",b[i]); sum=N*N; for(i=1;i<=N;i++) for(j=0;j<N;j++) { if(a[i][j]==b[j+1][N-i]) temp1++; if(a[i][j]==b[N+1-i][N-1-j]) temp2++; if(a[i][j]==b[N-j][i-1]) temp3++; if(a[i][j]==b[i][N-1-j]) temp4++; //-j不是I // printf("4=%d",temp4); // system("pause"); if(a[i][j]==b[i][j]) temp6++; } for(i=1;i<=N;i++) // { for(j=0;j<N;j++) // { c[i][j]=a[i][N-1-j]; // printf("%c",c[i][j]); // } // printf("\n"); // } for(i=1;i<=N;i++) for(j=0;j<N;j++) { if(c[i][j]==b[j+1][N-i]) t1++; if(c[i][j]==b[N+1-i][N-1-j]) t2++; if(c[i][j]==b[N-j][i-1]) t3++; } if(t1==sum||t2==sum||t3==sum) way5=1; // printf("temp1=%d\n",temp1); // printf("temp2=%d\n",temp2); // printf("temp3=%d\n",temp3); // printf("temp4=%d\n",temp4); // printf("temp6=%d\n",temp6); if(temp1==sum) way1=1; if(temp2==sum) way2=1; if(temp3==sum) way3=1; if(temp4==sum) way4=1; if(temp6==sum) way6=1; // printf("way1=%d\n",way1); if(way1) printf("1\n"); else if(way2) printf("2\n"); else if(way3) printf("3\n"); else if(way4) printf("4\n"); else if(way5) printf("5\n"); else if(way6) printf("6\n"); else if(!way1&&!way2&&!way3&&!way4&&!way6) printf("7\n"); }
AC:
/* TASK:transform LANG:C ID:sum-g1 */ #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { FILE *fin =fopen("transform.in","r"); FILE *fout=fopen("transform.out","w"); int N,i,j,sum; int way1=0,way2=0,way3=0,way4=0,way6=0,way5=0; int temp1=0,temp2=0,temp3=0,temp4=0,temp6=0; int t1=0,t2=0,t3=0; char a[15][15],b[15][15],c[15][15]; fscanf(fin,"%d",&N); for(i=1;i<=N;i++) fscanf(fin,"%s",a[i]); for(i=1;i<=N;i++) fscanf(fin,"%s",b[i]); sum=N*N; for(i=1;i<=N;i++) for(j=0;j<N;j++) { if(a[i][j]==b[j+1][N-i]) temp1++; if(a[i][j]==b[N+1-i][N-1-j]) temp2++; if(a[i][j]==b[N-j][i-1]) temp3++; if(a[i][j]==b[i][N-1-j]) temp4++; if(a[i][j]==b[i][j]) temp6++; } for(i=1;i<=N;i++) for(j=0;j<N;j++) c[i][j]=a[i][N-1-j]; for(i=1;i<=N;i++) //5要分开来判断 for(j=0;j<N;j++) { if(c[i][j]==b[j+1][N-i]) t1++; if(c[i][j]==b[N+1-i][N-1-j]) t2++; if(c[i][j]==b[N-j][i-1]) t3++; } if(t1==sum||t2==sum||t3==sum) way5=1; if(temp1==sum) way1=1; if(temp2==sum) way2=1; if(temp3==sum) way3=1; if(temp4==sum) way4=1; if(temp6==sum) way6=1; if(way1) fprintf(fout,"1\n"); else if(way2) fprintf(fout,"2\n"); else if(way3) fprintf(fout,"3\n"); else if(way4) fprintf(fout,"4\n"); else if(way5) fprintf(fout,"5\n"); else if(way6) fprintf(fout,"6\n"); else if(!way1&&!way2&&!way3&&!way4&&!way6) fprintf(fout,"7\n"); exit(0); }
总结:
题目不难,但是越简单就越考细心,不能浮躁。一开始因为没读懂题意是水平镜面对称还是垂直镜面对称所以错了两次,5要注意是按4的方法变换后再判断是否满足1.2.3中的其中一项。