洛谷 P1205 [USACO1.2] 方块转换 Transformations

[USACO1.2] 方块转换 Transformations

题目描述

一块 \(n \times n\) 正方形的黑白瓦片的图案要被转换成新的正方形图案。写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式:

  • 转 90° :图案按顺时针转 90° 。

  • 转 180° :图案按顺时针转 180° 。

  • 转 270° :图案按顺时针转 270° 。

  • 反射:图案在水平方向翻转(以中央铅垂线为中心形成原图案的镜像)。

  • 组合:图案在水平方向翻转,然后再按照 \(1 \sim 3​\) 之间的一种再次转换。

  • 不改变:原图案不改变。

  • 无效转换:无法用以上方法得到新图案。

如果有多种可用的转换方法,请选择序号最小的那个。

只使用上述 \(7\) 个中的一个步骤来完成这次转换。

输入格式

第一行一个正整数 \(n\)

然后 \(n\) 行,每行 \(n\) 个字符,全部为 @-,表示初始的正方形。

接下来 \(n\) 行,每行 \(n\) 个字符,全部为 @-,表示最终的正方形。

输出格式

单独的一行包括 \(1 \sim 7\) 之间的一个数字(在上文已描述)表明需要将转换前的正方形变为转换后的正方形的转换方法。

样例 #1

样例输入 #1

3
@-@
---
@@-
@-@
@--
--@

样例输出 #1

1

提示

【数据范围】
对于 \(100\%\) 的数据,\(1\le n \le 10\)

题目翻译来自 NOCOW。

USACO Training Section 1.2

若原图形为 a [ i ] [ j ]旋转后图形为 b[ i ][ j ]则将图形顺时针旋转九十度 b[ i ][ j ] = a[ j ][ n - i + 1 ]

将图形逆时针旋转九十度 b[ i ][ j ] = a[ n - j + 1 ] [i ]

将图形在水平方向翻转(以中央铅垂线为中心形成原图案的镜像)b[ i ][ j ] = a[ i ][ n - j + 1 ]

#include <bits/stdc++.h>
// #define ONLINE_JUDGE
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define SZ(v) ((int)v.size())
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef double db;
using namespace std;

int _;
int n;
char a[11][11], b[11][11], c[11][11];

bool check(char a[11][11], char b[11][11], int n) {  //旋转90度
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) if(a[i][j] != b[i][j]) return false;
    }
    return true;
}

void print(char a[11][11]) {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            cout << a[i][j] << " ";
        }
        cout << "\n";
    }
    cout << "\n";
}

void reverse_90(char a[11][11], char b[11][11]) {
    char d[11][11];
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= n; j++) 
            d[i][j] = a[i][j];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            a[i][j] = d[n-j+1][i];
        }
    }
}

bool f1(char a[11][11], char b[11][11]) {
    reverse_90(a, b);
    if(check(a, b, n)) return 1;
    return 0;
}

bool f2(char a[11][11], char b[11][11]) {
    reverse_90(a, b);
    if(check(a, b, n)) return 1;
    return 0;
}

bool f3(char a[11][11], char b[11][11]) {
    reverse_90(a, b);
    if(check(a, b, n)) return 1;
    return 0;
}

bool f4(char a[11][11], char b[11][11]) {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            a[i][j] = c[i][j];
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            a[i][j] = c[i][n-j+1];
        }
    }
    if(check(a, b, n)) return 1;
    return 0;
}

bool f5(char a[11][11], char b[11][11]) {
    reverse_90(a, b);
    if(check(a, b, n)) return 1;
    reverse_90(a, b);
    if(check(a, b, n)) return 1;
    reverse_90(a, b);
    if(check(a, b, n)) return 1;
    return 0;
}

bool f6(char a[11][11], char b[11][11]) {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            a[i][j] = c[i][j];
        }
    }
    if(check(a, b, n)) return 1;
    return 0;
}

void solve() {
    cin >> n;
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= n; j++) {
            cin >> a[i][j];
            c[i][j] = a[i][j];
        }
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= n; j++) 
            cin >> b[i][j];
    if(f1(a, b)) {
        cout << "1\n";
    }
    else if(f2(a, b)) {
        cout << "2\n";
    }
    else if(f3(a, b)) {
        cout << "3\n";
    }
    else if(f4(a, b)) {
        cout << "4\n";
    }
    else if(f5(a, b)) {
        cout << "5\n";
    }
    else if(f6(a, b)) {
        cout << "6\n";
    }
    else {
        cout << "7\n";
    }
}

int main() {
    FAST;       
    // cin >> _;
    // while(_--) {
        solve();
    // }
    return 0;
}
posted @ 2022-11-30 14:29  Evan619  阅读(141)  评论(0)    收藏  举报