Magic Square(水题,我水人做水题,完美)

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.

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

 

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;

int a[3][3];
string str[105];
string s[3];

void ni(int x)
{
    int t;
    switch (x)
    {
    case 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;break;
    case 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;break;
    case 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;break;
    case 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;break;
    }
}
void shun(int y)
{
    int t;
    switch (y)
    {
    case 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;break;
    case 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;break;
    case 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;break;
    case 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;break;
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        for(int i=0;i<3;i++)
        {
            cin>>s[i];
            a[i][0]=s[i][0]-'0';
            a[i][1]=s[i][1]-'0';
            a[i][2]=s[i][2]-'0';
        }
        for(int i=0;i<n;i++)
        {
            cin>>str[i];
            if(str[i][1]=='C')
            {
                shun(str[i][0]-'0');
            }
            if(str[i][1]=='R')
            {
                ni(str[i][0]-'0');
            }
        }
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                cout<<a[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值