簡體   English   中英

將M * N矩陣順時針旋轉90度,C ++

[英]Rotate M*N matrix 90 Degrees Clockwise ,C++

我正在嘗試旋轉chars的向量。

我進行了二維向量矩陣設置。 現在,矩陣從文件中獲取輸入,我使用vector.push_back(c)將字符添加到vvc中; vvc數組的一個例子是這樣的

aaaaa
azzza
azaza
azzza
azaaa
azaaa
azaaa
aaaaa

我有vvc設置,但是我試圖將其旋轉90度。 我將其逆時針旋轉90度,但我需要將其順時針旋轉90度。

截至目前,我的代碼執行此操作

90 counter clock
aaaaaaaa
azzzzzza
azazaaaa
azzzaaaa
aaaaaaaa

它通過這個循環來完成;

cout <<"\n90 counter clock"<<endl;
    for (size_t colNo = 0; colNo < kvsize2; colNo++)
    {
        for (const auto &row : twovector)
        {
            char colVal = row.at(colNo);
            cout << colVal;
        }
        cout << endl;
    }

我只是在學習向量及其范圍。 嘗試執行遞減循環幾乎可以工作,但總是讓我陷入段錯狀態。

我正在使用“已解決”

twovector.push_back(temp);

運用

twovector.insert(twovector.begin(),temp);

給我

90 counter clock aaaaaaaa azzzzzza aaaazaza aaaazzza aaaaaaaa

如果我做對了,而您想要做的就是順時針打印90度矩陣,請嘗試以下代碼:

for (int colNo = 0; colNo < vec[0].size(); colNo++)
{
    for (int i = vec.size() - 1; i >= 0; i--)
    {
        const auto& row = vec[i];
        int colVal = row.at(colNo);
        cout << colVal;
    }
    cout << endl;
}

解決問題的特定部分:

如果有人對如何旋轉M * N 2d向量數組有任何提示或建議

C ++擅長將算法與數據分離。

請注意,答案有點冗長,目的是為了教程。
讓我們開始 !!

我們需要rotate_2d_matrix_clockwise算法中的3個功能:

  • 它應該適用於所有數據類型,即intchardouble或任何用戶定義的類型。
  • 它應該與不同類型的容器一起使用,例如std::arraystd::vector
  • 它應該是可鏈接的,即用戶應該能夠對rotate_2d_matrix_clockwise返回的結果調用rotate_2d_matrix_clockwise ,以實現兩次旋轉。

一旦明確了需求,就可以為算法起草一些用例。

std::vector<std::vector<char>> data = { {'a', 'b', 'c', 'd'}, 
                                        {'e', 'f', 'g', 'h'}, 
                                        {'i', 'j', 'k', 'l'} };
rotate_2d_matrix_clockwise(data); // rotating 2d-matrix of vector<char>

std::array<std::array<int, 4>, 3> data2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// rotating 2d-matrix of array<int>, twice
rotate_2d_matrix_clockwise(rotate_2d_matrix_clockwise(data2))); 

因此,讓我們使用一些模板來創建通用的2d順時針旋轉功能。

我們的rotate_2d_matrix_clockwise將:

  • 接受original_matrix並返回一個新的rotated_matrix
  • 自動推斷尺寸,即傳遞給它的容器的M xN
  • 創建rotated_matrix並將它傳遞給一個輔助函數rotate_2d_matrix_clockwise_impl的實際工作將完成。

那么std::arrayrotate_2d_matrix_clockwise實現的外觀如何?

template<typename T, size_t M, size_t N>
auto rotate_2d_matrix_clockwise(std::array<std::array<T, M>, N> const & original_matrix)
    -> std::array<std::array<T, N>, M>
{
    std::array<std::array<T, N>, M> rotated_matrix;
    rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N); // rotate
    return rotated_matrix;
}

整潔而精確。
rotate_2d_matrix_clockwisestd::vectorrotate_2d_matrix_clockwise的實現有點混亂。

template<typename Matrix2D>
auto rotate_2d_matrix_clockwise(Matrix2D const & original_matrix) -> Matrix2D
{
    int const M = original_matrix[0].size(); // deduce M and N
    int const N = original_matrix.size();

    Matrix2D rotated_matrix; // vector has no form, hence we have to resize it for `N x M`
    rotated_matrix.resize(M);
    for (auto x = 0; x < M; ++x) {
        rotated_matrix[x].resize(N);
    }

    rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N); // rotate
    return rotated_matrix;
}

現在讓我們看一下實際的旋轉算法rotate_2d_matrix_clockwise_impl外觀。
應該注意的是,該算法與容器和/或所包含的數據無關。 它只是專注於旋轉。

template<typename OriginalMatrix2D, typename RotatedMatrix2D>
void rotate_2d_matrix_clockwise_impl(OriginalMatrix2D const & original_matrix,
                                     RotatedMatrix2D        & rotated_matrix,
                                     int              const M,
                                     int              const N)
{
    for (auto x = 0; x < N; ++x) {
        for (auto y = 0; y < M; ++y) {
            // Source : https://stackoverflow.com/questions/4780119/2d-euclidean-vector-rotations
            rotated_matrix[y][-x -1 +N] = original_matrix[x][y];
        }
    }
}

這是用C ++ 11編譯的完整示例。

#include <iostream>
#include <vector>
#include <array>

template<typename Matrix2D>
void print_matrix(Matrix2D const & vec)
{
    std::cout << "size of matrix is [" << vec[0].size() << " x " << vec.size() << "]\n";
    for (auto const & inner_vec : vec) {
        for (auto const & item : inner_vec) {
            std::cout << item << ", ";
        }
        std::cout << std::endl;
    }
}

template<typename OriginalMatrix2D, typename RotatedMatrix2D>
void rotate_2d_matrix_clockwise_impl(OriginalMatrix2D const & matrix,
                                     RotatedMatrix2D        & rotated_matrix,
                                     int              const M,
                                     int              const N)
{
    for (auto x = 0; x < N; ++x) {
        for (auto y = 0; y < M; ++y) {
            // Source : https://stackoverflow.com/questions/4780119/2d-euclidean-vector-rotations
            rotated_matrix[y][-x -1 +N] = matrix[x][y];
        }
    }
}

template<typename T, size_t M, size_t N>
auto rotate_2d_matrix_clockwise(std::array<std::array<T, M>, N> const & original_matrix)
    -> std::array<std::array<T, N>, M>
{
    std::array<std::array<T, N>, M> rotated_matrix;
    rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N);
    return rotated_matrix;
}

template<typename Matrix2D>
auto rotate_2d_matrix_clockwise(Matrix2D const & original_matrix) -> Matrix2D
{
    int const M = original_matrix[0].size();
    int const N = original_matrix.size();
    Matrix2D rotated_matrix;
    rotated_matrix.resize(M);
    for (auto x = 0; x < M; ++x) {
        rotated_matrix[x].resize(N);
    }
    rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N);
    return rotated_matrix;
}


int main()
{
    std::array<std::array<int, 4>, 3> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    std::cout << "\nBefore Rotation :\n";
    print_matrix(data);
    std::cout << "\nAfter 2nd Clockwise Rotation :\n";
    print_matrix(rotate_2d_matrix_clockwise(rotate_2d_matrix_clockwise(data)));

    std::vector<std::vector<char>> data2 = { {'a', 'b', 'c', 'd'}, {'e', 'f', 'g', 'h'}, {'i', 'j', 'k', 'l'}};
    std::cout << "Before Rotation :\n";
    print_matrix(data2);
    std::cout << "\nAfter Clockwise Rotation :\n";
    print_matrix(rotate_2d_matrix_clockwise(data2));

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM