HDU6401 Magic Square(2018HDU多校联赛第八场,模拟)

Problem Description

A magic square is a 3×3 square, where each element is a single digit between 1 and 9 inclusive, and each digit appears exactly once. There are 4 different contiguous 2×2 subsquares in a magic squares, which are labeled from 1 to 4 as the following figure shows. These 2×2 subsquares can be rotated. We use the label of the subsquare with an uppercase letter to represent a rotation. If we rotate the subsquare clockwise, the letter is ‘C’; if we rotate it counterclockwise, the letter is ‘R’. The following figure shows two different rotations.
img
Now, given the initial state of a magic square and a sequence of rotations, please print the final state of the magic square after these rotations are performed.

Input

The first line of input is a single integer T (1≤T≤100), the number of test cases.
Each test case begins with a single integer n (1≤n≤100), the number of rotations. It is then followed by a 3×3 square, where every digit between 1 and 9 inclusive appears exactly once, representing the initial state of the magic square. The following n lines describe the sequence of rotations.
The test data guarantees that the input is valid.

Output

For each test case, display a 3×3 square, denoting the final state of the magic square.

Sample Input

1
2
123
456
789
1C
4R

Sample Output

413
569
728

# 思路

题意是给一个 33 3 ∗ 3 的矩阵,分成了四个块,然后让按照题目给的旋转这些块,直接模拟即可。

代码

#include 
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int N = 2e5 + 100;
char s[5][5];
void rot(int x, int y) //顺时针旋转
{
    char t[5][5];
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            t[j][1 - i] = s[x + i][y + j];
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            s[x + i][y + j] = t[i][j];
}
void print()
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            printf("%d", s[i][j]);
        puts("");
    }
}
void calc(int x)
{
    if (x == 1)
        rot(0, 0);
    else if (x == 2)
        rot(0, 1);
    else if (x == 3)
        rot(1, 0);
    else if (x == 4)
        rot(1, 1);
}
void solve()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < 3; i++)
    {
        scanf("%s", s[i]);
        for (int j = 0; j < 3; j++)
            s[i][j] -= '0';
    }
    int x;
    char op[3];
    while (n--)
    {
        scanf("%d%s", &x, op);
        if (op[0] == 'C')
            calc(x);
        else
            calc(x), calc(x), calc(x);
    }
    print();
}
int main()
{
    //freopen("in.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
        solve();
    return 0;
}

你可能感兴趣的:(【思路,模拟,构造】,【多校联赛】)