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.
My Python solution:
'
def queensAttack(n, k, r_q, c_q, obstacles):
directions = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
total = 0
# obstacles = set(obstacles)
obstacles = set(tuple(obs) for obs in obstacles)
for dx, dy in directions:
x, y = r_q, c_q
while True:
x += dx
y += dy
if not (1 <= x <= n):
break
if not (1 <= y <= n):
break
if (x, y) in obstacles:
break
total += 1
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 →
My Python solution: ' def queensAttack(n, k, r_q, c_q, obstacles): directions = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)] total = 0 # obstacles = set(obstacles) obstacles = set(tuple(obs) for obs in obstacles) for dx, dy in directions: x, y = r_q, c_q while True: x += dx y += dy if not (1 <= x <= n): break if not (1 <= y <= n): break if (x, y) in obstacles: break total += 1 return total
'