BEATS 100% 🔥🔥 | Easy Solution in C++
PLEASE UPVOTE\u2B06\uFE0F THIS POST !\n\n\n\n\n---\n\n\n# Intuition\nThe problem requires traversing a matrix in a spiral order, starting from a specific cell a
# PLEASE UPVOTE\u2B06\uFE0F THIS POST !\n\n![Screenshot 2024-08-08 172504.png](https://assets.leetcode.com/users/images/d9502894-d780-4b44-b963-97c2ba206751_1723118196.6394975.png)\n\n\n---\n\n\n# Intuition\nThe problem requires traversing a matrix in a spiral order, starting from a specific cell and moving outwards. The main idea is to simulate the spiral movement by alternating between moving right, down, left, and up, while correctly handling the boundaries.\n\n---\n\n\n# Approach\n1. **Initialization**: Start from the initial position `(rStart, cStart)` and add it to the result list.\n2. **Movement Simulation**: \n   - Use boolean flags `right` and `down` to control the direction of movement.\n   - Alternate between moving horizontally (right or left) and vertically (down or up).\n   - After every two direction changes (right-down, left-up), increase the length of the spiral side.\n3. **Boundary Check**: For each step in the spiral, ensure the current cell is within the matrix bounds before adding it to the result list.\n\n---\n\n\n# Complexity\n- **Time complexity**: \n  - The algorithm visits each cell in the matrix exactly once, so the time complexity is $O(rows * cols)$.\n- **Space complexity**: \n  - The space complexity is $O(rows * cols)$ for storing the result.\n\n---\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        bool down = true, right = true;   \n        int len = 2;\n        int n = rows, m = cols, r = rStart, c = cStart;\n        vector<vector<int>> ans(1, vector<int>({r, c}));\n\n        int set1 = max(c+1, m-c+1), set2 = max(r+1, n-r+1);\n        while (len <= max(2*set1, 2*set2)) {\n            if (right && down) {\n                int lim = c + len - 1;\n                while (c < lim) {\n                    c++;\n                    if (r >= 0 && c >= 0 && r < n && c < m) {\n                        ans.push_back({r, c});\n                    }\n                }\n                right = !right;\n            } else if (!right && down) {\n                int lim = r + len - 1;\n                while (r < lim) {\n                    r++;\n                    if (r >= 0 && c >= 0 && r < n && c < m) {\n                        ans.push_back({r, c});\n                    }\n                }\n                down = !down;\n                len++;\n            } else if (!right && !down) {\n                int lim = c - len + 1;\n                while (c > lim) {\n                    c--;\n                    if (r >= 0 && c >= 0 && r < n && c < m) {\n                        ans.push_back({r, c});\n                    }\n                }\n                right = !right;\n            } else {\n                int lim = r - len + 1;\n                while (r > lim) {\n                    r--;\n                    if (r >= 0 && c >= 0 && r < n && c < m) {\n                        ans.push_back({r, c});\n                    }\n                }\n                down = !down;\n                len++;\n            }\n        }\n        return ans;\n    }\n};
['Array', 'Matrix', 'Simulation', 'C++']