/* ID:tianlin2 PROG:transform LANG:C++ */ #include <iostream> #include <fstream> using namespace std; //相等 bool six(char a[][11],char c[][11],int h) { int i,j; for(i=0;i!=h;++i) for(j=0;j!=h;++j) { if(a[i][j]!=c[i][j]) return false; } return true; } //90° bool one(char a[][11],char c[][11],int h) { char b[11][11]; int n=0,i,j,x,y; while(n<h/2&&h%2==0 || n<=h/2&&h%2!=0) { i=n; j=n; while(j!=h-n) { y=h-n-1; x=j; b[x][y]=a[i][j]; ++j; } ++i; --j; while(i!=h-n) { x=h-n-1; y=h-1-i; b[x][y]=a[i][j]; ++i; } --i; while(j>=n) { y=n; x=j; b[x][y]=a[i][j]; --j; } ++j; --i; while(i>n) { x=n; y=h-1-i; b[x][y]=a[i][j]; --i; } ++n; } if(six(b,c,h)) return true; else return false; } //180° bool two(char a[][11],char c[][11],int h) { char b[11][11]; int n=0,i,j,x,y; while(n<h/2&&h%2==0 || n<=h/2&&h%2!=0) { i=n; j=n; while(j!=h-n) { x=h-n-1; y=h-1-j; b[x][y]=a[i][j]; ++j; } ++i; --j; while(i!=h-n) { x=h-1-i; y=n; b[x][y]=a[i][j]; ++i; } --i; while(j>=n) { x=n; y=h-1-j; b[x][y]=a[i][j]; --j; } ++j; --i; while(i>n) { x=h-1-i; y=h-n-1; b[x][y]=a[i][j]; --i; } ++n; } if(six(b,c,h)) return true; else return false; } //270° bool three(char a[][11],char c[][11],int h) { char b[11][11]; int n=0,i,j,x,y; while(n<h/2&&h%2==0 || n<=h/2&&h%2!=0) { i=n; j=n; while(j!=h-n) { x=h-1-j; y=n; b[x][y]=a[i][j]; ++j; } --j; ++i; while(i!=h-n) { x=n; y=i; b[x][y]=a[i][j]; ++i; } --i; while(j>=n) { x=h-1-j; y=h-n-1; b[x][y]=a[i][j]; --j; } ++j; --i; while(i>n) { x=h-1-n; y=i; b[x][y]=a[i][j]; --i; } ++n; } if(six(b,c,h)) return true; else return false; } //水平映射 bool four(char a[][11],char c[][11],int h) { char b[11][11]; int n=0,i,j,x,y; while(n<h/2&&h%2==0 || n<=h/2&&h%2!=0) { i=n; j=n; while(j!=h-n) { x=i; y=h-1-j; b[x][y]=a[i][j]; ++j; } ++i;--j; while(i!=h-n) { x=i; y=h-1-j; b[x][y]=a[i][j]; ++i; } --i; while(j>=n) { x=i; y=h-1-j; b[x][y]=a[i][j]; --j; } ++j;--i; while(i>n) { x=i; y=h-1-j; b[x][y]=a[i][j]; --i; } ++n; } if(six(b,c,h)) return true; else return false; } //混合 bool five(char a[][11],char c[][11],int h) { char b[11][11]; int n=0,i,j,x,y; while(n<h/2&&h%2==0 || n<=h/2&&h%2!=0) { i=n; j=n; while(j!=h-n) { x=i; y=h-1-j; b[x][y]=a[i][j]; ++j; } ++i;--j; while(i!=h-n) { x=i; y=h-1-j; b[x][y]=a[i][j]; ++i; } --i; while(j>=n) { x=i; y=h-1-j; b[x][y]=a[i][j]; --j; } ++j;--i; while(i>n) { x=i; y=h-1-j; b[x][y]=a[i][j]; --i; } ++n; } /*for(i=0;i!=h;++i) { for(j=0;j!=h;++j) { cout<<b[i][j]<<' '; } cout<<endl; }*/ if(one(b,c,h)) return true; else if(two(b,c,h)) return true; else if(three(b,c,h)) return true; else return false; } int main() { ofstream fout("transform.out"); ifstream fin("transform.in"); char a[11][11],b[11][11]; int n=0,i,j,x,y,h,c=0; fin>>h; for(i=0;i!=h;++i) for(j=0;j!=h;++j) fin>>a[i][j]; for(i=0;i!=h;++i) for(j=0;j!=h;++j) fin>>b[i][j]; if(one(a,b,h)) c=1; else if(two(a,b,h)) c=2; else if(three(a,b,h)) c=3; else if(four(a,b,h)) { c=4; } else if(five(a,b,h)) { c=5; } else if(six(a,b,h)) c=6; else c=7; fout<<c<<endl; return 0; }
主要利用分情况:1,2,3,4,5,6!每种情况又分矩阵的上下左右四种情况,很复杂!
其实像官方答案一样,上下左右的规律,都是一样的:
for(r=0; r<b.n; r++) for(c=0; c<b.n; c++) nb.b[c][b.n+1 - r] = b.b[r][c];
还有一点忽略了,180°就是90°再转90°,270°则是转三次90°,导致写多了两个函数!
还有没用到结构体!
官方答案:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define MAXN 10 typedef struct Board Board; struct Board { int n; char b[MAXN][MAXN]; }; //90°的这个规律很好 /* rotate 90 degree clockwise: [r, c] -> [c, n+1 - r] */ Board rotate(Board b) { Board nb; int r, c; nb = b; for(r=0; r<b.n; r++) for(c=0; c<b.n; c++) nb.b[c][b.n+1 - r] = b.b[r][c]; return nb; } /* reflect board horizontally: [r, c] -> [r, n-1 -c] */ Board reflect(Board b) { Board nb; int r, c; nb = b; for(r=0; r<b.n; r++) for(c=0; c<b.n; c++) nb.b[r][b.n-1 - c] = b.b[r][c]; return nb; } /* return non-zero if and only if boards are equal */ int eqboard(Board b, Board bb) { int r, c; if(b.n != bb.n) return 0; for(r=0; r<b.n; r++) for(c=0; c<b.n; c++) if(b.b[r][c] != bb.b[r][c]) return 0; return 1; } Board rdboard(FILE *fin, int n) { Board b; int r, c; b.n = n; for(r=0; r<n; r++) { for(c=0; c<n; c++) b.b[r][c] = getc(fin); assert(getc(fin) == '/n'); } return b; } void main(void) { FILE *fin, *fout; Board b, nb; int n, change; fin = fopen("transform.in", "r"); fout = fopen("transform.out", "w"); assert(fin != NULL && fout != NULL); fscanf(fin, "%d/n", &n); b = rdboard(fin, n); nb = rdboard(fin, n); //注意按照时针旋转的三种情况 if(eqboard(nb, rotate(b))) change = 1; else if(eqboard(nb, rotate(rotate(b)))) change = 2; else if(eqboard(nb, rotate(rotate(rotate(b))))) change = 3; else if(eqboard(nb, reflect(b))) change = 4; else if(eqboard(nb, rotate(reflect(b))) || eqboard(nb, rotate(rotate(reflect(b)))) || eqboard(nb, rotate(rotate(rotate(reflect(b)))))) change = 5; else if(eqboard(nb, b)) change = 6; else change = 7; fprintf(fout, "%d/n", change); exit(0); }