Skip to content
Snippets Groups Projects
Unverified Commit abc7aa5a authored by Maarten van den Berg's avatar Maarten van den Berg
Browse files

Delete sub_prob.py

parent 1fa81170
No related branches found
No related tags found
No related merge requests found
"""
This is a standalone impelementation of a scan algorithm:
For every permutation of family order
try to seat them from left to right
if all seated terminate
"""
import copy
w = 9
h = 6
cinema = []
group_descr = [4, 4, 0, 1, 1, 0, 0, 0]
# group_descr = [2,2,1,1,0,0,0,0]
groups = []
for el in range(len(group_descr)):
for i in range(group_descr[el]):
groups.append(el + 1)
groups.reverse()
print(groups)
max_people = sum(groups)
def is_free(cinema, x, y):
if x < 0 or x >= w:
return False
if y < 0 or y >= h:
return False
return cinema[x + w * y] == 1
def is_safe(cinema, x, y):
if x < 0 or x >= w:
return True
if y < 0 or y >= h:
return True
return cinema[x + w * y] == 1 or cinema[x + w * y] == 0
def can_seat(cinema, x, y):
# is the seat free?
ret = is_free(cinema, x, y)
# is the rest free?
ret &= is_safe(cinema, x + 1, y)
ret &= is_safe(cinema, x + 1, y + 1)
ret &= is_safe(cinema, x, y + 1)
ret &= is_safe(cinema, x - 1, y + 1)
ret &= is_safe(cinema, x - 1, y)
ret &= is_safe(cinema, x - 1, y + 1)
ret &= is_safe(cinema, x, y + 1)
ret &= is_safe(cinema, x + 1, y + 1)
# extra two seats on the row
ret &= is_safe(cinema, x - 2, y)
ret &= is_safe(cinema, x + 2, y)
return ret
def can_group_seat(cinema, x, y, s):
ret = True
for i in range(x, x + s):
ret &= can_seat(cinema, i, y)
return ret
def set_group_seat(cinema, x, y, s):
for i in range(x, x + s):
cinema[i + w * y] = 2
def print_cinema(cinema):
for y in range(h):
for x in range(w):
print("x" if cinema[x + w * y] == 2 else cinema[x + w * y], end="")
print()
def encode(groups):
buckets = [0, 0, 0, 0, 0, 0, 0, 0]
for group in groups:
buckets[group - 1] += 1
return tuple(buckets)
mem = {}
lookups = 0
calcs = 0
def main(groups, cinema, first=False):
global lookups
global calcs
if len(groups) == 0:
return 0
max_score = 0
group, groups = groups[0], groups[1:]
# maximize the score over x and y and the first group
for y in range(h):
for x in range(w - group):
if first:
print(x, y)
if not can_group_seat(cinema, x, y, group):
continue
# copy the cinema and seat the group
new_cinema = copy.copy(cinema)
set_group_seat(new_cinema, x, y, group)
encoded_groups = encode(groups)
encoded_cinema = tuple(new_cinema)
mem_key = (encoded_groups, encoded_cinema)
if mem_key in mem:
score = group + mem[mem_key]
lookups += 1
else:
rest_score = main(groups, new_cinema)
score = group + rest_score
mem[mem_key] = rest_score
calcs += 1
max_score = max(score, max_score)
# Final case, calculate the score when the group is discarded
base_score = 0
encoded_groups = encode(groups)
encoded_cinema = tuple(cinema)
mem_key = (encoded_groups, encoded_cinema)
if mem_key in mem:
base_score = mem[mem_key]
else:
base_score = main(groups, cinema)
mem[mem_key] = base_score
return max(max_score, base_score)
def go():
cinema = [
0,
1,
1,
1,
0,
1,
1,
1,
1,
1,
1,
1,
1,
0,
1,
1,
1,
1,
1,
1,
1,
1,
0,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
0,
1,
1,
1,
1,
1,
1,
1,
1,
0,
1,
1,
1,
1,
]
print(main(groups, cinema, True))
print(f"lookups: {lookups}")
print(f"calcs: {calcs}")
go()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment