Luogu P4924 [1007] Magical Girl Little Scarlet
A simulation, enumeration, and violence problem about matrix transposition
The topic is very conscientious and does not need to consider cross-border situations.
Idea: Because the matrix is rotated around a certain center (x, y) and is guaranteed to be a square, in order to get the correct formula and easy to understand, we might as well set (x, y) as the origin (0, 0), so that a little After a simulation, you can get the coordinates of a point (i, j) on the square after rotating 90° clockwise becomes (j, -i), and after rotating 90° counterclockwise, the coordinates become (-j, i), and then enumerate i, j is enough.
Clockwise and counterclockwise rotation
void clockwise(int x,int y,int r){
//顺时针
for(int i=-r;i<=r;i++){
for(int j=-r;j<=r;j++){
temp[x+j][y-i]=g[x+i][y+j];
}
}
}
void anticlockwise(int x,int y,int r){
//逆时针
for(int i=-r;i<=r;i++){
for(int j=-r;j<=r;j++){
temp[x-j][y+i]=g[x+i][y+j];
}
}
}
answer
#include<iostream>
#include<stdio.h>
using namespace std;
const int N=505;
int n,m,sum=0;//方阵大小 魔法释放次数
int x,y,r,z;
int g[N][N],temp[N][N];
void clockwise(int x,int y,int r){
//顺时针
for(int i=-r;i<=r;i++){
for(int j=-r;j<=r;j++){
temp[x+j][y-i]=g[x+i][y+j];
}
}
}
void anticlockwise(int x,int y,int r){
//逆时针
for(int i=-r;i<=r;i++){
for(int j=-r;j<=r;j++){
temp[x-j][y+i]=g[x+i][y+j];
}
}
}
void same(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
g[i][j]=temp[i][j];
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
sum++;
g[i][j]=sum;
temp[i][j]=g[i][j];
// cout<<g[i][j]<<" ";
}
// cout<<endl;
}
while(m--){
scanf("%d%d%d%d",&x,&y,&r,&z);
if(z==0){
//顺时针
clockwise(x,y,r);
}
else{
//逆时针
anticlockwise(x,y,r);
}
same();
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("%d ",g[i][j]);
}
printf("\n");
}
return 0;
}