以下代码已经测试
#include<iostream>
#include<stdlib.h>
using namespace std;
class Matrix
{
public:
char ** x;
short width;
Matrix(){x=0;
};
void Init(short w)
{
x = new char*[w];
for(short i=0;i<w;i++)
x[i]=new char[w];
width=w;
}
friend bool operator==(Matrix mx1,Matrix mx2);
friend bool operator!=(Matrix mx1,Matrix mx2);
};
bool operator==(Matrix mx1,Matrix mx2)
{
short n;n=mx1.width;
if(n!=mx2.width) return false;
short row,col;
for(row=0;row<n;row++)
{
for(col=0;col<n;col++)
if(mx1.x[row][col]!=mx2.x[row][col])
return false;
}
return true;
}
bool operator!=(Matrix mx1,Matrix mx2){return !(mx1==mx2);}
Matrix Roll90(Matrix mx)/*旋转90度*/
{
short width;width=mx.width;
Matrix temp;temp.Init(width);
for(short row=0;row<width;row++)
{
for(short col=0;col<width;col++)
temp.x[row][col]=mx.x[width-1-col][row];
}
return temp;
}
Matrix Roll180(Matrix mx)/*旋转180度*/
{
short width;width=mx.width;
Matrix temp;temp.Init(width);
for(short row=0;row<width;row++)
{
for(short col=0;col<width;col++)
temp.x[row][col]=mx.x[width-1-row][width-1-col];
}
return temp;
}
Matrix Roll270(Matrix mx)/*旋转270度*/
{
short width;width=mx.width;
Matrix temp;temp.Init(width);
for(short row=0;row<width;row++)
{
for(short col=0;col<width;col++)
temp.x[row][col]=mx.x[col][width-1-row];
}
return temp;
}
bool Split1(const Matrix mx,Matrix &submx)/*分裂出左上方的小矩阵*/
{
short n;
n=mx.width;
if(n%2==1) return false;
submx.Init(n/2);
short row,col;
for(row=0;row<submx.width;row++)
{
for(col=0;col<submx.width;col++)
{
submx.x[row][col]=mx.x[row][col];
}
}
return true;
}
bool Split2(const Matrix mx,Matrix &submx)/*分裂出右上方的小矩阵*/
{
short n;
n=mx.width;
if(n%2==1) return false;
submx.Init(n/2);
short row,col;
for(row=0;row<submx.width;row++)
{
for(col=0;col<submx.width;col++)
{
submx.x[row][col]=mx.x[row][col+submx.width];
}
}
return true;
}
bool Split3(const Matrix mx,Matrix &submx)/*分裂出左下方的小矩阵*/
{
short n;
n=mx.width;
if(n%2==1) return false;
submx.Init(n/2);
short row,col;
for(row=0;row<submx.width;row++)
{
for(col=0;col<submx.width;col++)
{
submx.x[row][col]=mx.x[row+submx.width][col];
}
}
return true;
}
bool Split4(const Matrix mx,Matrix &submx)/*分裂出右下方的小矩阵*/
{
short n;
n=mx.width;
if(n%2==1) return false;
submx.Init(n/2);
short row,col;
for(row=0;row<submx.width;row++)
{
for(col=0;col<submx.width;col++)
{
submx.x[row][col]=mx.x[row+submx.width][col+submx.width];
}
}
return true;
}
bool FillValue(Matrix &mx,char *str)//用字符串为矩阵赋值
{
short size;
size=mx.width*mx.width;
if(size!=strlen(str)) return false;
short row,col;
for(row=0;row<mx.width;row++)
for(col=0;col<mx.width;col++)
mx.x[row][col]=str[row*mx.width+col];
return true;
}
bool Compare(const Matrix mxSource,const Matrix mxTarget)
{
if(mxSource.width!=mxTarget.width) return false;
Matrix mxRolled;
if(mxSource.width%2!=0)//不可再分割的情况
{
mxRolled=mxSource;//旋转0度
if(mxRolled==mxTarget) return true;
mxRolled=Roll90(mxSource);//旋转90度的情况
if(mxRolled==mxTarget) return true;
mxRolled=Roll180(mxSource);//旋转180度的情况
if(mxRolled==mxTarget) return true;
mxRolled=Roll270(mxSource);//旋转270度的情况
if(mxRolled==mxTarget) return true;
return false;//四种状况均不与目标方阵吻合
}
bool OK1=true,OK2=true,OK3=true,OK4=true;//用布尔变量记录四种转角递归后是否与目标区域一致
Matrix submx1,submx2,submx3,submx4;
Matrix subTg1,subTg2,subTg3,subTg4;
Split1(mxTarget,subTg1);Split2(mxTarget,subTg2);Split3(mxTarget,subTg3);Split4(mxTarget,subTg4);
/*获得目标矩形比较区域*/
mxRolled=mxSource;//旋转0度
Split1(mxRolled,submx1);Split2(mxRolled,submx2);Split3(mxRolled,submx3);Split4(mxRolled,submx4);
/*获得源矩形比较区域*/
if(!Compare(submx1,subTg1)) OK1=false;//左上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx2,subTg2)) OK1=false;//右上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx3,subTg3)) OK1=false;//左下角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx4,subTg4)) OK1=false;//右下角小方阵递归加密不与目标方阵相应区域吻合
/*---------------------------------------------------------------------------------*/
mxRolled=Roll90(mxSource);//旋转90度
Split1(mxRolled,submx1);Split2(mxRolled,submx2);Split3(mxRolled,submx3);Split4(mxRolled,submx4);
/*获得源矩形比较区域*/
if(!Compare(submx1,subTg1)) OK2=false;//左上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx2,subTg2)) OK2=false;//右上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx3,subTg3)) OK2=false;//左下角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx4,subTg4)) OK2=false;//右下角小方阵递归加密不与目标方阵相应区域吻合
/*---------------------------------------------------------------------------------*/
mxRolled=Roll180(mxSource);//旋转180度
Split1(mxRolled,submx1);Split2(mxRolled,submx2);Split3(mxRolled,submx3);Split4(mxRolled,submx4);
/*获得源矩形比较区域*/
if(!Compare(submx1,subTg1)) OK3=false;//左上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx2,subTg2)) OK3=false;//右上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx3,subTg3)) OK3=false;//左下角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx4,subTg4)) OK3=false;//右下角小方阵递归加密不与目标方阵相应区域吻合
/*---------------------------------------------------------------------------------*/
mxRolled=Roll270(mxSource);//旋转270度
Split1(mxRolled,submx1);Split2(mxRolled,submx2);Split3(mxRolled,submx3);Split4(mxRolled,submx4);
/*获得源矩形比较区域*/
if(!Compare(submx1,subTg1)) OK4=false;//左上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx2,subTg2)) OK4=false;//右上角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx3,subTg3)) OK4=false;//左下角小方阵递归加密不与目标方阵相应区域吻合
if(!Compare(submx4,subTg4)) OK4=false;//右下角小方阵递归加密不与目标方阵相应区域吻合
if(OK1||OK2||OK3||OK4)
return true;
else
return false;
}
int main()
{
Matrix A,B;
short number;/*测试次数*/short w;/*边长变量*/
char str1[101],str2[101];/*为源矩阵和目标矩阵赋值的字符串*/
cout<<"请输入想要测试的次数:";
cin>>number;
short row,col;//用于循环遍历赋值
for(short i=0;i<number;i++)
{
cout<<"请输入边长:";
cin>>w;A.Init(w);B.Init(w);
cout<<"请输入含"<<w*w<<"个字符的字符串,为源矩阵赋值:";
cin>>str1;
cout<<"请输入含"<<w*w<<"个字符的字符串,为目标矩阵赋值:";
cin>>str2;
if(!FillValue(A,str1)){cout<<"非法输入,按任意键退出。\n";system("pause");return 0;}
if(!FillValue(B,str2)){cout<<"非法输入,按任意键退出。\n";system("pause");return 0;}
for(row=0;row<w;row++)
{
for(col=0;col<w;col++)
cout<<'\t'<<A.x[row][col];
cout<<endl;
}
cout<<endl;
for(row=0;row<w;row++)
{
for(col=0;col<w;col++)
cout<<'\t'<<B.x[row][col];
cout<<endl;
}
if(Compare(A,B)) cout<<"YES!";
else cout<<"NO!";
}
system("pause");
return 0;
}