Easiest Simulation Approach Detailed Explanation🏆✨
Spiral Matrix III\n\n## Problem Description\nGiven a matrix of size rows x cols, you are required to traverse all cells in the matrix in a spiral order starting
# Spiral Matrix III\n\n## Problem Description\nGiven a matrix of size `rows x cols`, you are required to traverse all cells in the matrix in a spiral order starting from a given initial position `(rStart, cStart)`. The traversal must expand outward in a spiral, and you must return a list of all the cells in the order they are visited.\n\n## Intuition\nThe problem is about simulating a spiral traversal starting from a specific position in a matrix. The spiral expands outward, and our task is to correctly order the cells according to this pattern. The traversal alternates directions (right, down, left, up), and the number of steps increases after every two direction changes.\n\n## Approach\n1. **Initialization**:\n   - Store the starting cell `(rStart, cStart)` in the result list.\n   - Define the movement directions in the order: right, down, left, up.\n\n2. **Movement Simulation**:\n   - Use a loop to control the traversal. The number of steps increases every two direction changes.\n   - For each direction, move step by step and add the current position to the result if it lies within the matrix bounds.\n\n3. **Direction Change**:\n   - Change the direction after completing the required number of steps in the current direction.\n\n4. **Termination**:\n   - Stop once all the cells in the matrix have been visited.\n\n## Complexity\n\n- **Time Complexity**: \n  - The algorithm runs in \\(O(\\text{rows} \\times \\text{cols})\\) since every cell in the matrix is visited exactly once.\n\n- **Space Complexity**: \n  - The space complexity is \\(O(\\text{rows} \\times \\text{cols})\\) because we store the sequence of visited cells.\n\n## Code\n```cpp\nclass Solution {\npublic:\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        \n        // To store the path of cells visited\n        vector<vector<int>> paths;\n        // Start from the initial position\n        paths.push_back({rStart, cStart});\n\n        // Directions: right, down, left, up\n        vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n        \n        int steps = 0, direction = 0;\n\n        // Loop until all cells are visited\n        while (paths.size() != rows * cols) {\n\n            // Increase steps after completing a right or left move\n            if (direction == 0 || direction == 2) {\n                steps++;\n            }\n\n            // Move `steps` number of cells in the current direction\n            for (int i = 0; i < steps; i++) {\n                rStart += directions[direction].first;\n                cStart += directions[direction].second;\n\n                // Check if the new position is within the bounds of the matrix\n                if (rStart >= 0 && cStart >= 0 && rStart < rows && cStart < cols) {\n                    paths.push_back({rStart, cStart});\n                }\n            }\n\n            // Move to the next direction in the sequence\n            direction = (direction + 1) % 4;\n        }\n\n        return paths;\n    }\n};\n```
['Array', 'Matrix', 'Simulation', 'C++']