首页 > 其他分享 >[ABC375C] Spiral Rotation

[ABC375C] Spiral Rotation

时间:2024-10-18 15:12:19浏览次数:1  
标签:le 题意 矩阵 Spiral 3005 ABC375C Rotation

[ABC375C] Spiral Rotation

题意

给出一个边长为偶数 \(n\) 的只由 #. 组成的矩阵。

你需要按顺序对于 \(i=1,2,\cdots,\frac{n}{2}\) 将满足 \(i\le x,y\le n+1-i\) 的单元格 \((y,n+1−x)\) 替换成单元格 \((x,y)\) 的字符,问操作完后的矩阵。

\(2\le n\le 3000\)。

思路

C题最恶心的一集。

如果你对着题意直接模拟的话复杂度为 \(O(n^3)\),显然不能通过,但你一直对着题目看就会发现你也看不出什么,最起码我是看不出来。

于是通过对暴力程序的亿点点找规律可以发现题意可以转换成:

将正方形矩阵分成 \(\frac{n}{2}\) 圈,例如 \(n=8\) 如下:

11111111
12222221
12333321
12344321
12344321
12333321
12222221
11111111

对于第 \(i\) 圈,将它顺时针旋转 \(i\times 90°\),问操作完后的矩阵。

转换完后思路就很清楚了,对于第 \((i,j)\) 个单元格可以通过 \(\min\{i,n-i+1,j,n-j+1\}\) 算出在第几个圈,然后直接模拟旋转即可。

代码

#include <bits/stdc++.h>
using namespace std;
int n;
char s[3005][3005],s2[3005][3005];
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>s[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
            //转360°可以视为转0°
			if(min(min(i,n-i+1),min(j,n-j+1))%4==1)
				s2[j][n-i+1]=s[i][j];
			else if(min(min(i,n-i+1),min(j,n-j+1))%4==2)
				s2[n-i+1][n-j+1]=s[i][j];
			else if(min(min(i,n-i+1),min(j,n-j+1))%4==3)
				s2[n-j+1][i]=s[i][j];
			else
				s2[i][j]=s[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cout<<s2[i][j];
		}
		cout<<endl;
	}
	return 0; 
}

标签:le,题意,矩阵,Spiral,3005,ABC375C,Rotation
From: https://www.cnblogs.com/WuMin4/p/18474338

相关文章

  • [LeetCode] 885. Spiral Matrix III
    Youstartatthecell(rStart,cStart)ofanrowsxcolsgridfacingeast.Thenorthwestcornerisatthefirstrowandcolumninthegrid,andthesoutheastcornerisatthelastrowandcolumn.Youwillwalkinaclockwisespiralshapetovisiteverypo......
  • GENG3405 Stress rotation Friction
    GENG3405.Part 1–2024Submission (LMS) by 5 pm 16 September 2024.Thisisagroupassignment. Please do FRUITTheassignmenttestsbothyourabilitytodothe tasks and to explain how you did them. Instructions1.  A1isobtainedas......
  • 【AD9361 数字基带】多片基带内FPGA补偿 I/Q Rotation
    I/Q旋转Rotation在许多多通道射频系统中,如AD-FMCOMMS5,甚至在AD-FMCOMMS2、AD-FMCOMMS3上,都需要测量或校正两个复数(I/Q)RF信号之间的相位差。从纯粹的数学描述来看,单个正弦波没有相位,一个相位只能在两个不同的正弦波之间发展。增加复杂性的是,我们没有一个单一的真实......
  • LeetCode | 59 SpiralMatrixII
    主类https://github.com/dolphinmind/datastructure/tree/datastructure-array-02循环不变量原则,保证问题边界的逻辑一致性(从二分法的启发)初始位置旋转圈数奇偶性四条边的边界逻辑offsetpackagecom.github.dolphinmind.array;/***@authordolphinmind*@C......
  • opencascade AIS_RubberBand AIS_RotationMode源码学习
    //!相机旋转类型Camerarotationmode.enumAIS_RotationMode{AIS_RotationMode_BndBoxActive,//!<defaultOCCTrotationAIS_RotationMode_PickLast,//!<rotatearoundlastpickedpointAIS_RotationMode_PickCenter,//!<rotatearoundpointatthecenter......
  • 0054_Spiral-Matrix【M】
    JY:矩阵螺旋式遍历(一圈圈螺旋式、从外到里)参考:0054_Spiral-Matrix【M】·语雀1、基于矩阵4个边界指针实现顺时针顺序一层层遍历,共需遍历math.ceil(min(m,n)/2)圈fromtypingimportList,DictclassSolution:defspiralOrder(self,matrix:List[Lis......
  • 0059_Spiral-Matrix-ii【M】
    JY:矩阵的螺旋遍历相似题:0054_Spiral-Matrix【M】参考:0059_Spiral-Matrix-ii【M】 1、基于4个边界指针参考0054_Spiral-Matrix【M】中的解法2classSolution:defgenerateMatrix(self,n:int)->[[int]]:left,right,top,bottom=0,n-1,0,n......
  • swiftui中使用scaleEffect和rotationEffect实现缩放和旋转效果
    在SwiftUI中,你可以使用.scaleEffect()和.rotationEffect()来实现缩放和旋转动画,缩放和旋转的内容可以是图片,文字等view视图。scaleEffect可以实现缩放效果,配合动画可以实现好看的过度效果,其中的参数是缩放的倍数,1表示原本大小,大于1表示放大,小于1表示缩小。示例代码:Text("He......
  • linux-c-log-rotation-scheme
    linux-c-log-rotation-scheme#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>voidlogworker(){ino_tinode=0;FILE*logfile;logfile=fopen(logfilename,"a+");while(running){......
  • LeetCode 2326. Spiral Matrix IV
    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iv/description/题目:Youaregiventwointegers m and n,whichrepresentthedimensionsofamatrix.Youarealsogiventhe head ofalinkedlistofintegers.Generatean mxn matrixthatconta......