【问题标题】:algorithm to find overlaps寻找重叠的算法
【发布时间】:2011-01-29 17:55:55
【问题描述】:

基本上,我有一些 Ship 类型的结构,它们将放在可以具有可变宽度和高度的板上。有关船只的信息是从文件中读取的,我只需要知道确保没有船只重叠的最佳方法。

Ship 的结构如下:

int x // x position of first part of ship
int y // y position of first part of ship
char dir // direction of the ship, either 'N','S','E' or 'W'
int length // length of the ship

另外,处理方向的好方法是什么。比使用 switch 语句和为每个方向使用不同的条件更简洁的方法。

任何帮助将不胜感激!

【问题讨论】:

  • 您只需要 2 个方向(例如南和东),以防方向本身无关紧要。
  • 方向是从文件中读取的,因此不一定包含船的顶部或最左侧
  • 这看起来像战舰游戏,是吗?

标签: c algorithm loops struct


【解决方案1】:

除非你有很多船,否则只需使用简单的蛮力算法,这将是两个嵌套循环,即 O(n^2)。

【讨论】:

  • 你是对的 - 但我忘了添加我的问题的另一部分,即处理差异方向的最佳方法是什么(现在编辑)。对此有任何想法吗?谢谢你的时间:)
【解决方案2】:

保留棋盘的位图,其中每个位指示是否有船占据该图块。为每艘船标记它在棋盘上占据的瓷砖,并检查您是否标记了两次相同的位。

【讨论】:

    【解决方案3】:

    您可以保留整个网格的布尔数组,最初初始化为“false”。对于每艘船,对于船所覆盖的每个位置,检查该位置是否为“假”。如果是, 将其设置为“真”。如果没有,那么其他船只在该位置。

    这个算法在所有船只的总面积上是线性的,但也需要额外的空间 与板上的位置数量成正比。

    【讨论】:

      【解决方案4】:

      (战舰?)

      制作一个 2D 数组(“board”),其中包含船的 ID(当有船时)。因此,当您添加船时,您可以在 O(length) 时间内检查一个空间是否被占用。

      O(n * length) 时间,O(N^2) 空间。

      【讨论】:

        【解决方案5】:

        这与测试矩形是否相交是一回事,我认为如果您不将这些船视为一个点、长度和方向,而是将其视为一个矩形,那么您的代码会更简单。

        所以转换这个

        int x // x position of first part of ship
        int y // y position of first part of ship
        char dir // direction of the ship, either 'N','S','E' or 'W'
        int length // length of the ship
        

        对此(允许负 cx & cy 得到 N,S,E,W)

        int x // x position of first part of ship
        int y // y position of first part of ship
        int cx // length of the ship in X 
        int cy // length of the ship in Y
        

        或者这个

        int left   // x position of Eastern part of the ship
        int top    // y position of Northernmost part of ship
        int right  // x position of Westernmost part of the ship
        int bottom // y position of Southernmost part of ship
        bool orientation; // so we can tell East from West or North from South.
        

        然后一个简单的函数可以确定两艘船是否相交。

        bool DoShipsIntersect(Ship * a, Ship * b)
        {
            if ((a->right < b->left) || (b->right < a->left))
               return false;
            if ((a->bottom < b->top) || (b->bottom < a->top))
               return false;
            return true;
        }
        

        只要您没有数千艘船,将每艘船与每艘其他船进行蛮力比较应该很快。

        【讨论】:

        • 好答案 :) 现在我要弄清楚是否值得通过并重写大量代码来做到这一点..
        • @Gary:你可以一次做一点。编写一个函数将 Ship 转换为 ShipRect ,反之亦然,您的原始结构是存储船只的好方法,但不是比较它们的最有效方法。
        【解决方案6】:

        表示方向的一种方法是作为单位向量。这可以是 2 个整数:dirX 和 dirY。例如。 dirX=1 表示东;或 dirY=1 表示南方。

        然后,您可以使用以下命令遍历飞船占据的所有位置:

        int cx = x;
        int cy = y;
        for(int i = 0; i < length; i++) {
            cx += dirX;
            cy += dirY;
        }
        

        或者根据这些值获取一个边界框:

        x
        y
        x + dirX * (length - 1)
        y + dirY * (length - 1)
        

        【讨论】:

          【解决方案7】:

          您可能希望使用枚举类型作为您的方向。在寻找重叠方面,为您的电路板创建一个初始化为 false 的布尔值的二维数组。然后,对于每艘船,在数组中找到相应的条目,如果它们已经为真,则存在重叠。否则,将这些条目设置为 true。如果你已经放置了所有的船并且没有遇到一个已经真实的条目,那么就没有重叠。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-06-03
            • 2018-07-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多