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):
# Create a set of obstacle positions
obstacles = set(tuple(obstacle) for obstacle in obstacles)
# Directions for the queen's movements
directions = [
(1, 0), # Down
(-1, 0), # Up
(0, 1), # Right
(0, -1), # Left
(1, 1), # Down-Right
(1, -1), # Down-Left
(-1, 1), # Up-Right
(-1, -1) # Up-Left
]
total = 0
for dx, dy in directions:
x, y = r_q, c_q
# Initialize the maximum reachable distance in this direction
max_distance = n # maximum distance to the edge of the board
while True:
x += dx
y += dy
# Check if the new position is within the board limits
if not (1 <= x <= n and 1 <= y <= n):
break
# Check if there's an obstacle
if (x, y) in obstacles:
break
total += 1 # Increment the count of positions the queen can attack
return total
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): # Create a set of obstacle positions obstacles = set(tuple(obstacle) for obstacle in obstacles)