洛谷笔记 - P4924 [1007] 魔法少女小 Scarlet

年轻人,要和我签订契约,成为 魔法少女 CS 大神吗?

挺锻炼耐心的一道题。

题目描述

Scarlet 最近学会了一个数组魔法,她会在 n×nn\times n 二维数组上将一个奇数阶方阵按照顺时针或者逆时针旋转 9090^\circ

首先,Scarlet 会把 11n2n^2 的正整数按照从左往右,从上至下的顺序填入初始的二维数组中,然后她会施放一些简易的魔法。

Scarlet 既不会什么分块特技,也不会什么 Splay 套 Splay,她现在提供给你她的魔法执行顺序,想让你来告诉她魔法按次执行完毕后的二维数组。

输入格式

第一行两个整数 n,mn,m,表示方阵大小和魔法施放次数。

接下来 mm 行,每行 44 个整数 x,y,r,zx,y,r,z,表示在这次魔法中,Scarlet 会把以第 xx 行第 yy 列为中心的 2r+12r+1 阶矩阵按照某种时针方向旋转,其中 z=0z=0 表示顺时针,z=1z=1 表示逆时针。

输出格式

输出 nn 行,每行 nn 个用空格隔开的数,表示最终所得的矩阵

样例 #1

样例输入 #1

1
2
3
4
5
5 4
2 2 1 0
3 3 1 1
4 4 1 0
3 3 2 1

样例输出 #1

1
2
3
4
5
5 10 3 18 15
4 19 8 17 20
1 14 23 24 25
6 9 2 7 22
11 12 13 16 21

提示

对于 50% 的数据,满足 r=1r=1

对于 100% 的数据 1n,m5001\leq n,m\leq500,满足 1xrx+rn,1yry+rn1\leq x-r\leq x+r\leq n,1\leq y-r\leq y+r\leq n

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Problem: P4924 [1007]魔法少女小Scarlet
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4924
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

// https://www.luogu.com.cn/record/88875099
// https://www.luogu.com.cn/record/88877110
// https://www.luogu.com.cn/record/88878988

#include<iostream>
using namespace std;
int main(){
int n,m,p[501][501],nn=0;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int o=1;o<=n;o++){
p[i][o]=++nn;
}
}
for(int i=1;i<=m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d; //请注意:输入顺序是先输入中心点纵坐标,后输入中心点横坐标
//而非一般而言的先输入横坐标后输入纵坐标!
int tmp[2*c+1][2*c+1],ly=a-c,lx=b-c;
for(int x=a-c;x<=a+c;x++){
for(int o=b-c;o<=b+c;o++){
if(x<1&&o<1&&x>n&&o>n){
continue;
}
if(d==0){
tmp[o-lx][2*c-(x-ly)]=p[x][o];
}else if(d==1){
tmp[2*c-(o-lx)][x-ly]=p[x][o];
}
}
}
for(int x=a-c;x<=a+c;x++){
for(int o=b-c;o<=b+c;o++){
if(x<1&&o<1&&x>n&&o>n){
continue;
}
p[x][o]=tmp[x-ly][o-lx];
}
}
}
for(int i=1;i<=n;i++){
for(int o=1;o<=n;o++){
cout<<p[i][o]<<" ";
}
cout<<endl;
}
return 0;
}