Simple solution with directions!! with Dry run ✅
Approach\n Describe your approach to solving the problem. \n1. Initialization:\n    - Create a result array to store the coordinates of the cells in the order t
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initialization**:\n    - Create a result array to store the coordinates of the cells in the order they are visited.\n    - Define an array `directions` that contains the movement vectors for right, down, left, and up directions.\n    - Set the starting position `(r, c)` to `(rStart, cStart)` and initialize the direction index.\n    - Initialize `steps` to 1, which represents the number of steps to move in the current direction.\n    - Initialize `resultIndex` to 0, which keeps track of the current position in the result array.\n\n2. **Traversal**:\n    - Place the starting cell in the result array.\n    - Use a while loop to continue the traversal until all cells are visited.\n    - For each direction, move `steps` times and place each valid cell (within matrix bounds) in the result array.\n    - Change direction after completing the steps for two directions.\n    - Increment the `steps` after every two directions.\n\n3. **Direction Change**:\n    - Use the modulo operation to cycle through the directions.\n\n# Dry Run\n\nExample: rows = 3, cols = 3, rStart = 0 and cStart = 0 i.e: `(0, 0)`:\n\n```\nMatrix: 3x3\nStart: (0, 0)\nDirections: Right -> Down -> Left -> Up\n```\n\n#### Steps:\n\n1. Start at `(0, 0)`:\n   - Place `(0, 0)` in the result.\n\n2. Move right (1 step):\n   - New position: `(0, 1)`\n   - Place `(0, 1)` in the result.\n\n3. Move down (1 step):\n   - New position: `(1, 1)`\n   - Place `(1, 1)` in the result.\n\n4. Move left (2 steps):\n   - New position: `(1, 0)` and `(1, -1)` (out of bounds, so ignored)\n   - Place `(1, 0)` in the result.\n\n5. Move up (2 steps):\n   - New position: `(0, 0)` and `(-1, 0)` (out of bounds, so ignored)\n   - Ignore since `(0, 0)` is already in the result.\n\n6. Move right (3 steps):\n   - New position: `(0, 1)`, `(0, 2)` and `(0, 3)` (out of bounds, so ignored)\n   - Place `(0, 2)` in the result.\n\n7. Move down (3 steps):\n   - New position: `(1, 2)`, `(2, 2)` and `(3, 2)` (out of bounds, so ignored)\n   - Place `(1, 2)` and `(2, 2)` in the result.\n\n8. Move left (4 steps):\n   - New position: `(2, 1)`, `(2, 0)`, `(2, -1)` and `(2, -2)` (out of bounds, so ignored)\n   - Place `(2, 1)` and `(2, 0)` in the result.\n\nFinal result: \n```\n[(0, 0), (0, 1), (1, 1), (1, 0), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0)]\n```\n\n# Complexity\n- Time complexity: O(`rows` * `cols`) where `rows` and `cols` are dimensions of the matrix.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(`rows` * `cols`) where `rows` and `cols` are the size of the result matrix.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int[][] result = new int[rows * cols][2];\n        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // Right, Down, Left, Up\n        int r = rStart, c = cStart;\n        int directionIndex = 0;\n        int steps = 1;\n        int resultIndex = 0;\n        \n        result[resultIndex++] = new int[]{r, c};\n\n        while (resultIndex < rows * cols) {\n            for (int i = 0; i < 2; i++) { // Change direction every two steps (right and down, left and up)\n                for (int j = 0; j < steps; j++) {\n                    r += directions[directionIndex][0];\n                    c += directions[directionIndex][1];\n                    \n                    if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                        result[resultIndex++] = new int[]{r, c};\n                    }\n                }\n                directionIndex = (directionIndex + 1) % 4; // Change direction\n            }\n            steps++;\n        }\n\n        return result;\n    }\n}\n```\n\n![image.png](https://assets.leetcode.com/users/images/024ba2e4-c516-46e9-8273-a4439694920a_1723098353.0550914.png)\n![image.png](https://assets.leetcode.com/users/images/1dbe3ac6-929c-44a8-b1ab-e9a284509551_1723098358.0587327.png)\n\n
['Array', 'Depth-First Search', 'Matrix', 'Simulation', 'Java']