方块旋转

· · 个人记录

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