gpt4 book ai didi

c++ - 旋转矩阵 n 次

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:31 29 4
gpt4 key购买 nike

当我遇到这个问题时,我正在解决 HackerRank 上的问题。

问题陈述

You are given a 2D matrix, a, of dimension MxN and a positive integer R. You have to rotate the matrix R times and print the resultant matrix. Rotation should be in anti-clockwise direction.

下图表示 4x5 矩阵的旋转。请注意,在一次旋转中,您只需将元素移动一步(为了更清楚,请参阅示例测试)。 enter image description here

保证M和N的最小值是偶数。

输入

First line contains three space separated integers, M, N and R, where M is the number of rows, N is number of columns in matrix, and R is the number of times the matrix has to be rotated. Then M lines follow, where each line contains N space separated positive integers. These M lines represent the matrix.

输出

Print the rotated matrix.

约束

2 <= M, N <= 300 
1 <= R <= 10^9
min(M, N) % 2 == 0
1 <= aij <= 108, where i ∈ [1..M] & j ∈ [1..N]'

我尝试做的是将圆圈存储在一维数组中。像这样。

 while(true)
{
k = 0;
for(int j = left; j <= right; ++j) {temp[k] = a[top][j]; ++k;}
top++;
if(top > down || left > right) break;

for(int i = top; i <= down; ++i) {temp[k] = a[i][right]; ++k;}
right--;
if(top > down || left > right) break;

for(int j = right; j >= left; --j) {temp[k] = a[down][j] ; ++k;}
down--;
if(top > down || left > right) break;

for(int i = down; i >= top; --i) {temp[k] = a[i][left]; ++k;}
left++;
if(top > down || left > right) break;
}

然后我可以通过计算其长度模 R 轻松旋转一维矩阵。但是我如何将它放回矩阵形式?再次使用循环可能会导致超时。

请不要提供代码,只提供建议。我想自己做。

创建的解决方案:

#include <iostream>
using namespace std;



int main() {
int m,n,r;
cin>>m>>n>>r;
int a[300][300];
for(int i = 0 ; i < m ; ++i){
for(int j = 0; j < n ; ++j)
cin>>a[i][j];
}

int left = 0;
int right = n-1;
int top = 0;
int down = m-1;
int tleft = 0;
int tright = n-1;
int ttop = 0;
int tdown = m-1;
int b[300][300];
int k,size;
int temp[1200];

while(true){
k=0;
for(int i = left; i <= right ; ++i)
{
temp[k] = a[top][i];
// cout<<temp[k]<<" ";
++k;
}
++top;

if(top > down || left > right)
break;

for(int i = top; i <= down ; ++i)
{
temp[k]=a[i][right];
// cout<<temp[k]<<" ";
++k;
}
--right;
if(top > down || left > right)
break;

for(int i = right; i >= left ; --i)
{
temp[k] = a[down][i];
// cout<<temp[k]<<" ";
++k;
}
--down;

if(top > down || left > right)
break;

for(int i = down; i >= top ; --i)
{
temp[k] = a[i][left];
// cout<<temp[k]<<" ";
++k;
}

++left;
if(top > down || left > right)
break;

//________________________________\\

size = k;
k=0;
// cout<<size<<endl;
for(int i = tleft; i <= tright ; ++i)
{
b[ttop][i] = temp[(k + (r%size))%size];
// cout<<(k + (r%size))%size<<" ";
// int index = (k + (r%size))%size;
// cout<<index;
++k;
}
++ttop;

for(int i = ttop; i <= tdown ; ++i)
{
b[i][tright]=temp[(k + (r%size))%size];
++k;
}
--tright;

for(int i = tright; i >= tleft ; --i)
{
b[tdown][i] = temp[(k + (r%size))%size];
++k;
}
--tdown;

for(int i = tdown; i >= ttop ; --i)
{
b[i][tleft] = temp[(k + (r%size))%size];
++k;
}

++tleft;
}

size=k;
k=0;
if(top != ttop){
for(int i = tleft; i <= tright ; ++i)
{
b[ttop][i] = temp[(k + (r%size))%size];
++k;
}
++ttop;
}
if(right!=tright){
for(int i = ttop; i <= tdown ; ++i)
{
b[i][tright]=temp[(k + (r%size))%size];
++k;
}
--tright;
}
if(down!=tdown){
for(int i = tright; i >= tleft ; --i)
{
b[tdown][i] = temp[(k + (r%size))%size];
++k;
}
--tdown;
}
if(left!=tleft){
for(int i = tdown; i >= ttop ; --i)
{
b[i][tleft] = temp[(k + (r%size))%size];
++k;
}

++tleft;
}
for(int i = 0 ; i < m ;++i){
for(int j = 0 ; j < n ;++j)
cout<<b[i][j]<<" ";
cout<<endl;
}

return 0;
}

最佳答案

你需要分解这个问题(让我想起gg和fb的一个面试问题):

  1. 先解决将序列旋转一个位置的问题
  2. 然后求解旋转一个序列N次
  3. 将每个“圆”或环建模为一个数组。您实际上可能需要也可能不需要存储在单独的数据中
  4. 遍历每个环并应用旋转算法

让我们考虑长度为 L 的数组的情况需要旋转R时间。观察如果RL 的倍数,数组将保持不变。也观察旋转 x向右旋转的次数与旋转 L - x 相同向左(反之亦然)。

  1. 因此你可以先设计一个算法,可以向左或向右旋转一次正好一个位置
  2. 减少旋转问题R向左旋转的次数R modulo L向左
  3. 如果你想进一步减少旋转的问题R modulo L向左向左旋转R modulo L或向右旋转 L - R modulo L .这意味着如果您有 100 个元素并且必须向左旋转 99 次,那么您最好向右旋转 1 次并完成它。

所以复杂度将为 O(圆圈数 x 圆圈长度 x 单次旋转成本)

就地数组意味着 O( min(N,m) * (N * M)^2 )

如果你使用一个双向链表作为临时存储,一个单一的旋转序列是通过移除前面并把它放在尾部来完成的(反之亦然向右旋转)。所以你可以做的是先将所有数据复制到链表中。运行单旋转算法 R modulo L次,将环位置上的链表复制回来,然后向右移动,直到处理完所有环。

  • 复制ring要列出的数据是 O(L), L <= N*M
  • 单次旋转成本为 O(1)
  • 所有旋转 R modulo LO(L)
  • 重复所有min(N,m) rings

使用备用双链表意味着复杂度为 O( min(N,m) * (N * M))

关于c++ - 旋转矩阵 n 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32071650/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com