gpt4 book ai didi

java - 我应该如何将形状融入空间?

转载 作者:行者123 更新时间:2023-12-01 11:37:12 25 4
gpt4 key购买 nike

假设我有以下形状(字符串类型);

..ee
e..e

以及以下空间:

||..
.|..

我的方法shapeFitsAt()确定给定的形状是否可以放置在指定的行/列位置。 row,col 表示左上角的位置将放置形状的角[ block (0,0)]。一个形状会如果其中一个填充 block 与一个填充 block 发生冲突,则不适合空间中现有的填充 block 或将超出范围空间。 “|”表示阻挡/障碍,而“.”指的是空旷的空间。因此,如果我将上面的形状放入空间中,它将看起来像这样:

||ee
e|.e

请问 smb 可以帮助如何将形状融入空间吗?提前致谢!

public class CreateSpace {

private int height;
private int width;
private char[][] space = new char[height][width];
private Shape originalShape;

public CreateSpace(int height, int width, char[][] space, Shape shape)
{
this.height = height;
this.width = width;
this.space = space;
this.originalShape = shape;
}
public boolean shapeFitsAt(int row, int col, Shape shape)
{

if(row < 0 || row >= height || col < 0 || col >= width)
throw new FitItException("Oops! Out of bounds in CreateSpace class! Go and check!");

else if(space[row][col] == '|' || space[row][col] == this.originalShape.getDisplayChar())
throw new FitItException("The space position is already filled out with a wall or a character. GO CHECK CreateSpace class!");

else
{

}

}

最佳答案

一种解决方案是将图案“线性化”为 2 个位集(一个用于形状,一个用于障碍物)。

在形状位集中必须保留符号的地方放 1,在其他地方放 0;在障碍物所在的位置放置 0,在“障碍物”位集中的其他位置放置 1。

例如:

..ee    -->    ..eee..e    -->    00111001
e..e

||.. --> ||...|.. --> 00111011
.|..

然后将两个位集与,并查看结果是否与形状位集相同。如果是如果,形状适合;如果没有,至少有一个障碍物挡住了路。

    Shape : 00111001
Obstacles : 00111011
AND : 00111001 == Shape bitset => OK !

快速高效,您可以重复使用障碍物位集来测试多种形状模式。

关于java - 我应该如何将形状融入空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com