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.
一块 N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案. 写一个程序来找出将原始图案按照以下列转换方法转换成新图案的小方式:
#1:转 90 度:图案按顺时针转 90 度.
#2:转 180 度:图案按顺时针转 180 度.
#3:转 270 度:图案按顺时针转 270 度.
#4:反射:图案在水平方向翻转(形成原图案的镜像).
#5:组合:图案在水平方向翻转,然后按照#1-#3 之一转换.
#6:不改变:原图案不改变.
#7:无效转换:无法用以上方法得到新图案.
如果有多种可用的转换方法,请选择序号小的那个.
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 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
题解:
设 a 是原始状态,b 是改变后的状态。
水平翻转:b[i,n-j+1]:=a[i,j]
右旋 90 度:b[j,n-i+1]:=a[i,j]
枚举方案就行了,或直接枚举变换。 需要注意的是,USACO 是不给用 GOTO 的。注意代码的清晰程度。
代码如下:
/*
ID: zhaoxia13
LANG: C
TASK: transform
*/
#include<stdio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
int main()
{
char squ[500][500]={'\0'},squ2[500][500]={'\0'},squf[500][500]={'\0'};
int i,j,n,flag=0;
freopen("transform.in","r",stdin);
freopen("transform.out","w",stdout);
scanf("%d",&n);
getchar();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%c",&squ[i][j]);
}
getchar();
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%c",&squ2[i][j]);
}
getchar();
}
if(flag==0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(squ[i][j]!=squ2[j][n-i+1])
{
flag=2;
break;
}
}
if(flag==2)
break;
}
if(flag==2)flag=0;
else
{
flag=1;
printf("1\n");
}
}
if(flag==0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(squ[i][j]!=squ2[n-i+1][n-j+1])
{
flag=2;
break;
}
}
if(flag==2)
break;
}
if(flag==2)flag=0;
else
{
flag=1;
printf("2\n");
}
}
if(flag==0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(squ[i][j]!=squ2[n-j+1][i])
{
flag=2;
break;
}
}
if(flag==2)
break;
}
if(flag==2)flag=0;
else
{
flag=1;
printf("3\n");
}
}
if(flag==0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
squf[i][j]=squ[i][n-j+1];
if(squf[i][j]!=squ2[i][j])
{
flag=2;
}
}
}
if(flag==2)flag=0;
else
{
flag=1;
printf("4\n");
}
}
if(flag==0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(squf[i][j]!=squ2[j][n-i+1])
{
flag=2;
break;
}
}
if(flag==2)
break;
}
if(flag==0)
{
flag=1;
printf("5\n");
}
if(flag==2)
{
flag=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(squf[i][j]!=squ2[n-i+1][n-j+1])
{
flag=2;
break;
}
}
if(flag==2)
break;
}
}
if(flag==0)
{
flag=1;
printf("5\n");
}
if(flag==2)
{
for(i=1;i<=n;i++)
{
flag=0;
for(j=1;j<=n;j++)
{
if(squf[i][j]!=squ2[n-j+1][i])
{
flag=2;
break;
}
}
if(flag==2)
break;
}
}
if(flag==0)
{
flag=1;
printf("5\n");
}else if(flag!=1)
{
flag=0;
}
}
if(flag==0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(squ[i][j]!=squ2[i][j])
{
flag=2;
break;
}
}
if(flag==2)
break;
}
if(flag==2)flag=0;
else
{
printf("6\n");
flag=1;
}
}
if(flag==0)
{
printf("7\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}