Most efficient algorithm to find the biggest square in a two dimension map

前端 未结 5 579
长情又很酷
长情又很酷 2021-02-02 04:03

I would like to know the different algorithms to find the biggest square in a two dimensions map dotted with obstacles.

An example, where o would be obstacl

5条回答
  •  半阙折子戏
    2021-02-02 04:38

    So here's one rough approach.

    Store the x-y positions of all the obstacles.
    For each obstacle O
       find obstacle C that is nearest to it column-wise.
       find obstacle R-top that is nearest to it row-wise from the top.
       find obstacle R-bottom that is nearest to it row-wise from the bottom.
       if (|R-top.y - R-bottom.y| != |O.x - C.x|) continue
       Size of the square = Abs((R-top.y - R-bottom.y) * (O.x - C.x))
    Keep track of the sizes and positions to find the largest square
    

    Complexity is roughly O(k^2) where k is the number of obstacles. You could reduce it to O(k * log k) if you use binary search.

提交回复
热议问题