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.
This code works..!! Huhh.. finallyy..!!:))
def queensAttack(n, k, r_q, c_q, obstacles):
obstacle_set = {(r, c) for r, c in obstacles}
moves = 0
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 dr, dc in directions:
r, c = r_q + dr, c_q + dc
while 1 <= r <= n and 1 <= c <= n:
if (r, c) in obstacle_set:
break
moves += 1
r += dr
c += dc
return moves
if name == 'main':
n, k = map(int, input().split())
r_q, c_q = map(int, input().split())
obstacles = [tuple(map(int, input().split())) for _ in range(k)]
print(queensAttack(n, k, r_q, c_q, obstacles))
`
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 →
This code works..!! Huhh.. finallyy..!!:)) def queensAttack(n, k, r_q, c_q, obstacles): obstacle_set = {(r, c) for r, c in obstacles} moves = 0 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 dr, dc in directions: r, c = r_q + dr, c_q + dc while 1 <= r <= n and 1 <= c <= n: if (r, c) in obstacle_set: break moves += 1 r += dr c += dc return moves if name == 'main': n, k = map(int, input().split()) r_q, c_q = map(int, input().split()) obstacles = [tuple(map(int, input().split())) for _ in range(k)] print(queensAttack(n, k, r_q, c_q, obstacles))
`