TransformationsA 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
解这一题,千万要记住,顺序不能乱,我就是因为顺序随便,而WA了两次
/* ID:nealgav1 PROG:transform LANG:C++ */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 15 using namespace std; char map[N][N]; char _map[N][N]; int main() { freopen("transform.in","r",stdin); freopen("transform.out","w",stdout); int m; while(scanf("%d",&m)!=EOF) { for(int i=0;i<m;i++) scanf("%s",map[i]); for(int i=0;i<m;i++) scanf("%s",_map[i]); int i,j,k,y,x; bool flag=0; for(j=0,y=0;j<m;j++,y++)//90 { for(i=m-1,x=0;i>=0;i--,x++) if(map[i][j]!=_map[y][x]) flag=1; } if(!flag) { printf("%d\n",1); continue; } flag=0; for(i=m-1,y=0;i>=0;i--,y++)//180 { for(j=m-1,x=0;j>=0;j--,x++) if(map[i][j]!=_map[y][x]) flag=1; } if(!flag) { printf("%d\n",2); continue; } flag=0; for(j=m-1,y=0;j>=0;j--,y++)//270度 { for(i=0,x=0;i<m;i++,x++)//y { if(map[i][j]!=_map[y][x]) flag=1; } } if(!flag) { printf("%d\n",3); continue; } flag=0; for(i=0,y=0;i<m;i++,y++)//取反 { for(j=m-1,x=0;j>=0;j--,x++) if(map[i][j]!=_map[y][x]) flag=1; } if(!flag) { printf("%d\n",4); continue; } flag=0; //取反后的后继操作 for(j=m-1,y=0;j>=0;j--,y++)//90 { for(i=m-1,x=0;i>=0;i--,x++) if(map[i][j]!=_map[y][x]) flag=1; } if(!flag) { printf("%d\n",5); continue; } flag=0; for(i=m-1,y=0;i>=0;i--,y++)//180 { for(j=0,x=0;j<m;j++,x++) if(map[i][j]!=_map[y][x]) flag=1; } if(!flag) { printf("%d\n",5); continue; } flag=0; for(j=0,y=0;j<m;j++,y++)//270 { for(i=0,x=0;i<m;i++,x++) if(map[i][j]!=_map[y][x]) flag=1; } if(!flag) { printf("%d\n",5); continue; } flag=0; for(i=0,y=0;i<m;i++,y++)//判断是否一样 { for(j=0,x=0;j<m;j++,x++) { if(map[i][j]!=_map[y][x]) flag=1; } } if(!flag) { printf("%d\n",6); continue; } printf("%d\n",7); } return 0; }
USER: Neal Gavin Gavin [nealgav1] TASK: transform LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3048 KB] Test 2: TEST OK [0.000 secs, 3048 KB] Test 3: TEST OK [0.000 secs, 3048 KB] Test 4: TEST OK [0.000 secs, 3048 KB] Test 5: TEST OK [0.000 secs, 3048 KB] Test 6: TEST OK [0.000 secs, 3048 KB] Test 7: TEST OK [0.000 secs, 3048 KB] Test 8: TEST OK [0.000 secs, 3048 KB] All tests OK.Your program ('transform') produced all correct answers! This is your submission #3 for this problem. Congratulations!
Here are the test data inputs:
------- test 1 ---- 3 --- --- --- --- -@- --- ------- test 2 ---- 5 -@@@- -@@-- -@--- ----- ----- ----- ----@ ---@@ --@@@ ----- ------- test 3 ---- 5 @@@@@ @---@ @@@@@ @@@@@ @@@@@ @@@@@ @@@@@ @@@@@ @---@ @@@@@ ------- test 4 ---- 6 -@-@-@ @-@-@- -@-@-@ @-@-@- -@-@-@ @-@-@- @-@-@- -@-@-@ @-@-@- -@-@-@ @-@-@- -@-@-@ ------- test 5 ---- 3 @@@ --- @@@ @@@ --- @@@ ------- test 6 ---- 4 @@@@ @@@@ -@@@ @@@@ @@@@ @@@@ @@@- @@@@ ------- test 7 ---- 4 @-@@ @@@@ @@@@ @@@@ @@@@ @@@@ @@@@ @-@@ ------- test 8 ---- 10 @--------@ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- @--------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------@Keep up the good work! Transformations
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 <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]; }; /* 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); }