您的位置:首页 > 其它

第二层第二题:矩阵变换

2015-10-05 20:42 204 查看
Transformations
A square pattern of size N x N (1 <= N <= 10) black and white squaretiles is transformed into another square pattern. Write a program that willrecognize the minimum transformation that has been applied to the originalpattern given the following
list of possible transformations:
#1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
#2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
#3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
#4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).
#5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).

#6: No Change: The original pattern was not changed.
#7: Invalid Transformation: The new pattern was not obtained by any of the above methods.

In the case that more than one transform could have been used, choose theone with the minimum number above.

PROGRAM NAME: transform
INPUT FORMAT
Line 1:
A single integer, N
Line 2..N+1:
N lines of N characters (each either `@' or `-'); this is the square before transformation
Line N+2..2*N+1:
N lines of N characters (each either `@' or `-'); this is the square after transformation
SAMPLE INPUT (file transform.in)
3
@-@
---
@@-
@-@
@--
--@
OUTPUT FORMAT
A single line containing the the number from 1 through 7(described above) that categorizes the transformation required to change fromthe `before' representation to the `after' representation.

SAMPLE OUTPUT (file transform.out)
1
 

       题目大意是给出两个N*N的由’@’和’_’构成的矩阵,第一个为初始矩阵,第二个为目标矩阵,矩阵可以支持如下七种操作:

       1、顺时针转90度。

       2、顺时针转180度。

       3、顺时针转270度。= =!

       4、关于水平方向上的中心线对称

              例如:

              


              执行4操作后:

                              


       5、先执行4操作,再执行1、2、3操作的任意一种。

       6、不用操作就可以到达目标状态。(←_←)

       7、怎么操作都到达不了目标状态。(↑_↑)

       描述完成,那么问题来了:要怎么变换才能达到目标状态?请输出变换步骤。

       初次一看这题吓了一跳。。。这尼玛不是八数码一类的题目?机智的楼主又一次开了百度。。。发现一片人都说这题是只要一步变换就可以了。于是将信将疑地去试了一下。。。还真的过了,好吧。。。那么这只是一道模拟题,注意一下各方块变换前和变换后的坐标关系,就可以for循环解决了(≥W≤)

       附上代码:

/*
ID:su1q2d21
LANG:C++
TASK:transform
*/
#include<iostream>
#include<cstdio>
#define maxn 20
using namespace std;
int n;
char tmp[maxn][maxn],tmp1[maxn][maxn];
char st[maxn][maxn],ed[maxn][maxn];
bool chk(char tmp[][maxn])
{
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(tmp[i][j]!=ed[i][j]) return false;
}
}
return true;
}
void opt(int x,char st[][maxn],char tmp[][maxn])
{
int i,j;
if(x==1){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++) tmp[j][n-i+1]=st[i][j];
}
}
if(x==2){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++) tmp[n-i+1][n-j+1]=st[i][j];
}
}
if(x==3){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++) tmp[n-j+1][i]=st[i][j];
}
}
if(x==4){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++) tmp[i][n-j+1]=st[i][j];
}
}
return ;
}
int main()
{
freopen("transform.in","r",stdin);
freopen("transform.out","w",stdout);
int i,j;
char ctmp;
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%c",&ctmp);
while(ctmp!='@' && ctmp!='-') scanf("%c",&ctmp);
st[i][j]=ctmp;
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%c",&ctmp);
while(ctmp!='@' && ctmp!='-') scanf("%c",&ctmp);
ed[i][j]=ctmp;
}
}
opt(1,st,tmp);
if(chk(tmp)){
printf("1\n");
return 0;
}
opt(2,st,tmp);
if(chk(tmp)){
printf("2\n");
return 0;
}
opt(3,st,tmp);
if(chk(tmp)){
printf("3\n");
return 0;
}
opt(4,st,tmp);
if(chk(tmp)){
printf("4\n");
return 0;
}
opt(1,tmp,tmp1);
if(chk(tmp1)){
printf("5\n");
return 0;
}
opt(2,tmp,tmp1);
if(chk(tmp1)){
printf("5\n");
return 0;
}
opt(3,tmp,tmp1);
if(chk(tmp1)){
printf("5\n");
return 0;
}
if(chk(st)){
printf("6\n");
return 0;
}
printf("7\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: