Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- from itertools import combinations, product
- import sys
- import time
- A1, A2 = 6, 9
- for n in range(A1, A2 + 1):
- for m in range(n, A2 + 1):
- def _next_pos(i, j):
- if i == n - 1:
- return (0, j + 1)
- return (i + 1, j)
- def f(A, i, j):
- if i >= n or j >= m:
- yield A
- return
- a1 = i in (0, n - 1) or A[i - 1][j] != '1'
- a2 = j in (0, m - 1) or A[i][j - 1] != '1'
- i2, j2 = _next_pos(i, j)
- if a1 or a2:
- A[i][j] = '1'
- yield from f(A, i2, j2)
- else:
- for a in f(A, i2, j2):
- for char in '*+1':
- A[i][j] = char
- yield a
- best_answer = float('inf')
- def calc(A):
- s = 0.0
- for row in A:
- s += eval(''.join(row))
- if s >= best_answer:
- return None
- for j in range(len(A[0])):
- col = ''.join(row[j] for row in A)
- s += eval(col)
- if s >= best_answer:
- return None
- return s
- start = time.time()
- A = [[None] * m for _ in range(n)]
- best_A = None
- for a in f(A, 0, 0):
- ans = calc(a)
- if ans is not None:
- best_answer = ans
- best_A = copy.deepcopy(a)
- end = time.time()
- print((n, m), best_answer, end - start, file=sys.stderr)
- print((n, m), best_answer, end - start, flush=True)
- for row in best_A:
- print(''.join(row), flush=True)
- print(flush=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement