Magic Square
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 70 Accepted Submission(s): 60
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.
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
Source
2018 Multi-University Training Contest 8
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-------------------------"<<endl;
typedef long long ll;
const int maxn = 1e6+10;
const int inf = 0x3f3f3f3f;
const int Mod = 1e9+7;
const int N = 1010;
int T,n,m,k;
int main(){
scanf("%d",&T);
while(T--){
int q;
scanf("%d",&q);
getchar();
char s[3][4];
for(int i=0;i<3;i++)
scanf("%s",s[i]);
while(q--){
int id,t;
char c;
scanf("%d%c",&id,&c);
if(id == 1){
t = s[0][0];
if(c == 'C'){
s[0][0] = s[1][0];
s[1][0] = s[1][1];
s[1][1] = s[0][1];
s[0][1] = t;
}
else{
s[0][0] = s[0][1];
s[0][1] = s[1][1];
s[1][1] = s[1][0];
s[1][0] = t;
}
}
else if(id == 2){
t = s[0][1];
if(c == 'C'){
s[0][1] = s[1][1];
s[1][1] = s[1][2];
s[1][2] = s[0][2];
s[0][2] = t;
}
else{
s[0][1] = s[0][2];
s[0][2] = s[1][2];
s[1][2] = s[1][1];
s[1][1] = t;
}
}
else if(id == 3){
t = s[1][0];
if(c == 'C'){
s[1][0] = s[2][0];
s[2][0] = s[2][1];
s[2][1] = s[1][1];
s[1][1] = t;
}
else{
s[1][0] = s[1][1];
s[1][1] = s[2][1];
s[2][1] = s[2][0];
s[2][0] = t;
}
}
else if(id == 4){
t = s[1][1];
if(c == 'C'){
s[1][1] = s[2][1];
s[2][1] = s[2][2];
s[2][2] = s[1][2];
s[1][2] = t;
}
else{
s[1][1] = s[1][2];
s[1][2] = s[2][2];
s[2][2] = s[2][1];
s[2][1] = t;
}
}
}
for(int i=0;i<3;i++)
puts(s[i]);
}
return 0;
}