Jane OA 面试真题解析:二维网格落子与状态维护(Move Function 设计)

28次阅读
没有评论

Simulate a game on a 2D grid that is infinite to the left, right, and top, but bounded at the bottom.

There are two players: Red, represented by R, and Blue, represented by B.

Keep track of the board state and implement a move function.

The move function takes an x position and places a piece at the bottom of that x-column, shifting everything above it upward.

这道题本质上是在设计一个“按列下落”的双人棋盘模拟器:棋盘在左右和上方都是无限的,但底部有边界。每次调用 move 时,只需要根据给定的 x 列找到当前这一列最底部可落子的位置,把新棋子放入后,再让该列上方的棋子整体上移一格即可。实现时重点是维护每一列的状态,而不是显式存整个无限棋盘;通常可以用哈希表或按列存储的结构记录每列已有棋子的位置与颜色,从而高效支持连续落子和状态更新。

正文完
 0