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:
In the case that more than one transform could have been used, choose the one with the minimum number above.
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 |
3 @-@ --- @@- @-@ @-- --@
1
下边是代码:
/* ID:lk951201 PROB:transform LANG:C++ */ #include<iostream> #include<cstdio> #include<cstring> #define MAXN 105 #define pf printf using namespace std; int N; char bef[MAXN][MAXN]; //bef数组用于存放最开始的图形 char mid[MAXN][MAXN]; //mid数组用于存放旋转的图形 char aft[MAXN][MAXN]; //aft数组存放最后的图形 bool is_equal() //判断图形是不是相同 { bool ok=true; for(int i=0;i<N;i++) { if(strcmp(bef[i],aft[i]) ) {ok=false;break;} //采用每一行比较的方式 } return ok; } void copytobef() //因为把顺时针旋转过的图形放在了mid数组中,在复制回来,以方便上边的判断图形是否相同 { for(int i=0;i<N;i++) { strcpy(bef[i],mid[i]); //和上边一样,一行一行的复制 } } void trans_x() //对图形进行X镜像翻转 { for(int i=0;i<N;i++) { int n=N-1; for(int j=0;j<=n;j++) { char t=bef[i][j]; bef[i][j]=bef[i][n]; bef[i][n]=t; n--; } } } void clockwize() //对图形进行顺时针旋转,把旋转过的放到mid输注中 { int n=N-1; for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { mid[j][n]=bef[i][j]; } n--; } } int main() { freopen("transform.in","r",stdin); freopen("transform.out","w",stdout); memset(bef,0,sizeof(bef)); //数组进行清空 memset(mid,0,sizeof(mid)); memset(aft,0,sizeof(aft)); scanf("%d",&N); for(int i=0;i<N;i++) scanf("%s",bef[i]); for(int i=0;i<N;i++) scanf("%s",aft[i]); clockwize();copytobef(); //第一次进行顺时针旋转,旋转过后在mid数组中,因为判断是在bef中,所以在复制到bef中 if(is_equal()) {pf("1\n");return 0;} //如果图形一样,输出1,并结束 clockwize();copytobef(); //第二次旋转 if(is_equal()) {pf("2\n");return 0;} clockwize();copytobef(); //第三次旋转 if(is_equal()) {pf("3\n");return 0;} clockwize();copytobef();trans_x(); //旋转四次,就恢复成原图形,这时候再进行X镜像旋转 if(is_equal()) {pf("4\n");return 0;} //如果图形相同,~ clockwize();copytobef(); //因为已经X镜像旋转过,所以依次旋转1次,判断 if(is_equal()) {pf("5\n");return 0;} clockwize();copytobef(); //两次 if(is_equal()) {pf("5\n");return 0;} clockwize();copytobef(); //三次 if(is_equal()) {pf("5\n");return 0;} clockwize();copytobef(); //如果x旋转过后,在旋转1,2,3次后还不满足,再旋转一次,成最初的图形 if(is_equal()) {pf("6\n");return 0;} //判断,为什么不开始就判断呢?因为如果都满足,先输出小的 printf("7\n");return 0; // 都不满足,只能输出7了 }