Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 二元矩阵中矩形的计数_Python 3.x - Fatal编程技术网

Python 3.x 二元矩阵中矩形的计数

Python 3.x 二元矩阵中矩形的计数,python-3.x,Python 3.x,给定一个nxn二进制矩阵(“#”或"),我们如何计算由“#”创建的矩形的数量 例如,下面有一个矩形: _ _ _ # # # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # # # _ _ _ _ _ _ # # # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # # # _ _ _ 拿一个完美的NxN正方形,你可以做:

给定一个nxn二进制矩阵(“#”或"),我们如何计算由“#”创建的矩形的数量

例如,下面有一个矩形:

_ _ _ # # # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # # # _ _ _ _ _ _ # # # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # _ # _ _ _ _ _ _ # # # _ _ _
拿一个完美的NxN正方形,你可以做:

st='''\
########## 
#####----# 
##--#----# 
##--#----# 
##--#----# 
##--#----# 
#####----# 
########## 
-####----# 
########## '''

def max_size(mat, taken):
    """Find the largest X or a square not taken in the matrix `mat`."""
    nrows, ncols = len(mat), len(mat[0]) 
    assert nrows==ncols 
    dirs=(0,1),(1,0),(1,1) # right, down, right and down
    counts = [[0]*ncols for _ in range(nrows)]
    for i in reversed(range(nrows)):     # for each row
        assert len(mat[i]) == ncols      # matrix must be rectangular
        for j in reversed(range(ncols)): # for each element in the row
            if mat[i][j] != taken:
                if i < (nrows - 1) and j < (ncols - 1):
                    add=1+min(counts[i+x][j+y] for x,y in (dirs))
                else:
                    add=1      # edges 
                counts[i][j]=add        

    for line in counts: print(line)                               
    return max(c for rows in counts for c in rows)   # max X (or Y) number elements

table=[[c for c in s.strip()] for s in st.splitlines()]     

print (max_size(table,'#')**2)
st=''\
########## 
#####----# 
##--#----# 
##--#----# 
##--#----# 
##--#----# 
#####----# 
########## 
-####----# 
########## '''
def最大尺寸(垫,已取):
“”“查找矩阵'mat'中未采用的最大X或正方形。”
nrows,ncols=len(mat),len(mat[0])
断言nrows==ncols
dirs=(0,1)、(1,0)、(1,1)#右、下、右、下
计数=[[0]*范围内(nrows)的NCOL
对于反向(范围(nrows))中的i:#对于每行
断言len(mat[i])==ncols#矩阵必须为矩形
对于反向(范围(ncols))中的j:#对于行中的每个元素
如果mat[i][j]!=拿:
如果i<(nrows-1)和j<(ncols-1):
加法=1+min(x的计数[i+x][j+y],y英寸(dirs))
其他:
添加=1#边
计数[i][j]=相加
对于行内计数:打印(行)
返回最大值(c为行中的行计数,c为行计数)#最大X(或Y)个数字元素
表=[[c代表s.strip()中的c代表s.splitlines()中的s代表c]
打印(最大尺寸(表“#”)**2)

有趣的问题。不过,如果您为输入/输出带来一些其他示例,那就太好了。
中有多少个矩形?