Anyone knows an algorithm for finding “shapes” in 2d arrays?

后端 未结 5 1383
不思量自难忘°
不思量自难忘° 2021-02-04 08:25

Let\'s take this map, where \'#\' illustrates a taken square and \'.\' illustrates a free square:

1 . # # # . .
2 . # . . # .
3 # . . . . #
4 . # # # . .
5 . . . . .          


        
5条回答
  •  再見小時候
    2021-02-04 08:35

    If you model this map as a graph, and each square is connected to its four neighbours, you can use a bridge finding algorithm to find the square you need.

    Note this model gives you several subgraphs to work with sometimes, so it might produce a number of false positives around the border, since adding a # there would certainly separate some nodes from the rest. To get around this, you could pad two levels of squares around the graph, so that no single # could separate a border node from the rest.

    @svick's comment inspired this method.

提交回复
热议问题