广搜题,由于情况数很少,依次判断每一种情况即可。
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 15;
struct Node
{
char mp[MAXN][MAXN]; //图形
int isReflect; //是否翻转
};
char ed[MAXN][MAXN]; //终止图形
Node now, nex, temp;
int n; //图形边长
//判断node的图形是否为终止图形
bool isEnd(Node node)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (node.mp[i][j] != ed[i][j])
return false;
}
}
return true;
}
//旋转90°
void rt90()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
now.mp[j][n - i - 1] = nex.mp[i][j];
}
}
}
//旋转180°
void rt180()
{
rt90();
temp = nex;
nex = now;
rt90();
nex = temp;
}
//旋转270°
void rt270()
{
rt90();
temp = nex;
nex = now;
rt90();
nex = now;
rt90();
nex = temp;
}
//翻转
void reflect()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
now.mp[n - i - 1][j] = nex.mp[i][j];
}
}
}
//广搜
void BFS()
{
queue<Node> q;
if (isEnd(now))
{
cout << "Preserved" << endl;
return;
}
now.isReflect = 0;
q.push(now);
while (!q.empty())
{
nex = q.front();
q.pop();
if (nex.isReflect == 0) //未翻转
{
now.isReflect = 1;
rt90();
if (isEnd(now))
{
cout << "Rotated through 90 degrees" << endl;
return;
}
rt180();
if (isEnd(now))
{
cout << "Rotated through 180 degrees" << endl;
return;
}
rt270();
if (isEnd(now))
{
cout << "Rotated through 270 degrees" << endl;
return;
}
reflect();
if (isEnd(now))
{
cout << "Reflected" << endl;
return;
}
q.push(now);
}
else if (nex.isReflect == 1) //已翻转
{
rt90();
if (isEnd(now))
{
cout << "Reflected and rotated through 90 degrees" << endl;
return;
}
rt180();
if (isEnd(now))
{
cout << "Reflected and rotated through 180 degrees" << endl;
return;
}
rt270();
if (isEnd(now))
{
cout << "Reflected and rotated through 270 degrees" << endl;
return;
}
cout << "Improper" << endl;
}
}
}
int main()
{
while (cin >> n)
{
if (n == 0)
break;
for (int i = 0; i < n; i++)
{
cin >> now.mp[i] >> ed[i];
}
BFS();
}
return 0;
}
继续加油。