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
我的解答,代码比较多,不够简洁。
/* ID:rongkan1 LANG: C++ PROG:transform */ #include#include void rotate270(char **sour,int n,char **destin); void rotate180(char **sour,int n,char **destin); void rotate90(char **sour,int n,char **destin); void reflection(char **sour,int n, char **destin); int isequal(char **p1,char **p2,int n); int main() { FILE *fin = fopen("transform.in","r"), *fout = fopen("transform.out","w"); int n; fscanf(fin,"%d",&n); char **tmp = new char*[n]; char** des=new char*[n]; //用来计算的 char** p1=new char*[n]; //存储读入的转换前矩阵 char** p2=new char*[n]; //存储读入的转换后矩阵, char *line = new char[n+1]; for(int i=0;i
下面是官方解答:
/*
We represent a board as a data structure containing the dimension and the contents. We pass around the data structure itself, not a reference to it, so that we can return new boards, and so on. This makes it easy to define reflect and rotate operations that return reflected and rotated boards. Once we have these, we just check to see what combination of transformations makes the old board into the new board.
*/ #include#include #include #include #define MAXN 10 typedef struct Board Board; struct Board { int n; char b[MAXN][MAXN]; }; /* 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 [r, n-1 -c] */ Board reflect(Board b) { Board nb; int r, c; nb = b; for(r=0; r