Python 实现【荒地建设电站/ 区域发电量统计】

B站影视 电影资讯 2025-05-19 14:23 1

摘要:def count_valid_squares(rows, cols, k, min_power, grid): # Create a prefix sum matrix prefix = [[0] * (cols + 1) for _ in range(ro

def count_valid_squares(rows, cols, k, min_power, grid): # Create a prefix sum matrix prefix = [[0] * (cols + 1) for _ in range(rows + 1)] for i in range(1, rows + 1): row_sum = 0 for j in range(1, cols + 1): row_sum += grid[i-1][j-1] prefix[i][j] = prefix[i-1][j] + row_sum count = 0 # Iterate over all possible top-left corners of the k x k square for i in range(rows - k + 1): for j in range(cols - k + 1): # Calculate the sum of the square from (i,j) to (i+k-1, j+k-1) total = prefix[i + k][j + k] - prefix[i][j + k] - prefix[i + k][j] + prefix[i][j] if total >= min_power: count += 1 return count# Read inputrows, cols, k, min_power = map(int, input.split)grid = for _ in range(rows): row = list(map(int, input.split)) grid.append(row)# Compute and print the resultprint(count_valid_squares(rows, cols, k, min_power, grid))

来源:我就这样咋地了

相关推荐