我有一个NxM矩阵,在矩阵中的随机索引中以0和1编码(1表示要绘制的像素,0表示空格)。我想将所有这些组合在字典中并将它们打印为一个命令而不是单独的迭代以进行优化,然后将其放回原始矩阵中以创建其最终形状(俄罗斯方块游戏)。
矩阵看起来像这样:
[[0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1]
[0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1]
[1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1]
[1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1]
[0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]]
我的代码如下:
def solution(imat, painter):
nrows, ncols = imat.shape # gets the shape of the matrix (20 rows and 20 columns)
start = 0
end = 0
rows = dict.fromkeys(range(nrows), []) #creates the dictionary
for r in range(nrows): # loops through the number of rows
for c in range(ncols): # loops through the number of columns
if imat[r,c] == 1: # checks if at a specific index there exists a 1
rows[r].append((r,c)) # if so it appends it to the dictionary
painter.paint('square', r, c, 1) # prints the 1s, but as individuals instead of a block.
我可以附加所有1的字典,但无法将所有1作为一个对象并放回原始矩阵(即imat.shape
)
最终输出应该看起来像this,尽可能少的命令。
可以找到其他信息:here
所有帮助和建议都将受到高度赞赏
答案 0 :(得分:0)
我做了一个朴素压缩算法的示例。我们的想法是计算连续多少0
和1
s。在最糟糕的情况下,你根本没有压缩,但是你的数据似乎是完全分组的,所以一般来说你应该看到相当多的减少。
我还添加了一个关于如何打印此数据的示例,因为它最终似乎是您想要的。更改print_block
方法以使用您用于绘制屏幕的任何内容 - 我在此向您展示如何使用它来打印块中的内容'压缩数据。
m = [[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
def compress(mat):
""" Compress each row by counting how many 0's and 1's are in a row.
Example:
1 1 0 0 1 1 1 0
0 0 0 0 1 0 1 1
Result
0 2 2 3 1
4 1 1 2
"""
nm = []
for r in mat:
nr = []
gather = 0
acc = 0
for c in r:
if c == gather:
acc += 1
else:
nr.append(acc)
gather = int(not gather)
acc = 1
nr.append(acc)
nm.append(nr)
return nm
print_calls = 0
def print_block(value, nbr):
global print_calls
print_calls += 1
for i in range(nbr):
print(value, end=" ")
def print_compressed(mat):
for r_block in mat:
for i, nbr in enumerate(r_block):
print_block(i % 2, nbr) # change this line to draw your actual board
print()
cm = compress(m)
print_compressed(cm)
print('print_calls =', print_calls)
这为我输出
0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1
1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1
1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
print_calls = 70
打印电话的减少是从20 * 20 = 400 - > 70.也就是说,减少了82.5%。