Queen's Attack II Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def queensAttack(n, k, r_q, c_q, obstacles):
sum_attack_queen = 0
obstacles_set = set(tuple(obstacle) for obstacle in obstacles)
def count_attacks(dx, dy):
count = 0
x, y = r_q + dx, c_q + dy
while 1 <= x <= n and 1 <= y <= n:
if (x, y) in obstacles_set:
break
count += 1
x += dx
y += dy
return count
# Directions (dx, dy)
directions = [
(1, 0), # up
(-1, 0), # down
(0, 1), # right
(0, -1), # left
(1, 1), # up-right
(1, -1), # up-left
(-1, 1), # down-right
(-1, -1) # down-left
]
for dx, dy in directions:
sum_attack_queen += count_attacks(dx, dy)
return sum_attack_queen
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queen's Attack II
You are viewing a single comment's thread. Return to all comments →
def queensAttack(n, k, r_q, c_q, obstacles): sum_attack_queen = 0 obstacles_set = set(tuple(obstacle) for obstacle in obstacles)