题目:图形对称性判断
问题描述
给定一个二维方阵,对应代表一个图形,判断其对称性。请编写五个函数checkHorSym,checkVerSym,checkDiaSym,checkAntiDiaSym,checkRotSym分别判断方阵的水平对称性、垂直对称性、对角对称性、反对角对称性、旋转对称性。函数声明如下:
int checkHorSym(char **pA, int n);
int checkVerSym(char **pA, int n);
int checkDiaSym(char **pA, int n);
int checkAntiDiaSym(char **pA, int n);
int checkRotSym(char **pA, int n);
返回值:
1表示符合该对称性,0表示不符合该对称性。
参数:
pA:输入参数,输入指向该方阵的二维指针
n:表示该二维方阵的维度为nxn维
注意:函数声明已包含在主程序中,不需要自己定义。只需要提交自定义的函数代码。
主程序如下:
#include
#include
int checkHorSym(char **pA, int n);
int checkVerSym(char **pA, int n);
int checkDiaSym(char **pA, int n);
int checkAntiDiaSym(char **pA, int n);
int checkRotSym(char **pA, int n);
int main(void)
{
int n,i,j,iHorSymFlag=1, iVerSymFlag=1, iDiaSymFlag=1, iAntiDiaSymFlag=1, iRotSymFlag=1;
char **pA,*pTmp1,*pTmp2;
scanf("%d", &n);
pA=(char**) malloc(sizeof(char*)*n);
for(i=0; i { pA[i]=(char*) malloc(sizeof(char)*n); } for(i=0; i { pTmp1=pA[i]; for(j=0;j { scanf("%c", pTmp1); while(*pTmp1==' '|| *pTmp1=='\n')//skip spacing or line break { scanf("%c", pTmp1); } pTmp1++; } } //horizental symmetry iHorSymFlag = checkHorSym(pA, n); //vertical symmetry iVerSymFlag = checkVerSym(pA, n); //diagonal symmetry iDiaSymFlag = checkDiaSym(pA, n); //antidiagonal symmetry iAntiDiaSymFlag = checkAntiDiaSym(pA, n); //rotationally symmetry iRotSymFlag = checkRotSym(pA, n); if(iHorSymFlag) printf("1 "); if(iVerSymFlag) printf("2 "); if(iDiaSymFlag) printf("3 "); if(iAntiDiaSymFlag) printf("4 "); if(iRotSymFlag) printf("5 "); printf("\n"); return 0; } 主函数输入格式 输入的第一行包含一个整数n,表示方阵维度,方阵为nxn维。 第二行开始的n行表示一个方阵,每行包含n个字符a1, a2, …, an,表示给定的方阵,相邻的字符之间用一个空格分隔。 主函数输出格式 输出一个或多个整数,表示该方阵对应图形的对称性:1代表水平对称,2代表垂直对称,3代表对角对称(即以左上到右下的线为对称轴),4代表反对角对称(即以右上到左下的线为对称轴),5代表旋转对称(即以中心位置为轴旋转180°后与原图形重合)。整数之间按照值的大小从小到大排列,以空格隔开。 样例输入 11 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 样例输出 1 2 3 4 5 样例说明 样例中以0和1两种字符给出了菱形图形,满足所有对称方式。 评测用例规模与约定 1 ≤ n ≤ 100,ai可以是0-9,或者是a-z、A-Z这些字母或数字字符。int checkHorSym(char** pA, int n)
{
for (int i = 0; i < n; i++)
{
char* left = pA[i];
char* right = pA[i] + n - 1;
while (left < right)
{
if (*left != *right)
{
return 0;
}
left++;
right--;
}
}
return 1;
}
int checkVerSym(char** pA, int n)
{
for (int i = 0; i < n / 2; i++)
{
for (int j = 0; j < n; j++)
{
if (pA[i][j] != pA[n - 1 - i][j])
{
return 0;
}
}
}
return 1;
}
int checkDiaSym(char** pA, int n)
{
for (int i = 1; i < n; i++)
{
for (int j = i - 1; j < n; j++)
{
if (pA[i][j] != pA[j][i])
{
return 0;
}
}
}
return 1;
}
int checkAntiDiaSym(char** pA, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (pA[i][j] != pA[n - 1 - j][n - 1 - i])
{
return 0;
}
}
}
return 1;
}
int checkRotSym(char** pA, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (pA[i][j] != pA[n - 1 - i][n - 1 - j])
{
return 0;
}
}
}
return 1;
}