跳到正文

个人记录

方块旋转

chenqingwei约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月7日
篇幅
约 2 分钟
获赞
1

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;
int n;
char a[12][12],b[12][12],c[12][12],d[12][12];

void rotate90() //顺时针转90度
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		   {
			d[j][n+1-i]=c[i][j];}
	}
	 memcpy(c, d, sizeof(d));
	return;
}
void reverse() //⽔平翻转
{
	for(int i=1;i<=n;i++)
		{ for(int j=1;j<=n;j++)
			c[i][n+1-j]=a[i][j];
		}
}
bool chk() //检查是否为⽬标图案
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(c[i][j]!=b[i][j])
				return 0;
		}
	}
	return 1;
}
void restore() //复原
{
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=n;j++)
		{
			c[i][j]=a[i][j];
		}
	}
}

int work(){
//1~3
for(int i=1;i<=3;++i){
	rotate90();
	if(chk()) return i;
 }
//4
    restore();
	reverse();
	if(chk()) return 4;
//5
for(int i=1;i<=3;++i){
	rotate90();
	if(chk()) return 5;
 }
//6
    restore();
	if(chk()) return 6;
	//7
	return 7;
}
int main(){
//输⼊
	cin>>n;
	for(int i=1;i<=n;++i) cin>>a[i]+1;
	for(int i=1;i<=n;++i) cin>>b[i]+1;
	memcpy(c, a, sizeof(a));
	cout<<work();
	//for(int i=1;i<=n;++i) cout<<c[i]+1<<endl;
	return 0;
}
原文