1. 被这道题虐了无数次,每次编译通过,但是运行会错误(当时用的是string数组), 后来把string数组改成char[][]二维数组就好了;
2. 给的参考代码是不对的
3. 如果函数的返回类型是数组,只能通过指针来实现,但是参考代码给了一种巧妙的方法,把数组封装成结构体,然后返回结构体
以下是我的代码:
/* ID: dollar4 PROG: transform LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <cstring> using namespace std; typedef char array[10][10]; char ori[10][10], tasf[10][10]; char rst[10][10]; int n; int rot90(char arr[][10], array &cag) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cag[j][n-1-i] = arr[i][j]; } return 1; } int rot180(char arr[][10], array &cag) { char cag1[10][10]; rot90(arr, cag1); rot90(cag1, cag); return 1; } int rot270(char arr[][10], array &cag) { char cag1[10][10]; rot180(arr, cag1); rot90(arr, cag); return 1; } int reflect(char arr[][10], array &cag) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cag[i][n-j-1] = arr[i][j]; return 1; } int grp90(char arr[][10], array &cag) { char cag1[10][10]; reflect(arr, cag1); rot90(cag1, cag); return 1; } int grp2(char arr[][10], array &cag) { char cag1[10][10]; reflect(arr, cag1); rot180(cag1, cag); return 1; } int grp3(char arr[][10], array &cag) { char cag1[10][10]; reflect(arr, cag1); rot270(cag1, cag); return 1; } bool equ(char arr1[][10], char arr2[][10]) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (arr1[i][j] != arr2[i][j]) return false; else continue; return true; } int main() { ofstream fout ("transform.out"); ifstream fin ("transform.in"); int i, j; fin >> n; for (i = 0; i < n; i++) for (j = 0; j < n; j++) fin >> ori[i][j]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) fin >> tasf[i][j]; if (rot90(ori, rst) && equ(rst, tasf)) fout << 1 << endl; else if (rot180(ori, rst) && equ(rst, tasf)) fout << 2 << endl; else if (rot270(ori, rst) && equ(rst, tasf)) fout << 3 << endl; else if (reflect(ori, rst) && equ(rst, tasf)) fout << 4 << endl; else if (grp90(ori, rst) && equ(rst, tasf)) fout << 5 << endl; else if (grp2(ori, rst) && equ(rst, tasf)) fout << 5 << endl; else if (grp3(ori, rst) && equ(rst, tasf)) fout << 5 << endl; else if (equ(ori, tasf)) fout << 6 << endl; else fout << 7 << endl; return 0; }
以下是参考代码:
#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); }