欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 6401 - Magic Square(模拟)

程序员文章站 2022-06-07 09:55:25
...

Magic Square

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 440    Accepted Submission(s): 362


 

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.

HDU 6401 - Magic Square(模拟)



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

 

思路

这个题的意思就是给你一个3x3的矩阵,给你一条指令,例如1C,意思就是将图示中1这个2x2的小矩阵顺时针旋转一次,1R,意思就是将图示中1这个2x2的小矩阵逆时针旋转一次。

这个题比较水,属于模拟题,那么水题就用水题的做法做,所以代码有点长。

代码

#include<cstdio>

using namespace std;

int main(){
    int t;
    scanf("%d",&t);

    while(t--){
        int n;
        scanf("%d",&n);

        int a[3][3],i=0;
        for(int i=0;i<3;i++){
            getchar();
            for(int j=0;j<3;j++){
                char c = getchar();
                a[i][j] = c - '0';
            }
        }
        getchar();

        while(n--){
            int t;
            int p = getchar() - '0';
            char q = getchar();
            getchar();
            if(q == 'C'){
                if(p == 1){
                    t = a[0][0];
                    a[0][0] = a[1][0];
                    a[1][0] = a[1][1];
                    a[1][1] = a[0][1];
                    a[0][1] = t;
                }else if(p == 2){
                    t=a[0][1];
                    a[0][1]=a[1][1];
                    a[1][1]=a[1][2];
                    a[1][2]=a[0][2];
                    a[0][2]=t;
                }else if(p == 3){
                    t=a[1][0];
                    a[1][0]=a[2][0];
                    a[2][0]=a[2][1];
                    a[2][1]=a[1][1];
                    a[1][1]=t;
                }else if(p == 4){
                    t=a[1][1];
                    a[1][1]=a[2][1];
                    a[2][1]=a[2][2];
                    a[2][2]=a[1][2];
                    a[1][2]=t;
                }
            }else{
                if(p == 1){
                    t=a[0][0];
                    a[0][0]=a[0][1];
                    a[0][1]=a[1][1];
                    a[1][1]=a[1][0];
                    a[1][0]=t;
                }else if(p == 2){
                    t=a[0][1];
                    a[0][1]=a[0][2];
                    a[0][2]=a[1][2];
                    a[1][2]=a[1][1];
                    a[1][1]=t;
                }else if(p == 3){
                    t=a[1][0];
                    a[1][0]=a[1][1];
                    a[1][1]=a[2][1];
                    a[2][1]=a[2][0];
                    a[2][0]=t;
                }else if(p == 4){
                    t=a[1][1];a[1][1]=a[1][2];a[1][2]=a[2][2];a[2][2]=a[2][1];a[2][1]=t;
                }
            }
        }

        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                printf("%d",a[i][j]);
            }
            printf("\n");

        }
    }

    return 0;
}

 

相关标签: hdu 2018 c