P1205 [USACO1.2] 方块转换 Transformations

· · 个人记录

错误原因

旋转了原数组,导致最后条件 6 判断有误

更正方法

创建备用数组,只对备用数组判断

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
char a[11][11];
char b[11][11];
char ans[11][11];
void zhuan(char a[][11],char b[][11]){
    char c[11][11];
    int x,y;
    for(int i=1;i<=n;i++){
        x=i;
        y=1;
        for(int j=n;j>=1;j--){
            c[x][y]=a[j][i];
            y++;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            b[i][j]=c[i][j];
        }
    }
    return;
}
void fan(char a[][11],char b[][11]){
    char c[11][11];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            c[i][j]=a[i][n-j+1];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            b[i][j]=c[i][j];
        }
    }
    return;
}
bool check(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(b[i][j]!=ans[i][j]){
                return false;
            }
        }
    }
    return true;
}
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>ans[i][j];
        }
    }
    zhuan(a,b);
    if(check()){cout<<1;return 0;}
    zhuan(b,b);
    if(check()){cout<<2;return 0;}
    zhuan(b,b);
    if(check()){cout<<3;return 0;}
    fan(a,b);
    if(check()){cout<<4;return 0;}
    zhuan(b,b);
    if(check()){cout<<5;return 0;}
    zhuan(b,b);
    if(check()){cout<<5;return 0;}
    zhuan(b,b);
    if(check()){cout<<5;return 0;}
    bool flag=true;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j]!=ans[i][j]){
                flag=false;
                break;
            }
        }
    }
    if(flag)cout<<6;
    else cout<<7;
    return 0;
}