用俄罗斯方块填充整个2D阵列(俄罗斯方块板),没有剩余空间(Java)

时间:2023-02-09 21:52:10

Let's say I have a 2D int array..

假设我有一个2D int数组..

int[][] board = new int[10][20];

public void initBoard()
{
    for(int r = 0; r < 10; r++)
        for(int c = 0; c < 20; c++)
            board[r][c] = 0;
}

0 means no piece The pieces are represented by 1-7;

0表示没有作品这些作品用1-7表示;

1 - Z Shape

1 - Z形

2 - S Shape

2 - S形

3 - Line Shape

3 - 线形

4 - T Shape

4 - T形

5 - Box Shape

5 - 盒子形状

6 - L Shape

6 - L形

7 - Backwards L Shape

7 - 向后L形

What is the best way to fill the entire array with random shapes and no spaces left over.

用随机形状填充整个数组并且没有剩余空格的最佳方法是什么。

Note: I have the game working, I'm just trying to adapt it to something different while still using the Tetris gameplay

注意:我让游戏正常运行,我只是想让它适应不同的东西,同时仍然使用俄罗斯方块游戏

2 个解决方案

#1


0  

It's not so easy as it seems. It's NP-hard problem in fact. Packing rectangles is similar, you can start with that a little bit simpler problem.

它看起来并不那么容易。事实上,这是NP难问题。包装矩形是类似的,你可以从一个简单的问题开始。

#2


0  

This is actually a really complicated question you are asking. In Computer Science, it is known as a Packing Problem, and there are lots of possible algorithms and possible approaches, depending on the exact nature of what it is you want to accomplish.

这实际上是一个非常复杂的问题。在计算机科学中,它被称为包装问题,并且有许多可能的算法和可能的方法,这取决于你想要完成什么的确切性质。

In the general case, this problem is hard, really hard, in fact, it is NP-hard to find an optimal general solution. For more information, check out the research paper by Demaine et al from MIT.

在一般情况下,这个问题很难,真的很难,事实上,找到一个最优的通用解决方案是非常难的。有关更多信息,请查看来自麻省理工学院的Demaine等人的研究论文。

#1


0  

It's not so easy as it seems. It's NP-hard problem in fact. Packing rectangles is similar, you can start with that a little bit simpler problem.

它看起来并不那么容易。事实上,这是NP难问题。包装矩形是类似的,你可以从一个简单的问题开始。

#2


0  

This is actually a really complicated question you are asking. In Computer Science, it is known as a Packing Problem, and there are lots of possible algorithms and possible approaches, depending on the exact nature of what it is you want to accomplish.

这实际上是一个非常复杂的问题。在计算机科学中,它被称为包装问题,并且有许多可能的算法和可能的方法,这取决于你想要完成什么的确切性质。

In the general case, this problem is hard, really hard, in fact, it is NP-hard to find an optimal general solution. For more information, check out the research paper by Demaine et al from MIT.

在一般情况下,这个问题很难,真的很难,事实上,找到一个最优的通用解决方案是非常难的。有关更多信息,请查看来自麻省理工学院的Demaine等人的研究论文。