小美的平衡矩阵
python解法如下:
def solve(i, j, m, sum_all): if m*m % 2 == 1: return False if i == 0 and j == 0 and i + m - 1 < n and j + m - 1 < n and sum_all[i + m - 1][j + m - 1] == m * m / 2: return True elif i == 0 and j > 0 and i + m - 1 < n and j + m - 1 < n and sum_all[i + m - 1][j + m - 1] - sum_all[i + m - 1][ j - 1] == m * m / 2: return True elif j == 0 and i > 0 and i + m - 1 < n and j + m - 1 < n and sum_all[i + m - 1][j + m - 1] - sum_all[i - 1][ j + m - 1] == m * m / 2: return True elif i >= 1 and j >= 1 and i + m - 1 < n and j + m - 1 < n and ( sum_all[i + m - 1][j + m - 1] - sum_all[i + m - 1][j - 1] - sum_all[i - 1][j + m - 1] + sum_all[i - 1][ j - 1]) == m * m / 2: return True return False n = int(input()) a = [[0] * n for _ in range(n)] for i in range(n): l = list(input()) l = [int(j) for j in l] a[i] = l # 二维前缀和sum_all[i][j]表示从(0, 0)到(i, j)的和 sum_all = [[0]*n for _ in range(n)] sum_all[0][0] = a[0][0] for j in range(1, n): sum_all[0][j] = sum_all[0][j-1] + a[0][j] for i in range(1, n): sum_all[i][0] = sum_all[i-1][0] + a[i][0] for i in range(1, n): for j in range(1, n): sum_all[i][j] = sum_all[i-1][j] + sum_all[i][j-1] - sum_all[i-1][j-1] + a[i][j] res = [0] * n for i in range(n): for j in range(n): for k in range(1, n + 1): if solve(i, j, k, sum_all): res[k-1] += 1 for i in range(n): print(res[i])#美团笔试#