C# Solution for Spiral Matrix III Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires visiting all cells in a grid in a spiral order starting from a spe
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires visiting all cells in a grid in a spiral order starting from a specified initial position (rStart, cStart). The approach involves simulating the movement akin to a snail\u2019s shell, expanding outward while adjusting direction and step size dynamically.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tDirectional Movement: We define four direction vectors for right, down, left, and up movements. These vectors facilitate changing the current position in the grid as we spiral outward.\n2.\tDynamic Steps and Direction: Unlike the fixed step approach in the previous Java solution, here we incrementally increase the number of steps taken in each direction as we progress through the spiral. After completing moves in each direction (right, down, left, up), we adjust the steps to ensure the spiral grows correctly.\n3.\tBoundary Checks: Before adding a position to the result array, ensure it lies within the grid boundaries (0 <= x < rows and 0 <= y < cols). This prevents accessing out-of-bound indices.\n4.\tResult Storage: Use an array result to store the visited positions in the order they are visited. This array is returned as the final output.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this solution is  O(rows x cols) . This is because we visit each cell in the grid exactly once. Within the loop, the operations involve constant-time checks and updates (O(1)), making the overall complexity linear with respect to the number of cells in the grid.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is  O(rows x cols)  due to the storage needed for the result array, which holds all the visited coordinates. Apart from this, the additional space used is constant  O(1)  for variables like x, y, direction, steps, and index.\n\n# Code\n```\npublic class Solution {\n    public int[][] SpiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        // Direction vectors: right, down, left, up\n        int[][] directions = new int[][] {\n            new int[] {0, 1},   // Right\n            new int[] {1, 0},   // Down\n            new int[] {0, -1},  // Left\n            new int[] {-1, 0}   // Up\n        };\n        \n        int totalCells = rows * cols;\n        int[][] result = new int[totalCells][];\n        int x = rStart, y = cStart;\n        int direction = 0; // Start by moving right\n        int steps = 1; // Initial steps in the current direction\n        int index = 0;\n        \n        result[index++] = new int[] {x, y};\n        \n        while (index < totalCells) {\n            for (int i = 0; i < 2; i++) { // Two moves in each step increase cycle\n                for (int j = 0; j < steps; j++) {\n                    x += directions[direction][0];\n                    y += directions[direction][1];\n                    if (x >= 0 && x < rows && y >= 0 && y < cols) {\n                        result[index++] = new int[] {x, y};\n                    }\n                }\n                direction = (direction + 1) % 4; // Change direction\n            }\n            steps++; // Increase steps after completing two direction moves\n        }\n        \n        return result;\n    }\n}\n```
['C#']