Easiest Solution || Beats 80% || Beginner Friendly
Intuition\nTo tackle this problem, you need to visit every cell in a grid in a spiral order. Starting from a specific cell, you\'ll move in a spiral pattern: ri
# Intuition\nTo tackle this problem, you need to visit every cell in a grid in a spiral order. Starting from a specific cell, you\'ll move in a spiral pattern: right, down, left, and up, repeating this pattern while covering all cells in the grid. Even if you move outside the grid boundary, you should ensure all cells are visited.\n\n# Approach\n1. **Initialization** \uD83D\uDCCD:\n   - Set the directions for movement: right (\u2192), down (\u2193), left (\u2190), and up (\u2191).\n   - Create an array to store the coordinates of each cell you visit in the order you visit them.\n   - Begin at the given starting cell `(rStart, cStart)`.\n\n2. **Spiral Movement** \uD83D\uDD04:\n   - Use a loop to simulate moving in a spiral. \n   - Move in each direction (right, down, left, up) and adjust the number of steps you take in each direction.\n   - After moving in two directions, increase the number of steps to expand the spiral.\n   - Ensure that each cell is within the grid boundaries before adding it to your list.\n\n3. **Completion** \uD83C\uDFAF:\n   - Continue this process until you have visited every cell in the grid.\n\n# Complexity\n- Time complexity: \\( O(m \\times n) \\) \uD83D\uDCC8\n  - This is because each cell in the grid is visited exactly once.\n- Space complexity: \\( O(m \\times n) \\) \uD83D\uDCBE\n  - This is due to the space needed to store the coordinates of all cells in the result array.\n# Code\n```Java []\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, 0, -1, 0};\n        int r = rStart, c = cStart;\n        int step = 1;\n        int index = 0;\n\n        result[index++] = new int[]{r, c};\n        \n        while (index < rows * cols) {\n            for (int i = 0; i < 4; i++) {\n                int dir = i % 4;\n                int dr = directions[dir];\n                int dc = directions[dir + 1];\n                \n                for (int j = 0; j < step; j++) {\n                    r += dr;\n                    c += dc;\n                    \n                    if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                        result[index++] = new int[]{r, c};\n                    }\n                }\n                \n                if (i % 2 == 1) {\n                    step++;\n                }\n            }\n        }\n        \n        return result;\n    }\n}\n\n```\n```python []\nclass Solution:\n    def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:\n        result = []\n        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]\n        r, c = rStart, cStart\n        step = 1\n        index = 0\n        \n        result.append([r, c])\n        \n        while len(result) < rows * cols:\n            for i in range(4):\n                dr, dc = directions[i]\n                \n                for _ in range(step):\n                    r += dr\n                    c += dc\n                    \n                    if 0 <= r < rows and 0 <= c < cols:\n                        result.append([r, c])\n                        \n                if i % 2 == 1:\n                    step += 1\n        \n        return result\n\n```\n```C++ []\n#include <vector>\n\nclass Solution {\npublic:\n    std::vector<std::vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        std::vector<std::vector<int>> result;\n        std::vector<std::vector<int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n        int r = rStart, c = cStart, step = 1;\n        \n        result.push_back({r, c});\n        \n        while (result.size() < rows * cols) {\n            for (int i = 0; i < 4; i++) {\n                int dr = directions[i][0];\n                int dc = directions[i][1];\n                \n                for (int j = 0; j < step; j++) {\n                    r += dr;\n                    c += dc;\n                    \n                    if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                        result.push_back({r, c});\n                    }\n                }\n                \n                if (i % 2 == 1) {\n                    step++;\n                }\n            }\n        }\n        \n        return result;\n    }\n};\n\n```\n```Javascript []\nvar spiralMatrixIII = function(rows, cols, rStart, cStart) {\n    const result = [];\n    const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];\n    let r = rStart, c = cStart, step = 1;\n    \n    result.push([r, c]);\n    \n    while (result.length < rows * cols) {\n        for (let i = 0; i < 4; i++) {\n            const [dr, dc] = directions[i];\n            \n            for (let j = 0; j < step; j++) {\n                r += dr;\n                c += dc;\n                \n                if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                    result.push([r, c]);\n                }\n            }\n            \n            if (i % 2 === 1) {\n                step++;\n            }\n        }\n    }\n    \n    return result;\n};\n\n```\n```ruby []\ndef spiral_matrix_iii(rows, cols, r_start, c_start)\n  result = []\n  directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]\n  r, c = r_start, c_start\n  step = 1\n  \n  result << [r, c]\n  \n  while result.size < rows * cols\n    directions.each_with_index do |(dr, dc), i|\n      step.times do\n        r += dr\n        c += dc\n        \n        if r >= 0 && r < rows && c >= 0 && c < cols\n          result << [r, c]\n        end\n      end\n      \n      step += 1 if i % 2 == 1\n    end\n  end\n  \n  result\nend\n![main-qimg-dd19ad82f9afcc414cdfc8ded048648e-lq.jpeg](https://assets.leetcode.com/users/images/cdd69581-b52f-412b-9ab6-abd838aababc_1723140926.8810954.jpeg)\n\n```\n```C []\n#include <stdlib.h>\n\nint** spiralMatrixIII(int rows, int cols, int rStart, int cStart, int* returnSize, int** returnColumnSizes) {\n    int** result = (int**)malloc(rows * cols * sizeof(int*));\n    *returnColumnSizes = (int*)malloc(rows * cols * sizeof(int));\n    int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n    int r = rStart, c = cStart, step = 1, index = 0;\n    \n    result[index] = (int*)malloc(2 * sizeof(int));\n    result[index][0] = r;\n    result[index][1] = c;\n    (*returnColumnSizes)[index] = 2;\n    index++;\n    \n    while (index < rows * cols) {\n        for (int i = 0; i < 4; i++) {\n            int dr = directions[i][0];\n            int dc = directions[i][1];\n            \n            for (int j = 0; j < step; j++) {\n                r += dr;\n                c += dc;\n                \n                if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                    result[index] = (int*)malloc(2 * sizeof(int));\n                    result[index][0] = r;\n                    result[index][1] = c;\n                    (*returnColumnSizes)[index] = 2;\n                    index++;\n                }\n            }\n            \n            if (i % 2 == 1) {\n                step++;\n            }\n        }\n    }\n    \n    *returnSize = rows * cols;\n    return result;\n}\n\n```\n\n\n![e78e83d2-3828-4681-96d5-40e225e1d8b0_1699854685.5280945.jpeg](https://assets.leetcode.com/users/images/bcd2632d-74c8-4474-b9d9-f9e6e85269a8_1723140994.445592.jpeg)\n\n
['Array', 'C', 'Matrix', 'Simulation', 'Python', 'C++', 'Java', 'Ruby', 'JavaScript']