Simulating Easy Solution that Beats 100%
\n\n# Approach\n1. initialize i,j variable which is the point in matrix from which we are going to start traversing.i,j = rStart, cStart\n2. initialize a dirict
![Untitledvideo-MadewithClipchamp-ezgif.com-video-to-gif-converter.gif](https://assets.leetcode.com/users/images/e33fcee3-56d1-4798-8435-5bddf22e165c_1723084161.2497864.gif)\n\n# Approach\n1. initialize i,j variable which is the point in matrix from which we are going to start traversing.`i,j = rStart, cStart`\n2. initialize a diriction movement `diri, dirj to 0, 1`  which is moving next cell to our current cell.\n3. initialize other variables: \n    - **twice**: Counter to track the need to change direction.\n    - **res**: Initialize the result list.\n    - **moves**: Number of steps to move in the current direction.\n    - **next_moves**: Number of steps to move after changing direction twice. \n4. now do a while loop till the last cell of matrix i.e. rows * cols\n    - if our i and j are still inside the matrix append the index in res list.\n    - now move in current direction and decrement the move by -1 whith every iteration.\n    - if `moves == 0` it means that now we have to change the direction to 90 degrees clockwise.\n    `i += diri\n    j += dirj`\n    - decrement twice by 1\n    - If twice reaches zero, it indicates that we\'ve changed direction twice (completed one spiral cycle). Reset twice to 2, update moves to next_moves, and increment next_moves for the next cycle.\n    - If twice is not zero, set moves to next_moves - 1 for the next half-cycle indicating that we have to visit n -1 cells when twice is not 0\n\n# Code\n```python []\nclass Solution(object):\n    def spiralMatrixIII(self, rows, cols, rStart, cStart):\n        i,j = rStart, cStart\n\n        diri, dirj = 0, 1 # directions to move\n        twice = 2\n        res = []\n        moves = 1\n        next_moves = 2\n\n        while len(res) < (rows * cols):\n            if (-1 < i < rows) and ( -1 < j < cols):\n                res.append([i,j])\n            \n            i += diri\n            j += dirj\n            moves -= 1\n            if moves == 0:\n                diri, dirj = dirj, -diri # 90 deg Clockwise\n                twice -= 1\n                if twice == 0:\n                    twice = 2\n                    moves = next_moves\n                    next_moves += 1\n                else:\n                    moves = next_moves - 1\n        return res\n```\n```C++ []\nclass Solution {\npublic:\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int i = rStart, j = cStart;\n        int diri = 0, dirj = 1; // directions to move\n        int twice = 2;\n        vector<vector<int>> res;\n        int moves = 1;\n        int next_moves = 2;\n\n        while (res.size() < rows * cols) {\n            if (i >= 0 && i < rows && j >= 0 && j < cols) {\n                res.push_back({i, j});\n            }\n\n            i += diri;\n            j += dirj;\n            moves -= 1;\n            if (moves == 0) {\n                int temp = diri;\n                diri = dirj;\n                dirj = -temp; // right direction\n                twice -= 1;\n                if (twice == 0) {\n                    twice = 2;\n                    moves = next_moves;\n                    next_moves += 1;\n                } else {\n                    moves = next_moves - 1;\n                }\n            }\n        }\n        return res;\n    }\n};\n```\n![unnamed.jpg](https://assets.leetcode.com/users/images/c2e7693c-e3df-4386-a963-ce7516af01ef_1723082538.6367753.jpeg)\n
['Array', 'Matrix', 'Simulation', 'Python', 'C++', 'Python3']