如何在Java中的O(n)中的2维数组中交换数组

发布于 2025-01-27 20:54:43 字数 275 浏览 2 评论 0原文

我正在制作俄罗斯方块的游戏。它是在R X C网格中播放的。现在,为了确定一个块是否填充,我将该块的布尔值存储在2维数组中。 当一行中的所有值都是真实的,或填充行时,将删除行并用上方的行替换,最高行被设置为所有零。 请注意,行从上到下进行0到R。 (0是顶部,r是底部) 现在,默认情况下是O(n^2)问题。但是我想在o(n)中这样做。为此,我打算将每个行的引用仅存储在一个数组中。每当要删除一行时,我只需要将该行引用替换为上一行引用,上一行的引用是前一个参考的参考,依此类推。然后,我只需要为最上方的行添加一个新的空行引用。 我该怎么做

I am making a game of tetris. It is played in a R x C grid. now, to determine whether a block is filled up or not I am storing the boolean value of that block in a 2 dimensional array.
When all values in a row is true, or the row is filled, the row is deleted and replaced with rows above it, with the top most row being set to all zeroes.
Note that rows go 0 to R from top to bottom. (0 being top, R being bottom)
Now, this is by default an O(n^2) problem. But I want to do it in O(n). To do this, I intend to store only references of each rows in an array. Whenever a row is to be deleted, I only need to replace that row reference with previous row reference, the previous one with it's previous one's reference and so on. Then I only need to add a new empty row reference for the topmost row.
How can I do this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文