Skip to content

Instantly share code, notes, and snippets.

@KoStard
Created May 1, 2018 14:55
Show Gist options
  • Save KoStard/9fff5227f57e30df5156460845fdee85 to your computer and use it in GitHub Desktop.
Save KoStard/9fff5227f57e30df5156460845fdee85 to your computer and use it in GitHub Desktop.
Task 90 of UniLecs in Telegram
def task90(M, N, A, B, cords):
massive = [["*" if any((cord[0]==x and cord[1]==y)
or (x in range(cord[0]-A+1, cord[0]+1) and y in range(cord[1]-B+1, cord[1]+1))
or (x in range(M-A+1, M+1))
or (y in range(N-B+1, N+1))
for cord in cords) else "_" for x in range(M)] for y in range(N)]
# print(*massive, sep = "\n") # Uncomment this for visualization
res = 0
for row in massive:
for elem in row:
if elem == "_":
res+=1
print(res)
# Explanation
# So the solution is here. Let's discuss an example, where
# M = N = 4
# A = B = 2
# (2, 2) is aleady used
# So we have this.
# ['_', '_', '_', '_']
# ['_', '_', '_', '_']
# ['_', '_', '*', '_']
# ['_', '_', '_', '_']
# As we can see, we can't put the 2x2 block's left top corner in ^s too.
# ['_', '_', '_', '_']
# ['_', '^', '^', '_']
# ['_', '^', '*', '_']
# ['_', '_', '_', '_']
# So we 'block' these elements too.
# ['_', '_', '_', '_']
# ['_', '*', '*', '_']
# ['_', '*', '*', '_']
# ['_', '_', '_', '_']
# But! We can't put the 2x2 block's left top corner in ^s too.
# ['_', '_', '_', '^']
# ['_', '*', '*', '^']
# ['_', '*', '*', '^']
# ['^', '^', '^', '^']
# So let's block these elements too.
# ['_', '_', '_', '*']
# ['_', '*', '*', '*']
# ['_', '*', '*', '*']
# ['*', '*', '*', '*']
# Done! We can put AxB block's top left corner in free elements (_). Just count them and that will be the result!
task90(4, 4, 2, 2, ((1, 1), (1, 3), (2, 2), (2, 4), (3, 4), (4, 1), ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment