这道搜索题没什么绕弯的地方,把transformation用矩阵位置变换表示,然后一一验证即可。
对于#5 combination我写得比较啰嗦: )
/*
ID: alexyua2
PROG: transform
LANG: C++
*/
#include<fstream>
using namespace std;
ifstream fin("transform.in");
ofstream fout("transform.out");
//ifstream fin("in.txt");
//ofstream fout("out.txt");
int MAX = 11;
int main()
{
//input
int N;
fin>>N;
char before[MAX][MAX],after[MAX][MAX];
int i,j;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
fin>>before[i][j];
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
fin>>after[i][j];
//search transformation pattern
//#1:90 Degree Rotation
int state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(before[i][j] != after[j][N+1-i])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"1" <<endl;
return 0;
}
//#2:180 Degree Rotation
state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(before[i][j] != after[N+1-i][N+1-j])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"2" <<endl;
return 0;
}
//#3:270 Degree Rotation
state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(before[i][j] != after[N+1-j][i])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"3" <<endl;
return 0;
}
//#4:Reflection
state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(before[i][j] != after[i][N+1-j])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"4" <<endl;
return 0;
}
//#5:Combination
char temp[MAX][MAX];
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
temp[i][j] = before[i][N+1-j];
state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(temp[i][j] != after[j][N+1-i])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"5" <<endl;
return 0;
}
state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(temp[i][j] != after[N+1-i][N+1-j])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"5" <<endl;
return 0;
}
state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(temp[i][j] != after[N+1-j][i])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"5" <<endl;
return 0;
}
//#6:No Change
state = 1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
if(before[i][j] != after[i][j])
{
state = 0;
break;
}
if(state == 1)
{
fout<<"6" <<endl;
return 0;
}
//#7:No Answer
fout<<"7" <<endl;
//close files
fin.close();
fout.close();
return 0;
}