http://train.usaco.org/usacoprob2?a=lZwR4PTPkId&S=transform
题目大意:
矩阵A执行如下操作是否能到矩阵C:
#1:顺时针旋转90°
#2:顺时针旋转180°
#3:顺时针选择270°
#4:左右对称
#5:对称后旋转90°——270°
#6:不改变
#7:不能变到
<pre name="code" class="html">#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
char a[10][10], b[10][10], c[10][10];
//a,原矩阵;b,用来操作的矩阵;c,目标矩阵
bool cpy(int n)
{
for(int i = 0; i < n; i++)
if(strcmp(b[i], c[i])) return false;
return true;
}
void p1(int n) //顺时针旋转90°
{
char d[15][15];
for(int i = 0; i < n; i++) strcpy(d[i], b[i]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
b[i][j] = d[n - j - 1][i];
}
void p2(int n) //左右对称
{
for(int i = 0; i < n; i++)
for(int j = n - 1; j >= 0; j--)
b[i][n - 1 - j] = a[i][j];
}
int main()
{
ifstream fin("transform.in");
ofstream fout("transform.out");
int n;
while(fin >> n)
{
bool f = false, f1 = false;
for(int i = 0; i < n; i++)
{
fin >> a[i];
strcpy(b[i], a[i]);
}
for(int i = 0; i < n; i++)
fin >> c[i];
if(cpy(n)) f1 = true;
for(int i = 1; i <= 3; i++) //#1-#3 顺时针旋转90°---270°
{
p1(n);
if(cpy(n))
{
f = true;
fout << i << endl;
break;
}
}
if(!f)
{
p2(n); //左右对称
if(cpy(n))
{
f = true;
fout << 4 << endl;
}
else for(int i = 1; i <= 3; i++) //对称后旋转
{
p1(n);
if(cpy(n))
{
f = true;
fout << 5 << endl;
break;
}
}
}
if(!f && f1) fout << 6 << endl;
if(!f && !f1) fout << 7 << endl;
fout.close();
}
return 0;
}
总结:代码能够逻辑清晰,但结构不够紧凑,函数写的太过于具体,如cpy, p1, p2都只能对函数中具体2中的矩阵操作,才不能对任意想进行操作的矩阵操作。因此后面的代码显得冗长