1. 程式人生 > >USACO 1.3.2_transform

USACO 1.3.2_transform

/*
ID: cjn77881
LANG: C++
TASK: transform
*/

#include <stdio.h>
#include <string.h>

typedef struct matrix matrix; //為了使得函式可以返回二維陣列,特定義一個結構體 
struct matrix{
	char ch[10][10];
};

char ch_tmp;
matrix a,b,stdrd;
int N,ans;

bool check(matrix a,matrix b){
	for (int i=0;i<N;i++)
		for (int j=0;j<
N;j++) if (a.ch[i][j]!=b.ch[i][j]) return false; return true; } matrix rotate(matrix mat){ matrix tmp; //如果不是定義的結構體,想要返回一個數組時,須使用static int array[x] 方式,申請靜態記憶體空間,否則在函式結束時,會釋放記憶體,無法得到想要的結果。 memset(tmp.ch,sizeof(tmp.ch),0); //初始化結構體中的陣列元素,第一次用感覺有點彆扭。 for (int i=0;i<N;i++) for (int j=0;j<
N;j++) tmp.ch[j][N-1-i] = mat.ch[i][j]; //操作結構體重的陣列元素 return tmp; } matrix reflect(matrix mat){ matrix tmp; memset(tmp.ch,sizeof(tmp.ch),0); for (int i=0;i<N;i++) for (int j=0;j<N;j++) tmp.ch[i][N-1-j] = mat.ch[i][j]; return tmp; } void transform(int x){ if (x == 1) { memset(b.
ch,sizeof(b.ch),0); b = rotate(a); if (check(stdrd,b)) ans=1; /* debug所用程式碼 printf("here is mode %d\n",x); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",b.ch[i][j]); printf("\n"); } */ } if (x == 2){ memset(b.ch,sizeof(b.ch),0); b = rotate(a); b = rotate(b); if (check(stdrd,b)) ans=2; /* printf("here is mode %d\n",x); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",b.ch[i][j]); printf("\n"); } */ } if (x == 3){ memset(b.ch,sizeof(b.ch),0); b = rotate(a); b = rotate(b); b = rotate(b); if (check(stdrd,b)) ans=3; /* printf("here is mode %d\n",x); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",b.ch[i][j]); printf("\n"); } */ } if (x == 4){ memset(b.ch,sizeof(b.ch),0); b=reflect(a); if (check(stdrd,b)) ans=4; /* printf("here is mode %d\n",x); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",b.ch[i][j]); printf("\n"); } */ } if (x == 5){ memset(b.ch,sizeof(b.ch),0); b = reflect(a); b = rotate(b); if (check(stdrd,b)) ans=5; /* printf("here is mode %d. First try\n",x); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",b.ch[i][j]); printf("\n"); } */ b = rotate(b); if (check(stdrd,b)) ans=5; /* printf("here is mode %d. Second try\n",x); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",b.ch[i][j]); printf("\n"); } */ b = rotate(b); if (check(stdrd,b)) ans=5; /* printf("here is mode %d. Third try\n",x); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",b.ch[i][j]); printf("\n"); } */ } if (x == 6){ memset(b.ch,sizeof(b.ch),0); b = a; if (check(stdrd,b)) ans=6; } return; } int main(){ FILE *fin = fopen("transform.in","r"); FILE *fout = fopen("transform.out","w"); memset(a.ch,sizeof(a.ch),0); memset(b.ch,sizeof(b.ch),0); fscanf(fin,"%d\n",&N); for (int i=0;i<N;i++){ for (int j=0;j<N;j++) fscanf(fin,"%c",&a.ch[i][j]); fscanf(fin,"\n"); } for (int i=0;i<N;i++){ for (int j=0;j<N;j++) fscanf(fin,"%c",&stdrd.ch[i][j]); fscanf(fin,"\n"); } /* printf("here is mode %d\n",0); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",stdrd.ch[i][j]); printf("\n"); } printf("here is mode %d,Original Data\n",0); for(int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%c",a.ch[i][j]); printf("\n"); } */ ans = 7; for (int i=1;i<7;i++) if (ans == 7) transform(i); fprintf(fout,"%d\n",ans); //printf("%d\n",ans); return 0; }