维护$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. javascriptRemke之深入迭代

    javascriptRemke之深入迭代 前言:"迭代"意为按照顺序反复多次执行一段程序,ECMAscript6中新增了两个高级特性:迭代器与生成器,使用这两个特性能更高效地实现迭 ...

  2. CF193D Two Segments (线段树+dp)(外加两个扩展题)

    大概算是个系列整理 (最强版是模拟赛原题)) 首先,我们先来看这个题目. QWQ一开始是毫无头绪,除了枚举就是枚举 首先,我们可以枚举一个右端点,然后算一下当前右端点的答案 我们令\(f[l,r]\) ...

  3. 👊 Spring技术原理系列(7)带你看看那些可能你还不知道的Spring特性技巧哦!

    前提介绍 本文主要介绍相关Spring框架的一些新特性问题机制,包含了一些特定注解方面的认识. @Lazy可以延迟依赖注入 @Lazy注解修饰在类层面! @Lazy @Service public c ...

  4. 从0到1使用Kubernetes系列(三):使用Ansible安装Kubernetes集群

    前两期的文章介绍了Kubernetes基本概念和架构,用Kubeadm+Ansible搭建Kubernetes集群所需要的工具及其作用.本篇介绍怎么使用Ansible安装Kubernetes集群. 启 ...

  5. ToString()字符串转换你用正确了吗?

    前言 在开发中,ToString()这个方法太方便了,以致于误解大家转换时都是直接Object.ToString()直接转换, 其实不然, 一般都是转之前先判断是否为null后再进行转换,否则会直接报 ...

  6. GAN实战笔记——第一章GAN简介

    GAN简介 一.什么是GAN GAN是一类由两个同时训练的模型组成的机器学习技术:一个是生成器,训练其生成伪数据:另一个是鉴别器,训练其从真实数据中识别伪数据. 生成(generative)一词预示着 ...

  7. 【数据结构】c语言实现集合的交并差运算

    待改写:存储数据类型int-->char 重复的元素可存储 功能上不完善 #include <stdio.h> #include <stdlib.h> typedef s ...

  8. SyntaxError: Non-UTF-8 code starting with '\xbb' in file D:\流畅学python\ex32.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

    1. 报错如下: SyntaxError: Non-UTF-8 code starting with '\xd3' in file D:\流畅学python\ex34.py on line 4, bu ...

  9. 初学python-day11 函数3

    函数 1. global关键字 修改全局变量,声明函数内外使用同一个变量 示例: 1 name = 'xiaoming' 2 3 def test(): 4 global name 5 name = ...

  10. CentOS 压缩解压

    目录 命令 tar gzip.gunzip bzip2.bunzip2 zip.unzip 命令组合 打包:将多个文件合成一个总的文件,这个总的文件通常称为"归档". 压缩:将一个 ...