Problem: We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.  The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is `0`, and white if S_{i,j} is `1`.  We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.  How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Submission attempts (in order):
import numpy as np

h, w, k = map(int, input().split())
s = np.array(
  [[int(s_ij) for s_ij in input()] for _ in range(h)],
  dtype=np.int)

minimum = (h - 1) * (w - 1)
for div_h in range(1 << (h - 1)):
  h_index = np.zeros(h, dtype=np.int)
  cur_index = 0
  for i in range(1, h):
    if div_h & 1:
      cur_index += 1
    h_index[i] = cur_index
    div_h >>= 1
  white_choco = np.zeros(cur_index + 1, dtype=np.int)
  divide = 0
  j = 0
  while j < w:
    for i in range(h):
      if s[i, j]:
        if white_choco[h_index[i]] < k:
          white_choco[h_index[i]] += 1
        else:
          break
    else:
      j += 1
      continue
    white_choco[:] = 0
    divide += 1
  minimum = min(minimum, divide + cur_index)
print(minimum)

import numpy as np

h, w, k = map(int, input().split())
s = np.array(
  [[s for s in map(int, input().split())] for _ in range(h)],
  dtype=np.int)

minimum = (h - 1) * (w - 1)
for div_h in range(1 << (h - 1)):
  h_index = np.zeros(h, dtype=np.int)
  cur_index = 0
  for i in range(1, h):
    if div_h & 1:
      cur_index += 1
    h_index[i] = cur_index
    div_h >>= 1
  white_choco = np.zeros(cur_index + 1, dtype=np.int)
  divide = 0
  j = 0
  while j < w:
    for i in range(h):
      if s[i, j]:
        if white_choco[h_index[i]] < k:
          white_choco[h_index[i]] += 1
        else:
          break
    else:
      j += 1
      continue
    white_choco[:] = 0
    divide += 1
  minimum = min(minimum, divide + cur_index)
print(minimum)

h, w, k = map(int, input().split())
s = [input() for _ in range(h)]

minimum = (h - 1) * (w - 1)
for div_h in range(1 << (h - 1)):
  h_index = [0] * h
  cur_index = 0
  mask = 1 << (h - 2)
  for i in range(1, h):
    if div_h & mask:
      cur_index += 1
    h_index[i] = cur_index
    mask >>= 1
  white_choco = [0] * (cur_index + 1)
  divide = 0
  for j in range(w):
    for i in range(h):
      if s[i][j] == '1':
        if white_choco[h_index[i]] < k:
          white_choco[h_index[i]] += 1
        else:
          break
    else:
      continue
    white_choco = [0] * (cur_index + 1)
    divide += 1
  minimum = min(minimum, divide + cur_index)
print(minimum)

h, w, k = map(int, input().split())
s = [input() for _ in range(h)]

minimum = (h - 1) * (w - 1)
for div_h in range(1 << (h - 1)):
  h_index = [0] * h
  cur_index = 0
  mask = 1 << (h - 2)
  for i in range(1, h):
    if div_h & mask:
      cur_index += 1
    h_index[i] = cur_index
    mask >>= 1
  white_choco = [0] * (cur_index + 1)
  divide = 0
  j = 0
  while j < w:
    for i in range(h):
      if s[i][j] == '1':
        if white_choco[h_index[i]] < k:
          white_choco[h_index[i]] += 1
        else:
          break
    else:
      j += 1
      continue
    white_choco = [0] * (cur_index + 1)
    divide += 1
  print(h_index, divide)
  minimum = min(minimum, divide + cur_index)
print(minimum)

h, w, k = map(int, input().split())
s = [[int(s_ij) for s_ij in input()] for _ in range(h)]

minimum = (h - 1) * (w - 1)
for div_h in range(1 << (h - 1)):
  h_index = [0] * h
  cur_index = 0
  mask = 1 << (h - 2)
  for i in range(1, h):
    if div_h & mask:
      cur_index += 1
    h_index[i] = cur_index
    mask >>= 1
  white_choco = [0] * (cur_index + 1)
  divide = 0
  j = 0
  flag = False
  while j < w:
    for i in range(h):
      white_choco[h_index[i]] += s[i][j]
    if max(white_choco) > k:
      if flag:
        break
      flag = True
      white_choco = [0] * (cur_index + 1)
      divide += 1
    else:
      j += 1
      flag = False
  else:
    minimum = min(minimum, divide + cur_index)
print(minimum)

Verdicts: Time Limit Exceeded

Runtime Error

Wrong Answer

Wrong Answer

Accepted