维护$n^{2}$个三元组$(x,y,z)$,每一个三元组描述$a_{x,y}=z$

对于RLDU这四个操作,即将所有三元组的$x$或$y$执行$\pm 1$(模$n$意义下)

对于IC这两个操作,即分别将$y$和$z$交换或$x$和$z$交换

维护这三个元素交换的顺序以及每一个元素加减的量,最终再$o(n^{2})$解出即可

时间复杂度为$o(n^{2}+m)$,可以通过

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 1005
4 #define M 100005
5 int t,n,m,num[11],p[3],d[3],b[3],a[N][N],ans[N][N];
6 char s[M];
7 int read(){
8 int x=0;
9 char c=getchar();
10 while ((c<'0')||(c>'9'))c=getchar();
11 while ((c>='0')&&(c<='9')){
12 x=x*10+c-'0';
13 c=getchar();
14 }
15 return x;
16 }
17 void write(int x,char c='\0'){
18 while (x){
19 num[++num[0]]=x%10;
20 x/=10;
21 }
22 if (!num[0])putchar('0');
23 while (num[0])putchar(num[num[0]--]+'0');
24 putchar(c);
25 }
26 int main(){
27 t=read();
28 while (t--){
29 n=read(),m=read();
30 for(int i=1;i<=n;i++)
31 for(int j=1;j<=n;j++)a[i][j]=read();
32 scanf("%s",s);
33 //当前第i维为原来第pi维+di
34 p[0]=0,p[1]=1,p[2]=2,d[0]=d[1]=d[2]=0;
35 for(int i=0;i<m;i++){
36 if (s[i]=='R')d[1]++;
37 if (s[i]=='L')d[1]--;
38 if (s[i]=='D')d[0]++;
39 if (s[i]=='U')d[0]--;
40 if (s[i]=='I'){
41 swap(p[1],p[2]);
42 swap(d[1],d[2]);
43 }
44 if (s[i]=='C'){
45 swap(p[0],p[2]);
46 swap(d[0],d[2]);
47 }
48 }
49 for(int i=1;i<=n;i++)
50 for(int j=1;j<=n;j++){
51 b[0]=i,b[1]=j,b[2]=a[i][j];
52 int x=((b[p[0]]+d[0])%n+n-1)%n+1;
53 int y=((b[p[1]]+d[1])%n+n-1)%n+1;
54 int z=((b[p[2]]+d[2])%n+n-1)%n+1;
55 ans[x][y]=z;
56 }
57 for(int i=1;i<=n;i++){
58 for(int j=1;j<n;j++)write(ans[i][j],' ');
59 write(ans[i][n],'\n');
60 }
61 }
62 return 0;
63 }

[cf1458C]Latin Square的更多相关文章

  1. 算法:拉丁方阵(Latin Square)

    拉丁方阵(英语:Latin square)是一种 n × n 的方阵,在这种 n × n 的方阵里,恰有 n 种不同的元素,每一种不同的元素在同一行或同一列里只出现一次.以下是两个拉丁方阵举例: 拉丁 ...

  2. namespace_std 杂题选讲

    CF1458C Latin Square 2021 EC Final C. Random Shuffle [THUPC2021] 混乱邪恶 [JOISC2022] 制作团子 3 2022 集训队互测 ...

  3. USACO 6.5 All Latin Squares

    All Latin Squares A square arrangement of numbers 1 2 3 4 5 2 1 4 5 3 3 4 5 1 2 4 5 2 3 1 5 3 1 2 4 ...

  4. 解数独(Python)

    0.目录 1.介绍 2.一些通用函数 3.全局变量(宏变量) 4.数独预处理(约束传播) 5.解数独(深度优先搜索+最小代价优先) 6.主函数 7.总代码 1.介绍 数独是一个非常有趣味性的智力游戏, ...

  5. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  6. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  7. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  8. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  9. OPEN CASCADE Gauss Least Square

    OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...

随机推荐

  1. C++ 与 Visual Studio 2019 和 WSL(四)——库组件

    C++ 与 Visual Studio 2019 和 WSL (库组件) Reference 在 C++ 项目中使用库和组件 | Microsoft Docs 演练:创建和使用自己的动态链接库 (C+ ...

  2. C++ 与 Visual Studio 2019 和 WSL(二)

    终端 A more integrated terminal experience | Visual Studio Blog (microsoft.com) Say hello to the new V ...

  3. 初识HTML02

    HTML 超文本标记语言 什么是超文本标记语言 浏览器能够解释和解析的语言 通过元素的形式构建页面结构和填充内容 构建HTML页面 构建页面的步骤 创建一个扩展名为.html和.html的页面文件 向 ...

  4. 什么是Spring,SpringMVC,SpringBoot,SpringCloud?通俗易懂

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架.Spring使你能够编写更干净.更可管理.并且更易于测试的代码. Spring MVC是Spring的一个模块,一个web框 ...

  5. [软软软]技术博客-Commitizen优化git commit

    工具介绍 commitizen/cz-cli是一个规范git commit的工具,使用它代替git commit能够方便有效地写好提交的log,使得团队项目的版本信息更清晰. 安装 (全局安装) np ...

  6. 2021.10.26考试总结[冲刺NOIP模拟16]

    T1 树上的数 \(DFS\)一遍.结构体存边好像更快? \(code:\) T1 #include<bits/stdc++.h> using namespace std; namespa ...

  7. RAW RGB格式

    RAW RGB格式 10bit Raw RGB, 就是说用10bit去表示一个R, G, 或者B, 通常的都是用8bit的. 所以你后面处理时要把它转换为8bit的, 比较简单的方法就是将低两位去掉, ...

  8. 理解ASP.NET Core - 文件服务器(File Server)

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 提供静态文件 静态文件默认存放在 Web根目录(Web Root) 中,路径为 项目根目录(C ...

  9. linux 文件描述符和inode 的理解和区别

    inode 或i节点是指对文件的索引.如一个系统,所有文件是放在磁盘或flash上,就要编个目录来说明每个文件在什么地方,有什么属性,及大小等.就像书本的目录一样,便于查找和管理.这目录是操作系统需要 ...

  10. RocketMQ Consumer 启动时都干了些啥?

    可能我们对 RocketMQ 的消费者认知乍一想很简单,就是一个拿来消费消息的客户端而已,你只需要指定对应的 Topic 和 ConsumerGroup,剩下的就是只需要: 接收消息 处理消息 就完事 ...