DEV Community

Cover image for 885. Spiral Matrix III
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

1

885. Spiral Matrix III

885. Spiral Matrix III

Medium

Topics: Array, Matrix, Simulation

You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.

You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.

Return an array of coordinates representing the positions of the grid in the order you visited them.

Example 1:

example_1

  • Input: rows = 1, cols = 4, rStart = 0, cStart = 0
  • Output: [[0,0],[0,1],[0,2],[0,3]]

Example 2:

example_2

  • Input: rows = 5, cols = 6, rStart = 1, cStart = 4
  • Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]

Constraints:

  • 1 <= rows, cols <= 100
  • 0 <= rStart < rows
  • 0 <= cStart < cols

Solution:

To solve this problem, we can follow these steps:

  1. Direction Array: Use a direction array to facilitate movement in the right order (east → south → west → north). Each direction will have a corresponding change in the row and column indices.
  2. Steps Management: You need to control how many steps to take in each direction. Initially, you move 1 step east, then 1 step south, 2 steps west, 2 steps north, and so on.
  3. Boundary Check: Ensure that after every move, you check whether the new position is within the grid boundaries. If it is, add it to the result array.
  4. Stopping Condition: Stop the loop once you have visited all rows * cols positions.

Let's implement this solution in PHP: 885. Spiral Matrix III

<?php
// Example Usage:
print_r(spiralMatrixIII(1, 4, 0, 0)); // [[0,0],[0,1],[0,2],[0,3]]
print_r(spiralMatrixIII(5, 6, 1, 4)); // [[1,4],[1,5],[2,5],[2,4], ...]
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Directions: The directions array holds the change in row and column for moving east, south, west, and north.
  2. Movement: We start at (rStart, cStart) and move according to the directions in the spiral pattern.
  3. Boundary Checking: Only add the position to result if it is within the grid.
  4. Steps Control: stepCount manages how many steps are taken in the current direction before turning. It increases after two turns.
  5. Termination: The loop continues until all positions in the grid have been visited.

This approach ensures that we visit every cell in the grid in the required spiral order.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay