xtu oj矩阵

一开始

复制代码
#include<stdio.h>
int main(){
	int K;
	scanf("%d",&K);
	while(K--){
		int n,m;
		scanf("%d %d",&n,&m);
		int str[n+2][n+2];
		int t=1;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				str[i][j]=t++;
			}
		}//输入
	//	int flag=0;
		getchar();
		while(m--){
			char s;
			int x,y;
			scanf("%c %d %d",&s,&x,&y);
		//	printf("%c",s);
			if(s=='L'){
				while(y--){
					int cup=str[x-1][0];
					for(int i=1;i<n;i++){
						str[x-1][i-1]=str[x-1][i];
					}
					str[x-1][n-1]=cup;
					//flag=1;
				}
			}else if(s=='R'){
				while(y--){
					int cup=str[x-1][n-1];
					for(int i=n-2;i>=0;i--){
						str[x-1][i+1]=str[x-1][i];
					}
					str[x-1][0]=cup;
				}
			}else if(s=='U'){
				while(y--){
					int cup=str[0][x-1];
					for(int i=1;i<n;i++){
						str[i-1][x-1]=str[i][x-1];
					}
					str[n-1][x-1]=cup;
				}
			}else if(s=='D'){
				while(y--){
					int cup=str[n-1][x-1];
					for(int i=n-2;i>=0;i--){
						str[i+1][x-1]=str[i][x-1];
					}
					str[0][x-1]=cup;
				}
			}
			
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				printf("%d ",str[i][j]);
			}
		}
	//	printf("  flag=%d\n",flag);
		printf("\n");
	}
}

显然的,这个代码时间超限了,用空间换时间,所以构建一个中间数组temp\[\],可以一次执行y次移动,然后再存储在arr\[\]中

复制代码
#include <stdio.h>
#include <string.h>

#define MAXN 10

// 循环左移数组 arr 长度为 n,移动 y 次
void shift_left(int arr[], int n, int y) {
	y %= n;
	if (y == 0) return;
	int temp[MAXN];
	for (int i = 0; i < n; i++) {
		temp[i] = arr[(i + y) % n];
	}
	for (int i = 0; i < n; i++) {
		arr[i] = temp[i];
	}
}

// 循环右移数组 arr 长度为 n,移动 y 次
void shift_right(int arr[], int n, int y) {
	y %= n;
	if (y == 0) return;
	int temp[MAXN];
	for (int i = 0; i < n; i++) {
		temp[i] = arr[(i - y + n) % n];
	}
	for (int i = 0; i < n; i++) {
		arr[i] = temp[i];
	}
}

int main() {
	int K;
	scanf("%d", &K);
	
	while (K--) {
		int n, m;
		scanf("%d %d", &n, &m);
		
		// 初始化矩阵
		int mat[MAXN][MAXN];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				mat[i][j] = i * n + j + 1;
			}
		}
		
		// 处理每条指令
		for (int i = 0; i < m; i++) {
			char cmd[2];
			int x, y;
			scanf("%s %d %d", cmd, &x, &y);
			x--; // 转为0-based索引
			
			if (cmd[0] == 'L') {
				shift_left(mat[x], n, y);
			} else if (cmd[0] == 'R') {
				shift_right(mat[x], n, y);
			} else if (cmd[0] == 'U') {
				// 提取第x列
				int col[MAXN];
				for (int r = 0; r < n; r++) {
					col[r] = mat[r][x];
				}
				shift_left(col, n, y); // 上移 = 列左移
				for (int r = 0; r < n; r++) {
					mat[r][x] = col[r];
				}
			} else if (cmd[0] == 'D') {
				int col[MAXN];
				for (int r = 0; r < n; r++) {
					col[r] = mat[r][x];
				}
				shift_right(col, n, y); // 下移 = 列右移
				for (int r = 0; r < n; r++) {
					mat[r][x] = col[r];
				}
			}
		}
		
		// 按行输出结果
		int first = 1;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (!first) printf(" ");
				printf("%d", mat[i][j]);
				first = 0;
			}
		}
		printf("\n");
	}
	
	return 0;
}
相关推荐
JieE21213 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2121 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack202 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树2 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2123 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2123 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术3 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦3 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050733 天前
(一)小红的数组操作
算法·编程语言