第二层第二题:矩阵变换

该博客讨论了一个编程问题,涉及识别如何从一个N*N的初始矩阵变换到目标矩阵,给出了7种可能的变换,包括旋转和反射。博主指出这个问题可以通过一步变换解决,并提供了相应的解题思路和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值