Luogu brush questions C++ language | P1205 Cube conversion

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

A  pattern of n × n  square black and white tiles is to be transformed into a new square pattern. Write a program to find the minimal way to transform an original pattern into a new pattern according to the following transformations:

  • Rotate 90°: The pattern is rotated 90° clockwise.
  • Rotate 180°: The pattern rotates 180° clockwise.
  • Rotate 270°: The pattern rotates 270° clockwise.
  • Reflection: The pattern is flipped horizontally (a mirror image of the original pattern centered on the central plumb line).
  • Combination: The pattern is flipped in the horizontal direction, and then converted again according to one of 1∼3.
  • Unchanged: The original pattern will not be changed.
  • Invalid Transformation: No new pattern can be obtained with the above method.

If there are several conversion methods available, choose the one with the lowest ordinal number.

Use only one of the 7 steps above to complete this conversion.

【enter】

The first line contains a positive integer  n .

Then  there are n  lines, each with  n  characters, all of which are @ or -, representing the initial square.

The next  n  lines, each with  n  characters, are all @ or -, indicating the final square.

【Output】

A single line containing a number between 1∼7 (described above) indicates the transformation method required to transform the untransformed square into the transformed square.

【Input example】

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

【Example of output】

1

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;

char a[15][15], b[15][15], c[15][15];  //a为输入的原始矩阵,c为输入旋转后的矩阵,b为a旋转的矩阵

bool func1(int n, char a[15][15], char c[15][15])
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (a[i][j] != c[j][n-1-i]) return false;
        }
    }
    return true;
}

bool func2(int n, char a[15][15], char c[15][15])
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (a[i][j] != c[n-1-i][n-1-j]) return false;
        }
    }
    return true;
}

bool func3(int n, char a[15][15], char c[15][15])
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (a[i][j] != c[n-1-j][i]) return false;
        }
    }
    return true;
}

bool func4(int n, char a[15][15], char c[15][15])
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (a[i][j] != c[i][n-1-j]) return false;
        }
    }
    return true;
}

bool func5(int n, char a[15][15], char c[15][15])
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            b[i][j] = a[i][n-1-j];
        }
    }
    if (func1(n, b, c) || func2(n, b, c) || func3(n , b, c)) return true;
    return false;
}

bool func6(int n, char a[15][15], char c[15][15])
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (a[i][j] != c[i][j]) return false;
        }
    }
    return true;
}

int main()
{
    int n;
    char c1;
    cin >> n;
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cin >> c1;
            a[i][j] = c1;
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cin >> c1;
            c[i][j] = c1;
        }
    }
    if (func1(n, a, c)) cout << 1;
    else if (func2(n, a, c)) cout << 2;
    else if (func3(n, a, c)) cout << 3;
    else if (func4(n, a, c)) cout << 4;
    else if (func5(n, a, c)) cout << 5;
    else if (func6(n, a, c)) cout << 6;
    else cout << 7;
    return 0;
}

【operation result】

3
@-@
---
@@-
@-@
@--
--@
1

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132686702