885. Spiral Matrix III¶
Intuition¶
The problem requires us to traverse a grid in a spiral order starting from a specific cell. The key challenge is to correctly move in a spiral pattern while ensuring that the movement is handled properly when the traversal extends beyond the grid boundaries. We must also ensure that we visit every cell in the grid.
Approach: Simulating¶
The idea is to simulate the spiral traversal by following a specific order of directions: right, down, left, and up. By moving in this sequence, we can achieve the spiral pattern. The simulation continues until we have visited all the cells in the grid.
Explanation:¶
- Direction Array:
dirs[] = {0, 1, 0, -1, 0}is used to manage movement in the four cardinal directions. The sequence of directions is:- Right:
(0, 1) - Down:
(1, 0) - Left:
(0, -1) - Up:
(-1, 0)
- Right:
- This array allows us to handle direction changes seamlessly during the spiral traversal.
- Initialization:
- We initialize the result vector
reswith a size ofrows * colsto store the coordinates of the cells we visit. d = 0: The direction index, starting with moving right.p = 0: The index to fill in theresvector.steps = 1: This represents the number of steps to move in the current direction. It starts at 1 and increases as we complete two directions (i.e., right and down, left and up).- Traversal:
- The outer
whileloop continues until we have visited all the grid cells (p < rows * cols). - The inner loop structure ensures that we move in the current direction for the required number of
steps. - For every step, if the current position
(x, y)is within the grid boundaries, we store the coordinate inres[p++]. - After moving
stepstimes in one direction, we updatexandyaccordingly and switch to the next direction usingd = (d + 1) % 4to cycle through the four directions. - Increasing Steps:
- After completing a pair of directions (right and down, or left and up), we increase
stepsby 1. This ensures that the spiral expands correctly.
Complexity¶
- Time complexity: O(rows × cols)
- We visit each cell in the grid exactly once.
- Space complexity: O(rows × cols)
- We store the coordinates of all cells in the
resvector.
Code¶
int dirs[] = {0, 1, 0, -1, 0};
class Solution {
public:
vector<vector<int>> spiralMatrixIII(int rows, int cols, int x, int y) {
vector<vector<int>> res(rows * cols);
int d = 0, p = 0, steps = 1;
while (p < rows * cols) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < steps; j++) {
if (x >= 0 && x < rows && y >= 0 && y < cols) {
res[p++] = {x, y};
}
x += dirs[d];
y += dirs[d + 1];
}
d = (d + 1) % 4;
}
++steps;
}
return res;
}
};