Spiral Matrix Traversal from a Given Start Point
Intuition\nTo solve this problem, I visualized the grid traversal as a series of expanding rectangles, starting from the initial position. By systematically inc
# Intuition\nTo solve this problem, I visualized the grid traversal as a series of expanding rectangles, starting from the initial position. By systematically increasing the number of steps taken in each direction (right, down, left, up) as we spiral outward, we can cover every cell.\n\nAt each step, I check whether the current position is within the grid\'s boundaries. If it is, I add the coordinates to the result. The process continues until all cells in the grid have been visited.\n\nThe algorithm efficiently handles the traversal by expanding the boundary of movement after completing each cycle (right -> down -> left -> up), ensuring that the entire grid is covered in a spiral manner.\n\n# Approach\nInitialization:\nStart by initializing your current position at (rStart, cStart).\nCreate a result vector ans and add the starting position to it.\nInitialize four variables\u2014right, bottom, left, and top\u2014to control the number of steps you will take in each respective direction (right, down, left, up).\n\nSpiral Traversal:\nUse a while loop to continue traversing until all cells in the grid have been visited.\nMove Right: Increment the column index (currCol) by 1 for right number of steps. If the new position is within the grid boundaries, add it to ans.\nMove Down: Increment the row index (currRow) by 1 for bottom number of steps, checking and adding valid positions.\nMove Left: Decrement the column index (currCol) by 1 for left number of steps, checking and adding valid positions.\nMove Up: Decrement the row index (currRow) by 1 for top number of steps, checking and adding valid positions.\n\nExpand the Spiral:\nAfter completing each direction (right -> down -> left -> up), increase the number of steps for the corresponding direction (e.g., right and left by 2, top and bottom by 2) to expand the spiral outward.\n\nBoundary Check:\nFor each movement, ensure that the position is within the grid\'s bounds (i.e., 0 <= currRow < rows and 0 <= currCol < cols). If the position is valid, add it to the result vector ans.\n\nCompletion:\nThe loop continues until all cells have been added to ans. Once done, return ans as the final result.\n\n# Complexity\n- Time complexity:\n    O(rows * cols)\n\n- Space complexity:\n    O(rows * cols)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart,\n                                        int cStart) {\n        vector<vector<int>> ans;\n        int currRow = rStart;\n        int currCol = cStart;\n        int right = 1;\n        int bottom = 1;\n        int left = 2;\n        int top = 2;\n        int cnt = rows * cols;\n        ans.push_back({currRow, currCol});\n        cnt--;\n        if (cnt == 0) {\n            return ans;\n        }\n        while (cnt > 0) {\n            for (int i = 1; i <= right; i++) {\n                currCol += 1;\n                if (currRow < rows && currRow >= 0 && currCol < cols &&\n                    currCol >= 0) {\n                    ans.push_back({currRow, currCol});\n                    cnt--;\n                    if (cnt == 0) {\n                        return ans;\n                    }\n                }\n            }\n            right += 2;\n            for (int i = 1; i <= bottom; i++) {\n                currRow += 1;\n                if (currRow < rows && currRow >= 0 && currCol < cols &&\n                    currCol >= 0) {\n                    ans.push_back({currRow, currCol});\n                    cnt--;\n                    if (cnt == 0) {\n                        return ans;\n                    }\n                }\n            }\n            bottom += 2;\n            for (int i = 1; i <= left; i++) {\n                currCol -= 1;\n                if (currRow < rows && currRow >= 0 && currCol < cols &&\n                    currCol >= 0) {\n                    ans.push_back({currRow, currCol});\n                    cnt--;\n                    if (cnt == 0) {\n                        return ans;\n                    }\n                }\n            }\n            left += 2;\n            for (int i = 1; i <= top; i++) {\n                currRow -= 1;\n                if (currRow < rows && currRow >= 0 && currCol < cols &&\n                    currCol >= 0) {\n                    ans.push_back({currRow, currCol});\n                    cnt--;\n                    if (cnt == 0) {\n                        return ans;\n                    }\n                }\n            }\n            top += 2;\n        }\n        return ans;\n    }\n};\n```
['C++']