题解 | 小红的矩阵染色
小红的矩阵染色
https://www.nowcoder.com/practice/f8b771318bb04490b7389cc35e148166
import sys
n,m,k=map(int,input().split())
grid = list(sys.stdin.read().splitlines())
len_o=list()
for j in range(m):#从列开始每行遍历
cur_len=0
for i in range(n):
if grid[i][j]=="o":
cur_len+=1
else:#出现"*"隔断重新计数
#只要大于1就可进入len_o,因为1-1=0不会影响积分
if cur_len>=1:
len_o.append(cur_len)
cur_len=0
#处理最后几行的o
if cur_len>=1:
len_o.append(cur_len)
len_o.sort(reverse=True)#先用最长的o:
score=0
for i in len_o:
if k>=1:
score+=min(i,k)-1
k-=min(i,k)
else:
break
print(score)
文远知行公司福利 563人发布