Search using itertools In many cases, full bit search is recommended for search, but I think that it is more practical to use itertools for Python. Summary of all enumerations that can be done with itertools in Python
Sample code
import itertools
N, M, X = list(map(int, input().split()))
a = [list( map( int, input().split() ) ) for i in range(N)]
for x in itertools.product([0,1], repeat=2):
cost = 0
level = [] * M
judge = True
ans = 1234567
for i in range(N):
if a[i] == 1:
cost += a[i][0]
level = [y + z for (y, z) in zip(level, a[i][1:])]
for j in range(M):
if any(level[k] < x for k in range(M)):
judge = False
if judge:
ans = min(ans, cost)
if ans == 1234567:
print(-1)
else:
print(ans)
Recommended Posts