Easy to UnderStand || Begginer Friendly
Intuition\n\nThe problem involves traversing a matrix in a spiral order starting from a given position. The traversal must continue until all cells in the matri
# Intuition\n\nThe problem involves traversing a matrix in a spiral order starting from a given position. The traversal must continue until all cells in the matrix are visited. \n\n# Approach\n\n1. **Initialization**: \n   - Create a list to store the result.\n   - Use variables to keep track of the current position `(x, y)`, the number of steps taken in each direction, and the total number of steps.\n   \n2. **Spiral Movement**:\n   - Move right for a certain number of steps, then increase the width for the next right movement.\n   - Move down for a certain number of steps, then increase the height for the next down movement.\n   - Move left for a certain number of steps, then increase the width for the next left movement.\n   - Move up for a certain number of steps, then increase the height for the next up movement.\n\n3. **Boundary Check**:\n   - At each step, check if the current position is within the matrix boundaries. If it is, add it to the result list.\n   \n4. **Repeat**:\n   - Continue the above steps until all cells are visited.\n\n# Complexity\n- **Time complexity**: $$O(n)$$, where $$n$$ is the total number of cells in the matrix. We visit each cell exactly once.\n- **Space complexity**: $$O(n)$$ for storing the result list.\n\n# Code\n```cpp\nclass Solution {\npublic:\n    bool init = []() {\n        ios_base::sync_with_stdio(false);\n        cin.tie(NULL);\n        cout.tie(NULL);\n        return true;\n    }();\n    \n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int x, int y) {\n        vector<vector<int>> ans;\n        int total = rows * cols, count = 0, width = 1, height = 1;\n        \n        while (count < total) {\n            // Move right\n            for (int i = 0; i < width; ++i) {\n                if (x >= 0 && x < rows && y >= 0 && y < cols) {\n                    ans.push_back({x, y});\n                    ++count;\n                }\n                ++y;\n            }\n            ++width;\n            \n            // Move down\n            for (int i = 0; i < height; ++i) {\n                if (x >= 0 && x < rows && y >= 0 && y < cols) {\n                    ans.push_back({x, y});\n                    ++count;\n                }\n                ++x;\n            }\n            ++height;\n            \n            // Move left\n            for (int i = 0; i < width; ++i) {\n                if (x >= 0 && x < rows && y >= 0 && y < cols) {\n                    ans.push_back({x, y});\n                    ++count;\n                }\n                --y;\n            }\n            ++width;\n            \n            // Move up\n            for (int i = 0; i < height; ++i) {\n                if (x >= 0 && x < rows && y >= 0 && y < cols) {\n                    ans.push_back({x, y});\n                    ++count;\n                }\n                --x;\n            }\n            ++height;\n        }\n        \n        return ans;\n    }\n};\n```\n\n### Explanation\n- The algorithm starts from the given position `(x, y)` and moves in a spiral pattern.\n- At each step, it checks if the current position is within the matrix boundaries and adds it to the result list if it is.\n- The movement direction (right, down, left, up) and the number of steps taken in each direction increase as the spiral expands.\n- This process continues until all cells in the matrix are visited.
['Array', 'Matrix', 'Simulation', 'C++']