Simulate simultaneous one-cell wind movements on a grid of leaf counts, discarding leaves that cross the boundary. Explain fresh-state updates, in-place traversal hazards, and how to compute the remaining total.
A rectangular grid stores the number of leaves in each cell. Apply a sequence of wind directions U, D, L, and R. Leaves that move outside the grid disappear permanently. Return the total number of leaves remaining after all directions.
### Constraints and Clarifications
For this practice interface, each direction moves every existing leaf exactly one cell simultaneously: U decreases the row index, D increases it, L decreases the column index, and R increases it. This unit-step and coordinate convention makes the reported directional movement precise. Counts are nonnegative integers; no numerical size bounds are given. An empty grid contains zero leaves, and an empty direction sequence preserves the starting total. The input grid is read only for this implementation.
### Example
Starting with [[1,2],[3,4]], apply R. The old right-column leaves disappear and the remaining grid is [[0,1],[0,3]], so the answer is 4. Applying L afterward moves those surviving leaves left; discarded leaves do not return.
### What a Strong Answer Covers
Implement simultaneous movement with correct boundary handling and sum the final grid. Explain why a fresh grid for each wind step avoids unintended repeated movement and why naive in-place updates can be wrong.
### Follow-up Questions
- Show a small case where a left-to-right in-place implementation of a right wind moves a leaf more than once.
- Can a carefully chosen traversal direction make an in-place implementation correct, and what additional care does it need?
Overview: Simulate simultaneous one-cell wind movements on a grid of leaf counts, discarding leaves that cross the boundary. Explain fresh-state updates, in-place traversal hazards, and how to compute the remaining total.
A rectangular grid stores the number of leaves in each cell. Apply a sequence of wind directions U, D, L, and R. Leaves that move outside the grid disappear permanently. Return the total number of leaves remaining after all directions.
Constraints and Clarifications
For this practice interface, each direction moves every existing leaf exactly one cell simultaneously: U decreases the row index, D increases it, L decreases the column index, and R increases it. This unit-step and coordinate convention makes the reported directional movement precise. Counts are nonnegative integers; no numerical size bounds are given. An empty grid contains zero leaves, and an empty direction sequence preserves the starting total. The input grid is read only for this implementation.
Example
Starting with [[1,2],[3,4]], apply R. The old right-column leaves disappear and the remaining grid is [[0,1],[0,3]], so the answer is 4. Applying L afterward moves those surviving leaves left; discarded leaves do not return.
What a Strong Answer Covers Guidance
Implement simultaneous movement with correct boundary handling and sum the final grid. Explain why a fresh grid for each wind step avoids unintended repeated movement and why naive in-place updates can be wrong.
Follow-up Questions Guidance
Show a small case where a left-to-right in-place implementation of a right wind moves a leaf more than once.
Can a carefully chosen traversal direction make an in-place implementation correct, and what additional care does it need?