有人能在我的代码中找到Matrix Rotation中的错误吗?

时间:2015-08-16 10:06:23

标签: c++ algorithm visual-c++ matrix

我正在尝试进行hackerrank Matrix Rotation挑战。我的代码中有一个我无法找到的错误。该代码应该旋转测试矩阵的所有外环和内环r次数(1 <= r <= 10 E 9)。如果我没有对r执行模数,则代码运行正常,除了r的较高值值,它在hackerrank服务器上超时。如果我在r上执行模数,则代码将失败测试用例,其中r> =(r%number_of _elements_in_outer_loop)。我找不到这个bug。提前感谢您的回复。以下是代码(在Visual Studio 2015中进行测试)。对要求的黑客挑战是here

// MatrixRotation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
//  int i, j, k;
    int m, n, r;
    int rotations, rot;
    int m_start, m_end, m_len;
    int n_start, n_end, n_len;
    int m_curr, n_curr;

//-------------------------------------------------------------------------
// For hackerrank testing
//-------------------------------------------------------------------------

    int i, j;

    for (i = 1; i <= 3; i++)
    {

        if (i == 1) std::cin >> m;
        else if (i == 2) std::cin >> n;
        else std::cin >> r;

    }

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

    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            std::cin >> matrix[i][j];


    //-------------------------------------------------------------------------
    // For local machine in Visual Studio testing
    //-------------------------------------------------------------------------

    /*

    int i, j, k;

    m = 10;
    n = 8;
    r = 40;

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

    k = 1;
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
        {
            matrix[i][j] = k;
            k += 1;
        }

    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
        {
            std::cout << matrix[i][j] << " ";

            if (j == n - 1)
                std::cout << endl;

        }

    std::cout << endl;

    */

    //-------------------------------------------------------------------------
    // Begin computations
    //-------------------------------------------------------------------------

    int outer_loop_rotation;

    outer_loop_rotation = (n - 1)  + (m - 1) + (n - 1) + (m - 1)  ;

    rot = r % outer_loop_rotation;

//  std::cout << endl << endl << "outer loop rotation = " << outer_loop_rotation << endl << endl << "rot = " << rot << endl << endl;

    for (rotations = 1; rotations <= rot; rotations++)
    {

        m_start = 0;
        m_end = m - 1;

        n_start = 0;
        n_end = n - 1;

        m_len = m_end - m_start;
        n_len = n_end - n_start;


        // Following while loop is 1 rotation for all loops

        while (m_len >= 1 && n_len >= 1)
        {

            int loop_start = matrix[m_start][n_start];

            // Following for loop is for row start

            m_curr = m_start;
            n_curr = n_start;

            for (i = 1; i <= n_len  ; i++)
            {

                matrix[m_curr][n_curr] = matrix[m_curr][n_curr + 1];
                n_curr += 1;

            }

            // Following for loop is for col end

            m_curr = m_start;
            n_curr = n_end;

            for (i = 1; i <= m_len  ; i++)
            {

                matrix[m_curr][n_curr] = matrix[m_curr + 1][n_curr];
                m_curr += 1;

            }

            // Following for loop is for row end

            m_curr = m_end;
            n_curr = n_end;

            for (i = 1; i <= n_len  ; i++)
            {

                matrix[m_curr][n_curr] = matrix[m_curr][n_curr - 1];
                n_curr -= 1;

            }

            // Following for loop is for col start

            m_curr = m_end;
            n_curr = n_start;

            for (i = 1; i <= m_len   ; i++)
            {
                if (i < m_len )
                {
                    matrix[m_curr][n_curr] = matrix[m_curr - 1][n_curr];
                    m_curr -= 1;


                }

                else
                {

                    matrix[m_curr][n_curr] = loop_start;


                }

            }

            m_start += 1;
            m_end -= 1;

            n_start += 1;
            n_end -= 1;

            m_len = m_end - m_start;
            n_len = n_end - n_start;


        } // End while loop

    } // End for loop

    // End computations, now output to command line

    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
        {
            std::cout << matrix[i][j] << " ";

            if (j == n - 1)
                std::cout << endl;

        }


    return 0;
}

0 个答案:

没有答案