计算矩阵中形成矩形的位置数

问题描述 投票:0回答:1

我有一个只包含 0 和 1 的方阵。例如,

1  0  1  1  1
1  1  0  0  1
1  0  1  1  0
0  1  1  1  1
1  0  1  1  1

我想计算顶点为 1 且边与矩阵的行和列平行的矩形的数量。矩形的边缘允许有 0。这是一个这样的矩形的例子(它的顶点在方括号中)

[1]  0  1  1  [1]
 1   1  0  0   1
 1   0  1  1   0
 0   1  1  1   1
[1]  0  1  1  [1]

我已经调查了link1link2但仍然没有线索..

algorithm graph combinatorics rectangles discrete-mathematics
1个回答
0
投票
- LOOP C over cells in the matrix
   - LOOP S over possible rectangle sizes, starting from this cell
       - Set found TRUE
       - LOOP R over cells forming rectangle of size S with top left at C
            - IF R contain 0
                  - SET found false
                  - BREAK from LOOP R
       - IF found
            Add to count

例子:

matrix 5 by 5
start cell 1,1  ( zero-based )
maximum rectangle is 4 by 4
check cells at  1,4    4,1    and 4,4
if all contain 1s you have found a 4 by 4 rectangle
© www.soinside.com 2019 - 2024. All rights reserved.