如何顺时针旋转矩阵?

发布于 2025-01-18 01:32:20 字数 1231 浏览 0 评论 0原文

这是一个示例:

1 2 3
4 5 6
7 8 9

旋转后:

4 1 2
7 5 3
8 9 6

4x4示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12 5x5是相似的

,我可以旋转90°,但在此练习中不是正确的 矩阵的一个元素仅移动一次 我已经尝试了2个小时以找到该算法,但是我的代码不起作用。 请帮助我解决这个问题

#include <iostream>
using namespace std;

void input(int **a,int row,int column)
{
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
            cin>>a[i][j];
    }
}

void output(int **a,int row,int column)
{
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
            cout<<a[i][j]<< " ";
        cout<<endl;
    }
}

void rotate(int **a,int **b,int m,int n)
{
    for(int i=0; i<n; i++)
    {
        int k=m-1;
        for(int j=0; j<m; j++)
        {
            b[i][j]=a[k][i];
            k--;
        }
    }
}

int main()
{
    int m,n;
    cin>>m>>n;

    int **a=new int*[m];
    for(int i=0; i<m; i++)
        a[i]=new int[n];

    int **b=new int*[n];
    for(int i=0; i<n; i++)
        b[i]=new int[m];

    input(a,m,n);
    rotate(a,b,m,n);
    output(b,n,m);
    return 0;
}

Here's an example:

1 2 3
4 5 6
7 8 9

After rotating:

4 1 2
7 5 3
8 9 6

4x4 example:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12
5x5 is similar

I can rotate 90° but that's not true in this exercise
A element of the matrix just move once
I have tried for 2 hours to find the algorithm but my code doesn't work.
Please help me solve this problem

#include <iostream>
using namespace std;

void input(int **a,int row,int column)
{
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
            cin>>a[i][j];
    }
}

void output(int **a,int row,int column)
{
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
            cout<<a[i][j]<< " ";
        cout<<endl;
    }
}

void rotate(int **a,int **b,int m,int n)
{
    for(int i=0; i<n; i++)
    {
        int k=m-1;
        for(int j=0; j<m; j++)
        {
            b[i][j]=a[k][i];
            k--;
        }
    }
}

int main()
{
    int m,n;
    cin>>m>>n;

    int **a=new int*[m];
    for(int i=0; i<m; i++)
        a[i]=new int[n];

    int **b=new int*[n];
    for(int i=0; i<n; i++)
        b[i]=new int[m];

    input(a,m,n);
    rotate(a,b,m,n);
    output(b,n,m);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2025-01-25 01:32:20

虽然我没有找到这种旋转的单一数学公式,但我使用四个小循环来完成它。

此外,我的程序支持任意大小的矩阵 NxM,不仅是正方形,而且不仅是奇数大小。

为了简单地运行以下代码片段而不是从 std::cin 读取矩阵,我将矩阵元素的值内联为代码中的常量。

另外,为了简单起见,我使用 std::vector 不执行任何普通矩阵的新操作/删除操作。如何将我的解决方案应用于普通数组的情况是非常明显的。

在线尝试!

#include <vector>
#include <iomanip>
#include <iostream>

void output(auto const & a) {
    for (size_t i = 0; i < a.size(); ++i) {
        for (size_t j = 0; j < a[i].size(); ++j)
            std::cout << std::setw(2) << a[i][j] << " ";
        std::cout << std::endl;
    }
}

void rotate(auto & a) {
    int i_first = 0, i_last = a.size() - 1,
        j_first = 0, j_last = a[0].size() - 1;
    auto b = a;
    while (i_first <= i_last && j_first <= j_last) {
        for (int j = j_first + 1; j <= j_last; ++j)
            b[i_first][j] = a[i_first][j - 1];
        for (int i = i_first + 1; i <= i_last; ++i)
            b[i][j_last] = a[i - 1][j_last];
        for (int j = j_last - 1; j >= j_first; --j)
            b[i_last][j] = a[i_last][j + 1];
        for (int i = i_last - 1; i >= i_first; --i)
            b[i][j_first] = a[i + 1][j_first];
        ++i_first; --i_last;
        ++j_first; --j_last;
    }
    a = b;
}

int main() {
    std::vector<std::vector<int>> a = {
        { 0,  1,  2,  3,  4,  5},
        { 6,  7,  8,  9, 10, 11},
        {12, 13, 14, 15, 16, 17},
        {18, 19, 20, 21, 22, 23},
        {24, 25, 26, 27, 28, 29},
    };
    std::cout << "Before:" << std::endl;
    output(a);
    rotate(a);
    std::cout << "After:" << std::endl;
    output(a);
}

输出:

Before:
 0  1  2  3  4  5 
 6  7  8  9 10 11 
12 13 14 15 16 17 
18 19 20 21 22 23 
24 25 26 27 28 29 

After:
 6  0  1  2  3  4 
12 13  7  8  9  5 
18 19 15 14 10 11 
24 20 21 22 16 17 
25 26 27 28 29 23 

Although I didn't find some single mathematical formula for this rotation, I did it using four tiny loops.

Also my program supports any arbitrary size of matrix NxM, not only square, and not only odd sizes.

For simplicity of running following code snippet instead of reading matrix from std::cin, I inlined values of matrix elements as constant in code.

Also for simplicity I used std::vector not to do any new/delete operations of plain matrix. It is quite obvious how to adopt my solution to your case of plain array.

Try it online!

#include <vector>
#include <iomanip>
#include <iostream>

void output(auto const & a) {
    for (size_t i = 0; i < a.size(); ++i) {
        for (size_t j = 0; j < a[i].size(); ++j)
            std::cout << std::setw(2) << a[i][j] << " ";
        std::cout << std::endl;
    }
}

void rotate(auto & a) {
    int i_first = 0, i_last = a.size() - 1,
        j_first = 0, j_last = a[0].size() - 1;
    auto b = a;
    while (i_first <= i_last && j_first <= j_last) {
        for (int j = j_first + 1; j <= j_last; ++j)
            b[i_first][j] = a[i_first][j - 1];
        for (int i = i_first + 1; i <= i_last; ++i)
            b[i][j_last] = a[i - 1][j_last];
        for (int j = j_last - 1; j >= j_first; --j)
            b[i_last][j] = a[i_last][j + 1];
        for (int i = i_last - 1; i >= i_first; --i)
            b[i][j_first] = a[i + 1][j_first];
        ++i_first; --i_last;
        ++j_first; --j_last;
    }
    a = b;
}

int main() {
    std::vector<std::vector<int>> a = {
        { 0,  1,  2,  3,  4,  5},
        { 6,  7,  8,  9, 10, 11},
        {12, 13, 14, 15, 16, 17},
        {18, 19, 20, 21, 22, 23},
        {24, 25, 26, 27, 28, 29},
    };
    std::cout << "Before:" << std::endl;
    output(a);
    rotate(a);
    std::cout << "After:" << std::endl;
    output(a);
}

Output:

Before:
 0  1  2  3  4  5 
 6  7  8  9 10 11 
12 13 14 15 16 17 
18 19 20 21 22 23 
24 25 26 27 28 29 

After:
 6  0  1  2  3  4 
12 13  7  8  9  5 
18 19 15 14 10 11 
24 20 21 22 16 17 
25 26 27 28 29 23 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文