注:本题代码也是参考学习他人代码优化后的。
Description
一块 n×n 正方形的黑白瓦片的图案要被转换成新的正方形图案。写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式:
-
转 90°:图案按顺时针转 90°。
-
转 180°:图案按顺时针转 180°。
-
转 270°:图案按顺时针转 270°。
-
反射:图案在水平方向翻转(以中央铅垂线为中心形成原图案的镜像)。
-
组合:图案在水平方向翻转,然后再按照 1∼3 之间的一种再次转换。
-
不改变:原图案不改变。
-
无效转换:无法用以上方法得到新图案。
如果有多种可用的转换方法,请选择序号最小的那个。
只使用上述 77 个中的一个步骤来完成这次转换。
输入格式
第一行一个正整数 n。
然后 n 行,每行n 个字符,全部为 @
或 -
,表示初始的正方形。
接下来 n 行,每行 n 个字符,全部为 @
或 -
,表示最终的正方形。
输出格式
单独的一行包括 1∼71∼7 之间的一个数字(在上文已描述)表明需要将转换前的正方形变为转换后的正方形的转换方法。
Sample Input
3 @-@ --- @@- @-@ @-- --@
Sample Output
1
Hint
1≤n≤10。
#include <bits/stdc++.h>
using namespace std;
int n;
string s, e;
string R90(string tmp)
{
string re = tmp;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
re[j * n + n - 1 - i] = tmp[i * n + j];
}
}
return re;
}
string R180(string tmp)
{
return (R90(R90(tmp)));
}
string R270(string tmp)
{
return (R90(R90(R90(tmp))));
}
string fun(string tmp)
{
string re = tmp;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
re[i * n + j] = tmp[i * n + n - j - 1];
}
}
return re;
}
int main()
{
cin >> n;
string t;
for (int i = 0; i < n; i++)
{
cin >> t;
s += t;
}
for (int i = 0; i < n; i++)
{
cin >> t;
e += t;
}
if (R90(s) == e)
{
cout << 1;
}
else if (R180(s) == e)
{
cout << 2;
}
else if (R270(s) == e)
{
cout << 3;
}
else if ((t = fun(s)) == e)
{
cout << 4;
}
else if (R90(t) == e || R180(t) == e || R270(t) == e)
{
cout << 5;
}
else if (s == e)
{
cout << 6;
}
else
{
cout << 7;
}
return 0;
}