Implement the function remaining_leaves(n, m, leaves, winds) which takes as inputs the integers n and m, the 2D integer array leaves, and the string winds, and returns the number of leaves left on the floor after applying all wind gusts.
The ground is represented by a grid that is n squares high and m squares wide. The top-left box is located at (0, 0), where the first integer represents the row and the second the column. Each element of the grid represents the number of leaves on the ground.
The string winds is composed of the characters U (top), D (bottom), R (right), and L (left).
Each gust of wind moves the leaves on the grid one square in the direction of the wind. Leaves that are pushed out of the grid are lost.
Return the total number of leaves remaining on the grid as an integer.
Example:
winds = "RRD"
After applying the gusts, some leaves may move out of bounds and disappear. The answer is the total count of leaves still inside the grid.
这道题本质上是一个二维数组模拟题:给定一个 n×m 的叶子分布矩阵和一串风向指令,需要按顺序模拟每一次风吹动。每次风只会让所有叶子整体朝一个方向移动一格,移出边界的叶子直接消失。实现时不需要复杂算法,关键是按风向处理矩阵的遍历顺序,避免覆盖尚未移动的数据。通常可以针对 U/D/L/R 分别使用原地移动或构造新矩阵来完成,最后统计剩余叶子总数即可。