1、刚开始看到这道题的时候还以为要用到旋转公式。。。其实不用,只要找准对应坐标即可。
2、刚开始WA是因为把“镜像”和“旋转”分开来判断,看是否同时满足。其实题目的意思是“同时发生”,所以对应的坐标还是有区别的:也就是说“旋转”和“镜像”的对应坐标根本是不同的,永远不可能同时为true,“旋转&镜像”也有自己的坐标对应法则。
/*
ID:mrxy564
PROG:transform
LANG:C++
*/
#include<cstdio>
#include<cstring>
using namespace std;
bool r90,r180,r270,ref,no;
void print(){
if(r90){
if(ref)
printf("5\n");
else
printf("1\n");
return;
}
if(r180){
if(ref)
printf("5\n");
else
printf("2\n");
return;
}
if(r270){
if(ref)
printf("5\n");
else
printf("3\n");
return;
}
if(ref){
printf("4\n");
return;
}
if(no){
printf("6\n");
return;
}
printf("7\n");
return;
}
int main(){
freopen("transform.in","r",stdin);
freopen("transform.out","w",stdout);
int n;
char a[12][12],b[12][12];
while(scanf("%d",&n)==1){
getchar();
r90=r180=r270=ref=no=true;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
scanf("%c",&a[i][j]);
getchar();
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
scanf("%c",&b[i][j]);
getchar();
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(a[i][j]!=b[j][n-1-i])
r90=false;
if(a[i][j]!=b[n-1-i][n-1-j])
r180=false;
if(a[i][j]!=b[n-1-j][i])
r270=false;
if(a[i][j]!=b[i][n-1-j])
ref=false;
if(a[i][j]!=b[i][j])
no=false;
}
print();
}
return 0;
}
官方题解:
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); }